Skip to content

Commit 78490a9

Browse files
committed
Add CollapseID & AlertSubtitle for iOS 10 support
1 parent e9ee6df commit 78490a9

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ func setHeaders(r *http.Request, n *Notification) {
125125
if n.ApnsID != "" {
126126
r.Header.Set("apns-id", n.ApnsID)
127127
}
128+
if n.CollapseID != "" {
129+
r.Header.Set("apns-collapse-id", n.CollapseID)
130+
}
128131
if n.Priority > 0 {
129132
r.Header.Set("apns-priority", fmt.Sprintf("%v", n.Priority))
130133
}

client_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ func TestDefaultHeaders(t *testing.T) {
118118
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
119119
assert.Equal(t, "application/json; charset=utf-8", r.Header.Get("Content-Type"))
120120
assert.Equal(t, "", r.Header.Get("apns-id"))
121+
assert.Equal(t, "", r.Header.Get("apns-collapse-id"))
121122
assert.Equal(t, "", r.Header.Get("apns-priority"))
122123
assert.Equal(t, "", r.Header.Get("apns-topic"))
123124
assert.Equal(t, "", r.Header.Get("apns-expiration"))
@@ -130,11 +131,13 @@ func TestDefaultHeaders(t *testing.T) {
130131
func TestHeaders(t *testing.T) {
131132
n := mockNotification()
132133
n.ApnsID = "84DB694F-464F-49BD-960A-D6DB028335C9"
134+
n.CollapseID = "game1.start.identifier"
133135
n.Topic = "com.testapp"
134136
n.Priority = 10
135137
n.Expiration = time.Now()
136138
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
137139
assert.Equal(t, n.ApnsID, r.Header.Get("apns-id"))
140+
assert.Equal(t, n.CollapseID, r.Header.Get("apns-collapse-id"))
138141
assert.Equal(t, "10", r.Header.Get("apns-priority"))
139142
assert.Equal(t, n.Topic, r.Header.Get("apns-topic"))
140143
assert.Equal(t, fmt.Sprintf("%v", n.Expiration.Unix()), r.Header.Get("apns-expiration"))

notification.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ type Notification struct {
3232
// response.
3333
ApnsID string
3434

35+
// A string which allows a notification to be replaced by a new notification
36+
// with the same CollapseID.
37+
CollapseID string
38+
3539
// A string containing hexadecimal bytes of the device token for the target device.
3640
DeviceToken string
3741

payload/builder.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type alert struct {
2828
LocArgs []string `json:"loc-args,omitempty"`
2929
LocKey string `json:"loc-key,omitempty"`
3030
Title string `json:"title,omitempty"`
31+
Subtitle string `json:"subtitle,omitempty"`
3132
TitleLocArgs []string `json:"title-loc-args,omitempty"`
3233
TitleLocKey string `json:"title-loc-key,omitempty"`
3334
}
@@ -100,6 +101,7 @@ func (p *Payload) ContentAvailable() *Payload {
100101
// MutableContent sets the aps mutable-content on the payload to 1.
101102
// This will indicate to the to the system to call your Notification Service
102103
// extension to mutate or replace the notification's content.
104+
//
103105
// {"aps":{"mutable-content":1}}
104106
func (p *Payload) MutableContent() *Payload {
105107
p.aps().MutableContent = 1
@@ -151,6 +153,16 @@ func (p *Payload) AlertTitleLocArgs(args []string) *Payload {
151153
return p
152154
}
153155

156+
// AlertSubtitle sets the aps alert subtitle on the payload.
157+
// This will display a short string describing the purpose of the notification.
158+
// Apple Watch & Safari display this string as part of the notification interface.
159+
//
160+
// {"aps":{"subtitle":"subtitle"}}
161+
func (p *Payload) AlertSubtitle(subtitle string) *Payload {
162+
p.aps().alert().Subtitle = subtitle
163+
return p
164+
}
165+
154166
// AlertBody sets the aps alert body on the payload.
155167
// This is the text of the alert message.
156168
//

payload/builder_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ func TestAlertTitleLocArgs(t *testing.T) {
9494
assert.Equal(t, `{"aps":{"alert":{"title-loc-args":["Jenna","Frank"]}}}`, string(b))
9595
}
9696

97+
func TestAlertSubtitle(t *testing.T) {
98+
payload := NewPayload().AlertSubtitle("hello")
99+
b, _ := json.Marshal(payload)
100+
assert.Equal(t, `{"aps":{"alert":{"subtitle":"hello"}}}`, string(b))
101+
}
102+
97103
func TestAlertBody(t *testing.T) {
98104
payload := NewPayload().AlertBody("body")
99105
b, _ := json.Marshal(payload)

0 commit comments

Comments
 (0)