fixed #147 support zero value of badge.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-12-08 10:13:32 +08:00
parent e690f81a3b
commit 1f2da28f4b
2 changed files with 73 additions and 5 deletions

View File

@ -76,7 +76,7 @@ type PushNotification struct {
Expiration int64 `json:"expiration,omitempty"` Expiration int64 `json:"expiration,omitempty"`
ApnsID string `json:"apns_id,omitempty"` ApnsID string `json:"apns_id,omitempty"`
Topic string `json:"topic,omitempty"` Topic string `json:"topic,omitempty"`
Badge int `json:"badge,omitempty"` Badge *int `json:"badge,omitempty"`
Category string `json:"category,omitempty"` Category string `json:"category,omitempty"`
URLArgs []string `json:"url-args,omitempty"` URLArgs []string `json:"url-args,omitempty"`
Alert Alert `json:"alert,omitempty"` Alert Alert `json:"alert,omitempty"`
@ -305,8 +305,8 @@ func GetIOSNotification(req PushNotification) *apns.Notification {
payload := payload.NewPayload().Alert(req.Message) payload := payload.NewPayload().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 >= 0 { if req.Badge != nil && *req.Badge >= 0 {
payload.Badge(req.Badge) payload.Badge(*req.Badge)
} }
if len(req.Sound) > 0 { if len(req.Sound) > 0 {

View File

@ -65,6 +65,7 @@ func TestIOSNotificationStructure(t *testing.T) {
var unix = time.Now().Unix() var unix = time.Now().Unix()
test := "test" test := "test"
expectBadge := 0
message := "Welcome notification Server" message := "Welcome notification Server"
req := PushNotification{ req := PushNotification{
ApnsID: test, ApnsID: test,
@ -72,7 +73,7 @@ func TestIOSNotificationStructure(t *testing.T) {
Expiration: time.Now().Unix(), Expiration: time.Now().Unix(),
Priority: "normal", Priority: "normal",
Message: message, Message: message,
Badge: 0, Badge: &expectBadge,
Sound: test, Sound: test,
ContentAvailable: true, ContentAvailable: true,
Data: D{ Data: D{
@ -108,7 +109,8 @@ func TestIOSNotificationStructure(t *testing.T) {
assert.Equal(t, unix, notification.Expiration.Unix()) assert.Equal(t, unix, notification.Expiration.Unix())
assert.Equal(t, ApnsPriorityLow, notification.Priority) assert.Equal(t, ApnsPriorityLow, notification.Priority)
assert.Equal(t, message, alert) 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, test, sound)
assert.Equal(t, 1, int(contentAvailable)) assert.Equal(t, 1, int(contentAvailable))
assert.Equal(t, "test", key1) assert.Equal(t, "test", key1)
@ -118,6 +120,72 @@ func TestIOSNotificationStructure(t *testing.T) {
assert.Contains(t, urlArgs, "b") assert.Contains(t, urlArgs, "b")
} }
// Silent Notification which payloads 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) { func TestIOSAlertNotificationStructure(t *testing.T) {
var dat map[string]interface{} var dat map[string]interface{}