Support get different mode of apns client in request for iOS app (#301)

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2017-10-25 09:51:33 -05:00 committed by GitHub
parent c06e819e08
commit 313d74c927
3 changed files with 51 additions and 1 deletions

View File

@ -82,6 +82,8 @@ type PushNotification struct {
URLArgs []string `json:"url-args,omitempty"` URLArgs []string `json:"url-args,omitempty"`
Alert Alert `json:"alert,omitempty"` Alert Alert `json:"alert,omitempty"`
MutableContent bool `json:"mutable-content,omitempty"` MutableContent bool `json:"mutable-content,omitempty"`
Production bool `json:"production,omitempty"`
Development bool `json:"development,omitempty"`
} }
// WaitDone decrements the WaitGroup counter. // WaitDone decrements the WaitGroup counter.

View File

@ -176,6 +176,21 @@ func GetIOSNotification(req PushNotification) *apns2.Notification {
return notification return notification
} }
func getApnsClient(req PushNotification) (client *apns2.Client) {
if req.Production {
client = ApnsClient.Production()
} else if req.Development {
client = ApnsClient.Development()
} else {
if PushConf.Ios.Production {
client = ApnsClient.Production()
} else {
client = ApnsClient.Development()
}
}
return
}
// PushToIOS provide send notification to APNs server. // PushToIOS provide send notification to APNs server.
func PushToIOS(req PushNotification) bool { func PushToIOS(req PushNotification) bool {
LogAccess.Debug("Start push notification for iOS") LogAccess.Debug("Start push notification for iOS")
@ -199,12 +214,13 @@ Retry:
) )
notification := GetIOSNotification(req) notification := GetIOSNotification(req)
client := getApnsClient(req)
for _, token := range req.Tokens { for _, token := range req.Tokens {
notification.DeviceToken = token notification.DeviceToken = token
// send ios notification // send ios notification
res, err := ApnsClient.Push(notification) res, err := client.Push(notification)
if err != nil { if err != nil {
// apns server error // apns server error

View File

@ -456,3 +456,35 @@ func TestPushToIOS(t *testing.T) {
isError := PushToIOS(req) isError := PushToIOS(req)
assert.True(t, isError) assert.True(t, isError)
} }
func TestApnsHostFromRequest(t *testing.T) {
PushConf, _ = config.LoadConf("")
PushConf.Ios.Enabled = true
PushConf.Ios.KeyPath = "../certificate/certificate-valid.pem"
err := InitAPNSClient()
assert.Nil(t, err)
err = InitAppStatus()
assert.Nil(t, err)
req := PushNotification{
Production: true,
}
client := getApnsClient(req)
assert.Equal(t, apns2.HostProduction, client.Host)
req = PushNotification{
Development: true,
}
client = getApnsClient(req)
assert.Equal(t, apns2.HostDevelopment, client.Host)
req = PushNotification{}
PushConf.Ios.Production = true
client = getApnsClient(req)
assert.Equal(t, apns2.HostProduction, client.Host)
PushConf.Ios.Production = false
client = getApnsClient(req)
assert.Equal(t, apns2.HostDevelopment, client.Host)
}