Skip to content

Commit

Permalink
Support get different mode of apns client in request for iOS app (#301)
Browse files Browse the repository at this point in the history
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy authored Oct 25, 2017
1 parent c06e819 commit 313d74c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions gorush/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ type PushNotification struct {
URLArgs []string `json:"url-args,omitempty"`
Alert Alert `json:"alert,omitempty"`
MutableContent bool `json:"mutable-content,omitempty"`
Production bool `json:"production,omitempty"`
Development bool `json:"development,omitempty"`
}

// WaitDone decrements the WaitGroup counter.
Expand Down
18 changes: 17 additions & 1 deletion gorush/notification_apns.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,21 @@ func GetIOSNotification(req PushNotification) *apns2.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.
func PushToIOS(req PushNotification) bool {
LogAccess.Debug("Start push notification for iOS")
Expand All @@ -199,12 +214,13 @@ Retry:
)

notification := GetIOSNotification(req)
client := getApnsClient(req)

for _, token := range req.Tokens {
notification.DeviceToken = token

// send ios notification
res, err := ApnsClient.Push(notification)
res, err := client.Push(notification)

if err != nil {
// apns server error
Expand Down
32 changes: 32 additions & 0 deletions gorush/notification_apns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,35 @@ func TestPushToIOS(t *testing.T) {
isError := PushToIOS(req)
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)
}

0 comments on commit 313d74c

Please sign in to comment.