Skip to content

Commit d1dcba2

Browse files
committed
Badge zero and empty are different
I don't like this particular fix because it changes the API.
1 parent 72d90a3 commit d1dcba2

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

notification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Alert struct {
4747

4848
type APS struct {
4949
Alert Alert `json:"alert,omitempty"`
50-
Badge int `json:"badge,omitempty"`
50+
Badge *int `json:"badge,omitempty"`
5151
Sound string `json:"sound,omitempty"`
5252
ContentAvailable int `json:"content-available,omitempty"`
5353
}

notification_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,30 @@ var _ = Describe("Notifications", func() {
113113
})
114114
})
115115

116+
Describe("APS", func() {
117+
Context("badge with a zero (clears notifications)", func() {
118+
It("should contain zero", func() {
119+
zero := 0
120+
a := apns.APS{Badge: &zero}
121+
122+
j, err := json.Marshal(a)
123+
124+
Expect(err).To(BeNil())
125+
Expect(j).To(Equal([]byte(`{"alert":{},"badge":0}`)))
126+
})
127+
})
128+
Context("no badge specified (do nothing)", func() {
129+
It("should omit the badge field", func() {
130+
a := apns.APS{}
131+
132+
j, err := json.Marshal(a)
133+
134+
Expect(err).To(BeNil())
135+
Expect(j).To(Equal([]byte(`{"alert":{}}`)))
136+
})
137+
})
138+
})
139+
116140
Describe("Notification", func() {
117141
Describe("#ToBinary", func() {
118142
Context("invalid token format", func() {

0 commit comments

Comments
 (0)