From 1f2da28f4beea42cb1d00b7f46c5e686a42a2799 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 8 Dec 2016 10:13:32 +0800 Subject: [PATCH] fixed #147 support zero value of badge. Signed-off-by: Bo-Yi Wu --- gorush/notification.go | 6 ++-- gorush/notification_test.go | 72 +++++++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/gorush/notification.go b/gorush/notification.go index 9766655..763a90a 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -76,7 +76,7 @@ type PushNotification struct { Expiration int64 `json:"expiration,omitempty"` ApnsID string `json:"apns_id,omitempty"` Topic string `json:"topic,omitempty"` - Badge int `json:"badge,omitempty"` + Badge *int `json:"badge,omitempty"` Category string `json:"category,omitempty"` URLArgs []string `json:"url-args,omitempty"` Alert Alert `json:"alert,omitempty"` @@ -305,8 +305,8 @@ func GetIOSNotification(req PushNotification) *apns.Notification { payload := payload.NewPayload().Alert(req.Message) // zero value for clear the badge on the app icon. - if req.Badge >= 0 { - payload.Badge(req.Badge) + if req.Badge != nil && *req.Badge >= 0 { + payload.Badge(*req.Badge) } if len(req.Sound) > 0 { diff --git a/gorush/notification_test.go b/gorush/notification_test.go index 918979d..c1f91a5 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -65,6 +65,7 @@ func TestIOSNotificationStructure(t *testing.T) { var unix = time.Now().Unix() test := "test" + expectBadge := 0 message := "Welcome notification Server" req := PushNotification{ ApnsID: test, @@ -72,7 +73,7 @@ func TestIOSNotificationStructure(t *testing.T) { Expiration: time.Now().Unix(), Priority: "normal", Message: message, - Badge: 0, + Badge: &expectBadge, Sound: test, ContentAvailable: true, Data: D{ @@ -108,7 +109,8 @@ func TestIOSNotificationStructure(t *testing.T) { assert.Equal(t, unix, notification.Expiration.Unix()) assert.Equal(t, ApnsPriorityLow, notification.Priority) assert.Equal(t, message, alert) - assert.Equal(t, 0, int(badge)) + assert.Equal(t, expectBadge, int(badge)) + assert.Equal(t, expectBadge, *req.Badge) assert.Equal(t, test, sound) assert.Equal(t, 1, int(contentAvailable)) assert.Equal(t, "test", key1) @@ -118,6 +120,72 @@ func TestIOSNotificationStructure(t *testing.T) { assert.Contains(t, urlArgs, "b") } +// Silent Notification which payload’s aps dictionary must not contain the alert, sound, or badge keys. +// ref: https://goo.gl/m9xyqG +func TestSendZeroValueForBadgeKey(t *testing.T) { + var dat map[string]interface{} + + test := "test" + message := "Welcome notification Server" + req := PushNotification{ + ApnsID: test, + Topic: test, + Priority: "normal", + Message: message, + Sound: test, + 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) + } + + 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") + + if req.Badge != nil { + t.Errorf("req.Badge must be nil") + } + + 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, 0, int(badge)) + assert.Equal(t, test, sound) + assert.Equal(t, 1, int(contentAvailable)) + + // Add Bage + expectBadge := 10 + req.Badge = &expectBadge + + 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) + } + + if req.Badge == nil { + t.Errorf("req.Badge must be equal %d", *req.Badge) + } + + badge, _ = jsonparser.GetInt(data, "aps", "badge") + assert.Equal(t, expectBadge, *req.Badge) + assert.Equal(t, expectBadge, int(badge)) +} + func TestIOSAlertNotificationStructure(t *testing.T) { var dat map[string]interface{}