add android payload testing.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
a41e63bcb7
commit
5bd75c8624
|
@ -112,11 +112,13 @@ func pushNotification(notification RequestPushNotification) bool {
|
||||||
return success
|
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 {
|
func GetIOSNotification(req RequestPushNotification) *apns.Notification {
|
||||||
notification := &apns.Notification{}
|
notification := &apns.Notification{}
|
||||||
|
|
||||||
if len(req.ApnsID) > 0 {
|
if len(req.ApnsID) > 0 {
|
||||||
notification.ApnsID = req.ApnsID
|
notification.ApnsID = req.ApnsID
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(req.Topic) > 0 {
|
if len(req.Topic) > 0 {
|
||||||
|
@ -157,6 +159,7 @@ func GetIOSNotification(req RequestPushNotification) *apns.Notification {
|
||||||
payload.AlertTitleLocKey(req.Alert.TitleLocKey)
|
payload.AlertTitleLocKey(req.Alert.TitleLocKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Need send PR to apns2 repo.
|
||||||
// if len(req.Alert.LocArgs) > 0 {
|
// if len(req.Alert.LocArgs) > 0 {
|
||||||
// payload.AlertLocArgs(req.Alert.LocArgs)
|
// payload.AlertLocArgs(req.Alert.LocArgs)
|
||||||
// }
|
// }
|
||||||
|
@ -204,8 +207,6 @@ func pushNotificationIos(req RequestPushNotification) bool {
|
||||||
|
|
||||||
notification := GetIOSNotification(req)
|
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 {
|
for _, token := range req.Tokens {
|
||||||
notification.DeviceToken = token
|
notification.DeviceToken = token
|
||||||
|
|
||||||
|
@ -228,10 +229,9 @@ func pushNotificationIos(req RequestPushNotification) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func pushNotificationAndroid(req RequestPushNotification) bool {
|
// HTTP Connection Server Reference for Android
|
||||||
|
// https://developers.google.com/cloud-messaging/http-server-ref
|
||||||
// HTTP Connection Server Reference for Android
|
func GetAndroidNotification(req RequestPushNotification) gcm.HttpMessage {
|
||||||
// https://developers.google.com/cloud-messaging/http-server-ref
|
|
||||||
notification := gcm.HttpMessage{}
|
notification := gcm.HttpMessage{}
|
||||||
|
|
||||||
notification.RegistrationIds = req.Tokens
|
notification.RegistrationIds = req.Tokens
|
||||||
|
@ -279,6 +279,13 @@ func pushNotificationAndroid(req RequestPushNotification) bool {
|
||||||
notification.Notification.Body = req.Message
|
notification.Notification.Body = req.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return notification
|
||||||
|
}
|
||||||
|
|
||||||
|
func pushNotificationAndroid(req RequestPushNotification) bool {
|
||||||
|
|
||||||
|
notification := GetAndroidNotification(req)
|
||||||
|
|
||||||
res, err := gcm.SendHttp(PushConf.Android.ApiKey, notification)
|
res, err := gcm.SendHttp(PushConf.Android.ApiKey, notification)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
package gopush
|
package gopush
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/buger/jsonparser"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"testing"
|
"github.com/buger/jsonparser"
|
||||||
|
"github.com/google/go-gcm"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"log"
|
"log"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIOSNotificationStructure(t *testing.T) {
|
func TestIOSNotificationStructure(t *testing.T) {
|
||||||
|
@ -14,25 +15,25 @@ func TestIOSNotificationStructure(t *testing.T) {
|
||||||
test := "test"
|
test := "test"
|
||||||
message := "Welcome notification Server"
|
message := "Welcome notification Server"
|
||||||
req := RequestPushNotification{
|
req := RequestPushNotification{
|
||||||
ApnsID: test,
|
ApnsID: test,
|
||||||
Topic: test,
|
Topic: test,
|
||||||
Priority: "normal",
|
Priority: "normal",
|
||||||
Message: message,
|
Message: message,
|
||||||
Badge: 1,
|
Badge: 1,
|
||||||
Sound: test,
|
Sound: test,
|
||||||
ContentAvailable: true,
|
ContentAvailable: true,
|
||||||
Extend: []ExtendJSON{
|
Extend: []ExtendJSON{
|
||||||
{
|
{
|
||||||
Key: "key1",
|
Key: "key1",
|
||||||
Value: "1",
|
Value: "1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Key: "key2",
|
Key: "key2",
|
||||||
Value: "2",
|
Value: "2",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Category: test,
|
Category: test,
|
||||||
URLArgs: []string{"a", "b"},
|
URLArgs: []string{"a", "b"},
|
||||||
}
|
}
|
||||||
|
|
||||||
notification := GetIOSNotification(req)
|
notification := GetIOSNotification(req)
|
||||||
|
@ -75,15 +76,15 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
|
||||||
test := "test"
|
test := "test"
|
||||||
req := RequestPushNotification{
|
req := RequestPushNotification{
|
||||||
Alert: Alert{
|
Alert: Alert{
|
||||||
Action: test,
|
Action: test,
|
||||||
ActionLocKey: test,
|
ActionLocKey: test,
|
||||||
Body: test,
|
Body: test,
|
||||||
LaunchImage: test,
|
LaunchImage: test,
|
||||||
LocArgs: []string{"a", "b"},
|
LocArgs: []string{"a", "b"},
|
||||||
LocKey: test,
|
LocKey: test,
|
||||||
Title: test,
|
Title: test,
|
||||||
TitleLocArgs: []string{"a", "b"},
|
TitleLocArgs: []string{"a", "b"},
|
||||||
TitleLocKey: test,
|
TitleLocKey: test,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,12 +99,12 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
action, _ := jsonparser.GetString(data, "aps", "alert", "action")
|
action, _ := jsonparser.GetString(data, "aps", "alert", "action")
|
||||||
actionLocKey, _ := jsonparser.GetString(data, "aps", "alert","action-loc-key")
|
actionLocKey, _ := jsonparser.GetString(data, "aps", "alert", "action-loc-key")
|
||||||
body, _ := jsonparser.GetString(data, "aps", "alert","body")
|
body, _ := jsonparser.GetString(data, "aps", "alert", "body")
|
||||||
launchImage, _ := jsonparser.GetString(data, "aps", "alert","launch-image")
|
launchImage, _ := jsonparser.GetString(data, "aps", "alert", "launch-image")
|
||||||
locKey, _ := jsonparser.GetString(data, "aps", "alert","loc-key")
|
locKey, _ := jsonparser.GetString(data, "aps", "alert", "loc-key")
|
||||||
title, _ := jsonparser.GetString(data, "aps", "alert","title")
|
title, _ := jsonparser.GetString(data, "aps", "alert", "title")
|
||||||
titleLocKey, _ := jsonparser.GetString(data, "aps", "alert","title-loc-key")
|
titleLocKey, _ := jsonparser.GetString(data, "aps", "alert", "title-loc-key")
|
||||||
aps := dat["aps"].(map[string]interface{})
|
aps := dat["aps"].(map[string]interface{})
|
||||||
alert := aps["alert"].(map[string]interface{})
|
alert := aps["alert"].(map[string]interface{})
|
||||||
titleLocArgs := alert["title-loc-args"].([]interface{})
|
titleLocArgs := alert["title-loc-args"].([]interface{})
|
||||||
|
@ -118,3 +119,40 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
|
||||||
assert.Contains(t, titleLocArgs, "a")
|
assert.Contains(t, titleLocArgs, "a")
|
||||||
assert.Contains(t, titleLocArgs, "b")
|
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()
|
initTest()
|
||||||
|
|
||||||
PushConf.Ios.Enabled = false
|
PushConf.Ios.Enabled = false
|
||||||
|
@ -74,9 +74,28 @@ func TestIosPushHandler(t *testing.T) {
|
||||||
|
|
||||||
r.POST("/api/push").
|
r.POST("/api/push").
|
||||||
SetJSON(gofight.D{
|
SetJSON(gofight.D{
|
||||||
"tokens": []string{"dc0ca2819417e528d8a4a01fc3e6bc7d2518ce738930c3b11203c0ef7c0fab8c"},
|
"tokens": []string{"dc0ca2819417e528d8a4a01fc3e6bc7d2518ce738930c3b11203c0ef7c0fab8c"},
|
||||||
"platform": 1,
|
"platform": 1,
|
||||||
"message": "Welcome",
|
"message": "Welcome",
|
||||||
|
}).
|
||||||
|
Run(GetMainEngine(), func(r gofight.HttpResponse, rq gofight.HttpRequest) {
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, r.Code)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
Run(GetMainEngine(), func(r gofight.HttpResponse, rq gofight.HttpRequest) {
|
||||||
|
|
||||||
|
@ -93,9 +112,9 @@ func TestAndroidPushHandler(t *testing.T) {
|
||||||
|
|
||||||
r.POST("/api/push").
|
r.POST("/api/push").
|
||||||
SetJSON(gofight.D{
|
SetJSON(gofight.D{
|
||||||
"tokens": []string{"aaaaaa", "bbbbb"},
|
"tokens": []string{"aaaaaa", "bbbbb"},
|
||||||
"platform": 2,
|
"platform": 2,
|
||||||
"message": "Welcome",
|
"message": "Welcome",
|
||||||
}).
|
}).
|
||||||
Run(GetMainEngine(), func(r gofight.HttpResponse, rq gofight.HttpRequest) {
|
Run(GetMainEngine(), func(r gofight.HttpResponse, rq gofight.HttpRequest) {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue