ignore alert object if message is empty.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-12-09 22:56:03 +08:00
parent 20c8340875
commit 65d486b65b
2 changed files with 36 additions and 1 deletions

View File

@ -302,7 +302,12 @@ func GetIOSNotification(req PushNotification) *apns.Notification {
notification.Priority = apns.PriorityLow notification.Priority = apns.PriorityLow
} }
payload := payload.NewPayload().Alert(req.Message) payload := payload.NewPayload()
// add alert object if message length > 0
if len(req.Message) > 0 {
payload.Alert(req.Message)
}
// zero value for clear the badge on the app icon. // zero value for clear the badge on the app icon.
if req.Badge != nil && *req.Badge >= 0 { if req.Badge != nil && *req.Badge >= 0 {

View File

@ -186,6 +186,36 @@ func TestSendZeroValueForBadgeKey(t *testing.T) {
assert.Equal(t, expectBadge, int(badge)) assert.Equal(t, expectBadge, int(badge))
} }
// Silent Notification:
// The payloads aps dictionary must include the content-available key with a value of 1.
// The payloads aps dictionary must not contain the alert, sound, or badge keys.
// ref: https://goo.gl/m9xyqG
func TestCheckSilentNotification(t *testing.T) {
var dat map[string]interface{}
test := "test"
req := PushNotification{
ApnsID: test,
Topic: test,
Priority: "normal",
ContentAvailable: true,
}
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.Nil(t, dat["aps"].(map[string]interface{})["alert"])
assert.Nil(t, dat["aps"].(map[string]interface{})["sound"])
assert.Nil(t, dat["aps"].(map[string]interface{})["badge"])
}
func TestIOSAlertNotificationStructure(t *testing.T) { func TestIOSAlertNotificationStructure(t *testing.T) {
var dat map[string]interface{} var dat map[string]interface{}