diff --git a/gorush/notification.go b/gorush/notification.go index 763a90a..f04c46d 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -302,7 +302,12 @@ func GetIOSNotification(req PushNotification) *apns.Notification { 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. if req.Badge != nil && *req.Badge >= 0 { diff --git a/gorush/notification_test.go b/gorush/notification_test.go index c1f91a5..f33b449 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -186,6 +186,36 @@ func TestSendZeroValueForBadgeKey(t *testing.T) { assert.Equal(t, expectBadge, int(badge)) } +// Silent Notification: +// The payload’s aps dictionary must include the content-available key with a value of 1. +// The payload’s 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) { var dat map[string]interface{}