add android payload testing.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
a41e63bcb7
commit
5bd75c8624
|
@ -112,6 +112,8 @@ func pushNotification(notification RequestPushNotification) bool {
|
|||
return success
|
||||
}
|
||||
|
||||
// The iOS Notification Payload
|
||||
// ref: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html
|
||||
func GetIOSNotification(req RequestPushNotification) *apns.Notification {
|
||||
notification := &apns.Notification{}
|
||||
|
||||
|
@ -157,6 +159,7 @@ func GetIOSNotification(req RequestPushNotification) *apns.Notification {
|
|||
payload.AlertTitleLocKey(req.Alert.TitleLocKey)
|
||||
}
|
||||
|
||||
// Need send PR to apns2 repo.
|
||||
// if len(req.Alert.LocArgs) > 0 {
|
||||
// payload.AlertLocArgs(req.Alert.LocArgs)
|
||||
// }
|
||||
|
@ -204,8 +207,6 @@ func pushNotificationIos(req RequestPushNotification) bool {
|
|||
|
||||
notification := GetIOSNotification(req)
|
||||
|
||||
// The Remote Notification Payload
|
||||
// https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html
|
||||
for _, token := range req.Tokens {
|
||||
notification.DeviceToken = token
|
||||
|
||||
|
@ -228,10 +229,9 @@ func pushNotificationIos(req RequestPushNotification) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func pushNotificationAndroid(req RequestPushNotification) bool {
|
||||
|
||||
// HTTP Connection Server Reference for Android
|
||||
// https://developers.google.com/cloud-messaging/http-server-ref
|
||||
func GetAndroidNotification(req RequestPushNotification) gcm.HttpMessage {
|
||||
notification := gcm.HttpMessage{}
|
||||
|
||||
notification.RegistrationIds = req.Tokens
|
||||
|
@ -279,6 +279,13 @@ func pushNotificationAndroid(req RequestPushNotification) bool {
|
|||
notification.Notification.Body = req.Message
|
||||
}
|
||||
|
||||
return notification
|
||||
}
|
||||
|
||||
func pushNotificationAndroid(req RequestPushNotification) bool {
|
||||
|
||||
notification := GetAndroidNotification(req)
|
||||
|
||||
res, err := gcm.SendHttp(PushConf.Android.ApiKey, notification)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
package gopush
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/buger/jsonparser"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"github.com/buger/jsonparser"
|
||||
"github.com/google/go-gcm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIOSNotificationStructure(t *testing.T) {
|
||||
|
@ -118,3 +119,40 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
|
|||
assert.Contains(t, titleLocArgs, "a")
|
||||
assert.Contains(t, titleLocArgs, "b")
|
||||
}
|
||||
|
||||
func TestAndroidNotificationStructure(t *testing.T) {
|
||||
|
||||
test := "test"
|
||||
req := RequestPushNotification{
|
||||
Tokens: []string{"a", "b"},
|
||||
Message: "Welcome",
|
||||
To: test,
|
||||
Priority: "high",
|
||||
CollapseKey: "1",
|
||||
ContentAvailable: true,
|
||||
DelayWhileIdle: true,
|
||||
TimeToLive: 100,
|
||||
RestrictedPackageName: test,
|
||||
DryRun: true,
|
||||
Data: map[string]interface{}{
|
||||
"a": "1",
|
||||
"b": "2",
|
||||
},
|
||||
Notification: gcm.Notification{
|
||||
Title: test,
|
||||
},
|
||||
}
|
||||
|
||||
notification := GetAndroidNotification(req)
|
||||
|
||||
assert.Equal(t, test, notification.To)
|
||||
assert.Equal(t, "high", notification.Priority)
|
||||
assert.Equal(t, "1", notification.CollapseKey)
|
||||
assert.True(t, notification.ContentAvailable)
|
||||
assert.True(t, notification.DelayWhileIdle)
|
||||
assert.Equal(t, 100, int(notification.TimeToLive))
|
||||
assert.Equal(t, test, notification.RestrictedPackageName)
|
||||
assert.True(t, notification.DryRun)
|
||||
assert.Equal(t, test, notification.Notification.Title)
|
||||
assert.Equal(t, "Welcome", notification.Notification.Body)
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ func TestMissingParameterPushHandler(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestIosPushHandler(t *testing.T) {
|
||||
func TestDisabledIosPushHandler(t *testing.T) {
|
||||
initTest()
|
||||
|
||||
PushConf.Ios.Enabled = false
|
||||
|
@ -84,6 +84,25 @@ func TestIosPushHandler(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestDisabledAndroidPushHandler(t *testing.T) {
|
||||
initTest()
|
||||
|
||||
PushConf.Android.Enabled = false
|
||||
|
||||
r := gofight.New()
|
||||
|
||||
r.POST("/api/push").
|
||||
SetJSON(gofight.D{
|
||||
"tokens": []string{"aaaaaa", "bbbbb"},
|
||||
"platform": 2,
|
||||
"message": "Welcome",
|
||||
}).
|
||||
Run(GetMainEngine(), func(r gofight.HttpResponse, rq gofight.HttpRequest) {
|
||||
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAndroidPushHandler(t *testing.T) {
|
||||
initTest()
|
||||
|
||||
|
|
Loading…
Reference in New Issue