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"`
|
||||
}
|
||||
|
||||
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"`
|
||||
ActionLocKey string `json:"action-loc-key,omitempty"`
|
||||
Body string `json:"body,omitempty"`
|
||||
|
@ -48,12 +62,10 @@ type RequestPushNotification struct {
|
|||
Topic string `json:"topic,omitempty"`
|
||||
Badge int `json:"badge,omitempty"`
|
||||
Sound string `json:"sound,omitempty"`
|
||||
Expiry int `json:"expiry,omitempty"`
|
||||
Retry int `json:"retry,omitempty"`
|
||||
Category string `json:"category,omitempty"`
|
||||
URLArgs []string `json:"url-args,omitempty"`
|
||||
Extend []ExtendJSON `json:"extend,omitempty"`
|
||||
Alert alert `json:"alert,omitempty"`
|
||||
Alert Alert `json:"alert,omitempty"`
|
||||
|
||||
// meta
|
||||
IDs []uint64 `json:"seq_id,omitempty"`
|
||||
|
@ -100,92 +112,103 @@ func pushNotification(notification RequestPushNotification) bool {
|
|||
return success
|
||||
}
|
||||
|
||||
func GetIOSNotification(req RequestPushNotification) *apns.Notification {
|
||||
notification := &apns.Notification{}
|
||||
|
||||
if len(req.ApnsID) > 0 {
|
||||
notification.ApnsID = req.ApnsID
|
||||
}
|
||||
|
||||
if len(req.Topic) > 0 {
|
||||
notification.Topic = req.Topic
|
||||
}
|
||||
|
||||
if len(req.Priority) > 0 && req.Priority == "normal" {
|
||||
notification.Priority = apns.PriorityLow
|
||||
}
|
||||
|
||||
payload := payload.NewPayload().Alert(req.Message)
|
||||
|
||||
if req.Badge > 0 {
|
||||
payload.Badge(req.Badge)
|
||||
}
|
||||
|
||||
if len(req.Sound) > 0 {
|
||||
payload.Sound(req.Sound)
|
||||
}
|
||||
|
||||
if req.ContentAvailable {
|
||||
payload.ContentAvailable()
|
||||
}
|
||||
|
||||
if len(req.Extend) > 0 {
|
||||
for _, extend := range req.Extend {
|
||||
payload.Custom(extend.Key, extend.Value)
|
||||
}
|
||||
}
|
||||
|
||||
// Alert dictionary
|
||||
|
||||
if len(req.Alert.Title) > 0 {
|
||||
payload.AlertTitle(req.Alert.Title)
|
||||
}
|
||||
|
||||
if len(req.Alert.TitleLocKey) > 0 {
|
||||
payload.AlertTitleLocKey(req.Alert.TitleLocKey)
|
||||
}
|
||||
|
||||
// if len(req.Alert.LocArgs) > 0 {
|
||||
// payload.AlertLocArgs(req.Alert.LocArgs)
|
||||
// }
|
||||
|
||||
if len(req.Alert.TitleLocArgs) > 0 {
|
||||
payload.AlertTitleLocArgs(req.Alert.TitleLocArgs)
|
||||
}
|
||||
|
||||
if len(req.Alert.Body) > 0 {
|
||||
payload.AlertBody(req.Alert.Body)
|
||||
}
|
||||
|
||||
if len(req.Alert.LaunchImage) > 0 {
|
||||
payload.AlertLaunchImage(req.Alert.LaunchImage)
|
||||
}
|
||||
|
||||
if len(req.Alert.LocKey) > 0 {
|
||||
payload.AlertLocKey(req.Alert.LocKey)
|
||||
}
|
||||
|
||||
if len(req.Alert.Action) > 0 {
|
||||
payload.AlertAction(req.Alert.Action)
|
||||
}
|
||||
|
||||
if len(req.Alert.ActionLocKey) > 0 {
|
||||
payload.AlertActionLocKey(req.Alert.ActionLocKey)
|
||||
}
|
||||
|
||||
// General
|
||||
|
||||
if len(req.Category) > 0 {
|
||||
payload.Category(req.Category)
|
||||
}
|
||||
|
||||
if len(req.URLArgs) > 0 {
|
||||
payload.URLArgs(req.URLArgs)
|
||||
}
|
||||
|
||||
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 := &apns.Notification{}
|
||||
notification.DeviceToken = token
|
||||
|
||||
if len(req.ApnsID) > 0 {
|
||||
notification.ApnsID = req.ApnsID
|
||||
}
|
||||
|
||||
if len(req.Topic) > 0 {
|
||||
notification.Topic = req.Topic
|
||||
}
|
||||
|
||||
if len(req.Priority) > 0 && req.Priority == "normal" {
|
||||
notification.Priority = apns.PriorityLow
|
||||
}
|
||||
|
||||
payload := payload.NewPayload().Alert(req.Message)
|
||||
|
||||
if req.Badge > 0 {
|
||||
payload.Badge(req.Badge)
|
||||
}
|
||||
|
||||
if len(req.Sound) > 0 {
|
||||
payload.Sound(req.Sound)
|
||||
}
|
||||
|
||||
if req.ContentAvailable {
|
||||
payload.ContentAvailable()
|
||||
}
|
||||
|
||||
if len(req.Extend) > 0 {
|
||||
for _, extend := range req.Extend {
|
||||
payload.Custom(extend.Key, extend.Value)
|
||||
}
|
||||
}
|
||||
|
||||
// Alert dictionary
|
||||
|
||||
if len(req.Alert.Title) > 0 {
|
||||
payload.AlertTitle(req.Alert.Title)
|
||||
}
|
||||
|
||||
if len(req.Alert.TitleLocKey) > 0 {
|
||||
payload.AlertTitleLocKey(req.Alert.TitleLocKey)
|
||||
}
|
||||
|
||||
if len(req.Alert.LocArgs) > 0 {
|
||||
payload.AlertTitleLocArgs(req.Alert.LocArgs)
|
||||
}
|
||||
|
||||
if len(req.Alert.Body) > 0 {
|
||||
payload.AlertBody(req.Alert.Body)
|
||||
}
|
||||
|
||||
if len(req.Alert.LaunchImage) > 0 {
|
||||
payload.AlertLaunchImage(req.Alert.LaunchImage)
|
||||
}
|
||||
|
||||
if len(req.Alert.LocKey) > 0 {
|
||||
payload.AlertLocKey(req.Alert.LocKey)
|
||||
}
|
||||
|
||||
if len(req.Alert.Action) > 0 {
|
||||
payload.AlertAction(req.Alert.Action)
|
||||
}
|
||||
|
||||
if len(req.Alert.ActionLocKey) > 0 {
|
||||
payload.AlertActionLocKey(req.Alert.ActionLocKey)
|
||||
}
|
||||
|
||||
// General
|
||||
|
||||
if len(req.Category) > 0 {
|
||||
payload.Category(req.Category)
|
||||
}
|
||||
|
||||
if len(req.URLArgs) > 0 {
|
||||
payload.URLArgs(req.URLArgs)
|
||||
}
|
||||
|
||||
notification.Payload = payload
|
||||
|
||||
// send ios 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