From 1707f39b7d6d5ff1cf081a91b22c15afe63fd508 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 24 Dec 2016 15:54:48 +0800 Subject: [PATCH] remove required field: message Fix #162 Fix #146 // "aps" : { // "alert" : { // "title" : "Game Request", // "body" : "Bob wants to play poker", // "action-loc-key" : "PLAY" // }, // "badge" : 5 // }, and // "aps" : { // "alert" : "You got your emails.", // "badge" : 9, // "sound" : "bingbong.aiff" // }, Signed-off-by: Bo-Yi Wu --- gorush/notification.go | 6 ++- gorush/notification_test.go | 89 +++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/gorush/notification.go b/gorush/notification.go index f04c46d..33d67e9 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -54,7 +54,7 @@ type PushNotification struct { // Common Tokens []string `json:"tokens" binding:"required"` Platform int `json:"platform" binding:"required"` - Message string `json:"message" binding:"required"` + Message string `json:"message,omitempty"` Title string `json:"title,omitempty"` Priority string `json:"priority,omitempty"` ContentAvailable bool `json:"content_available,omitempty"` @@ -239,6 +239,10 @@ func iosAlertDictionary(payload *payload.Payload, req PushNotification) *payload payload.AlertTitle(req.Title) } + if len(req.Alert.Title) > 0 { + payload.AlertTitle(req.Alert.Title) + } + // Apple Watch & Safari display this string as part of the notification interface. if len(req.Alert.Subtitle) > 0 { payload.AlertSubtitle(req.Alert.Subtitle) diff --git a/gorush/notification_test.go b/gorush/notification_test.go index f33b449..d6d2b88 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -216,6 +216,95 @@ func TestCheckSilentNotification(t *testing.T) { assert.Nil(t, dat["aps"].(map[string]interface{})["badge"]) } +// URL: https://goo.gl/5xFo3C +// Example 2 +// { +// "aps" : { +// "alert" : { +// "title" : "Game Request", +// "body" : "Bob wants to play poker", +// "action-loc-key" : "PLAY" +// }, +// "badge" : 5 +// }, +// "acme1" : "bar", +// "acme2" : [ "bang", "whiz" ] +// } +func TestAlertStringExample2ForIos(t *testing.T) { + var dat map[string]interface{} + + test := "test" + title := "Game Request" + body := "Bob wants to play poker" + actionLocKey := "PLAY" + req := PushNotification{ + ApnsID: test, + Topic: test, + Priority: "normal", + Alert: Alert{ + Title: title, + Body: body, + ActionLocKey: actionLocKey, + }, + } + + 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) + } + + assert.Equal(t, title, dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["title"]) + assert.Equal(t, body, dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["body"]) + assert.Equal(t, actionLocKey, dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["action-loc-key"]) +} + +// URL: https://goo.gl/5xFo3C +// Example 3 +// { +// "aps" : { +// "alert" : "You got your emails.", +// "badge" : 9, +// "sound" : "bingbong.aiff" +// }, +// "acme1" : "bar", +// "acme2" : 42 +// } +func TestAlertStringExample3ForIos(t *testing.T) { + var dat map[string]interface{} + + test := "test" + badge := 9 + sound := "bingbong.aiff" + req := PushNotification{ + ApnsID: test, + Topic: test, + Priority: "normal", + ContentAvailable: true, + Message: test, + Badge: &badge, + Sound: sound, + } + + 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) + } + + assert.Equal(t, sound, dat["aps"].(map[string]interface{})["sound"]) + assert.Equal(t, float64(badge), dat["aps"].(map[string]interface{})["badge"].(float64)) + assert.Equal(t, test, dat["aps"].(map[string]interface{})["alert"]) +} + func TestIOSAlertNotificationStructure(t *testing.T) { var dat map[string]interface{}