Add ios payload testing.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
3538cfba4d
commit
a41e63bcb7
|
@ -13,7 +13,21 @@ type ExtendJSON struct {
|
||||||
Value string `json:"val"`
|
Value string `json:"val"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type alert struct {
|
const (
|
||||||
|
// PriorityLow will tell APNs to send the push message at a time that takes
|
||||||
|
// into account power considerations for the device. Notifications with this
|
||||||
|
// priority might be grouped and delivered in bursts. They are throttled, and
|
||||||
|
// in some cases are not delivered.
|
||||||
|
ApnsPriorityLow = 5
|
||||||
|
|
||||||
|
// PriorityHigh will tell APNs to send the push message immediately.
|
||||||
|
// Notifications with this priority must trigger an alert, sound, or badge on
|
||||||
|
// the target device. It is an error to use this priority for a push
|
||||||
|
// notification that contains only the content-available key.
|
||||||
|
ApnsPriorityHigh = 10
|
||||||
|
)
|
||||||
|
|
||||||
|
type Alert struct {
|
||||||
Action string `json:"action,omitempty"`
|
Action string `json:"action,omitempty"`
|
||||||
ActionLocKey string `json:"action-loc-key,omitempty"`
|
ActionLocKey string `json:"action-loc-key,omitempty"`
|
||||||
Body string `json:"body,omitempty"`
|
Body string `json:"body,omitempty"`
|
||||||
|
@ -48,12 +62,10 @@ type RequestPushNotification struct {
|
||||||
Topic string `json:"topic,omitempty"`
|
Topic string `json:"topic,omitempty"`
|
||||||
Badge int `json:"badge,omitempty"`
|
Badge int `json:"badge,omitempty"`
|
||||||
Sound string `json:"sound,omitempty"`
|
Sound string `json:"sound,omitempty"`
|
||||||
Expiry int `json:"expiry,omitempty"`
|
|
||||||
Retry int `json:"retry,omitempty"`
|
|
||||||
Category string `json:"category,omitempty"`
|
Category string `json:"category,omitempty"`
|
||||||
URLArgs []string `json:"url-args,omitempty"`
|
URLArgs []string `json:"url-args,omitempty"`
|
||||||
Extend []ExtendJSON `json:"extend,omitempty"`
|
Extend []ExtendJSON `json:"extend,omitempty"`
|
||||||
Alert alert `json:"alert,omitempty"`
|
Alert Alert `json:"alert,omitempty"`
|
||||||
|
|
||||||
// meta
|
// meta
|
||||||
IDs []uint64 `json:"seq_id,omitempty"`
|
IDs []uint64 `json:"seq_id,omitempty"`
|
||||||
|
@ -100,13 +112,8 @@ func pushNotification(notification RequestPushNotification) bool {
|
||||||
return success
|
return success
|
||||||
}
|
}
|
||||||
|
|
||||||
func pushNotificationIos(req RequestPushNotification) bool {
|
func GetIOSNotification(req RequestPushNotification) *apns.Notification {
|
||||||
|
|
||||||
// The Remote Notification Payload
|
|
||||||
// https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html
|
|
||||||
for _, token := range req.Tokens {
|
|
||||||
notification := &apns.Notification{}
|
notification := &apns.Notification{}
|
||||||
notification.DeviceToken = token
|
|
||||||
|
|
||||||
if len(req.ApnsID) > 0 {
|
if len(req.ApnsID) > 0 {
|
||||||
notification.ApnsID = req.ApnsID
|
notification.ApnsID = req.ApnsID
|
||||||
|
@ -150,8 +157,12 @@ func pushNotificationIos(req RequestPushNotification) bool {
|
||||||
payload.AlertTitleLocKey(req.Alert.TitleLocKey)
|
payload.AlertTitleLocKey(req.Alert.TitleLocKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(req.Alert.LocArgs) > 0 {
|
// if len(req.Alert.LocArgs) > 0 {
|
||||||
payload.AlertTitleLocArgs(req.Alert.LocArgs)
|
// payload.AlertLocArgs(req.Alert.LocArgs)
|
||||||
|
// }
|
||||||
|
|
||||||
|
if len(req.Alert.TitleLocArgs) > 0 {
|
||||||
|
payload.AlertTitleLocArgs(req.Alert.TitleLocArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(req.Alert.Body) > 0 {
|
if len(req.Alert.Body) > 0 {
|
||||||
|
@ -186,6 +197,18 @@ func pushNotificationIos(req RequestPushNotification) bool {
|
||||||
|
|
||||||
notification.Payload = payload
|
notification.Payload = payload
|
||||||
|
|
||||||
|
return notification
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
// send ios notification
|
// send ios notification
|
||||||
res, err := ApnsClient.Push(notification)
|
res, err := ApnsClient.Push(notification)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,120 @@
|
||||||
|
package gopush
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/buger/jsonparser"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIOSNotificationStructure(t *testing.T) {
|
||||||
|
var dat map[string]interface{}
|
||||||
|
|
||||||
|
test := "test"
|
||||||
|
message := "Welcome notification Server"
|
||||||
|
req := RequestPushNotification{
|
||||||
|
ApnsID: test,
|
||||||
|
Topic: test,
|
||||||
|
Priority: "normal",
|
||||||
|
Message: message,
|
||||||
|
Badge: 1,
|
||||||
|
Sound: test,
|
||||||
|
ContentAvailable: true,
|
||||||
|
Extend: []ExtendJSON{
|
||||||
|
{
|
||||||
|
Key: "key1",
|
||||||
|
Value: "1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Key: "key2",
|
||||||
|
Value: "2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Category: test,
|
||||||
|
URLArgs: []string{"a", "b"},
|
||||||
|
}
|
||||||
|
|
||||||
|
notification := GetIOSNotification(req)
|
||||||
|
|
||||||
|
dump, _ := json.Marshal(notification.Payload)
|
||||||
|
data := []byte(string(dump))
|
||||||
|
|
||||||
|
if err := json.Unmarshal(data, &dat); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
alert, _ := jsonparser.GetString(data, "aps", "alert")
|
||||||
|
badge, _ := jsonparser.GetInt(data, "aps", "badge")
|
||||||
|
sound, _ := jsonparser.GetString(data, "aps", "sound")
|
||||||
|
contentAvailable, _ := jsonparser.GetInt(data, "aps", "content-available")
|
||||||
|
category, _ := jsonparser.GetString(data, "aps", "category")
|
||||||
|
key1 := dat["key1"].(string)
|
||||||
|
key2 := dat["key2"].(string)
|
||||||
|
aps := dat["aps"].(map[string]interface{})
|
||||||
|
urlArgs := aps["url-args"].([]interface{})
|
||||||
|
|
||||||
|
assert.Equal(t, test, notification.ApnsID)
|
||||||
|
assert.Equal(t, test, notification.Topic)
|
||||||
|
assert.Equal(t, ApnsPriorityLow, notification.Priority)
|
||||||
|
assert.Equal(t, message, alert)
|
||||||
|
assert.Equal(t, 1, int(badge))
|
||||||
|
assert.Equal(t, test, sound)
|
||||||
|
assert.Equal(t, 1, int(contentAvailable))
|
||||||
|
assert.Equal(t, "1", key1)
|
||||||
|
assert.Equal(t, "2", key2)
|
||||||
|
assert.Equal(t, test, category)
|
||||||
|
assert.Contains(t, urlArgs, "a")
|
||||||
|
assert.Contains(t, urlArgs, "b")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIOSAlertNotificationStructure(t *testing.T) {
|
||||||
|
var dat map[string]interface{}
|
||||||
|
|
||||||
|
test := "test"
|
||||||
|
req := RequestPushNotification{
|
||||||
|
Alert: Alert{
|
||||||
|
Action: test,
|
||||||
|
ActionLocKey: test,
|
||||||
|
Body: test,
|
||||||
|
LaunchImage: test,
|
||||||
|
LocArgs: []string{"a", "b"},
|
||||||
|
LocKey: test,
|
||||||
|
Title: test,
|
||||||
|
TitleLocArgs: []string{"a", "b"},
|
||||||
|
TitleLocKey: test,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
notification := GetIOSNotification(req)
|
||||||
|
|
||||||
|
dump, _ := json.Marshal(notification.Payload)
|
||||||
|
data := []byte(string(dump))
|
||||||
|
|
||||||
|
if err := json.Unmarshal(data, &dat); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
action, _ := jsonparser.GetString(data, "aps", "alert", "action")
|
||||||
|
actionLocKey, _ := jsonparser.GetString(data, "aps", "alert","action-loc-key")
|
||||||
|
body, _ := jsonparser.GetString(data, "aps", "alert","body")
|
||||||
|
launchImage, _ := jsonparser.GetString(data, "aps", "alert","launch-image")
|
||||||
|
locKey, _ := jsonparser.GetString(data, "aps", "alert","loc-key")
|
||||||
|
title, _ := jsonparser.GetString(data, "aps", "alert","title")
|
||||||
|
titleLocKey, _ := jsonparser.GetString(data, "aps", "alert","title-loc-key")
|
||||||
|
aps := dat["aps"].(map[string]interface{})
|
||||||
|
alert := aps["alert"].(map[string]interface{})
|
||||||
|
titleLocArgs := alert["title-loc-args"].([]interface{})
|
||||||
|
|
||||||
|
assert.Equal(t, test, action)
|
||||||
|
assert.Equal(t, test, actionLocKey)
|
||||||
|
assert.Equal(t, test, body)
|
||||||
|
assert.Equal(t, test, launchImage)
|
||||||
|
assert.Equal(t, test, locKey)
|
||||||
|
assert.Equal(t, test, title)
|
||||||
|
assert.Equal(t, test, titleLocKey)
|
||||||
|
assert.Contains(t, titleLocArgs, "a")
|
||||||
|
assert.Contains(t, titleLocArgs, "b")
|
||||||
|
}
|
Loading…
Reference in New Issue