Skip to content

Commit

Permalink
remove required field: message
Browse files Browse the repository at this point in the history
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 <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Dec 24, 2016
1 parent e2cc0a4 commit 1707f39
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
6 changes: 5 additions & 1 deletion gorush/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down
89 changes: 89 additions & 0 deletions gorush/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down

0 comments on commit 1707f39

Please sign in to comment.