Skip to content

Commit 223c93f

Browse files
Vidul Parthasarathysideshow
authored andcommitted
Support iOS 12 Critical Alerts (#113)
* Support sound dictionaries for critical alerts
1 parent 656cc74 commit 223c93f

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

payload/builder.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type aps struct {
1616
Category string `json:"category,omitempty"`
1717
ContentAvailable int `json:"content-available,omitempty"`
1818
MutableContent int `json:"mutable-content,omitempty"`
19-
Sound string `json:"sound,omitempty"`
19+
Sound interface{} `json:"sound,omitempty"`
2020
ThreadID string `json:"thread-id,omitempty"`
2121
URLArgs []string `json:"url-args,omitempty"`
2222
}
@@ -34,6 +34,12 @@ type alert struct {
3434
TitleLocKey string `json:"title-loc-key,omitempty"`
3535
}
3636

37+
type sound struct {
38+
Critical int `json:"critical,omitempty"`
39+
Name string `json:"name,omitempty"`
40+
Volume float32 `json:"volume,omitempty"`
41+
}
42+
3743
// NewPayload returns a new Payload struct
3844
func NewPayload() *Payload {
3945
return &Payload{
@@ -84,7 +90,7 @@ func (p *Payload) UnsetBadge() *Payload {
8490
// This will play a sound from the app bundle, or the default sound otherwise.
8591
//
8692
// {"aps":{"sound":sound}}
87-
func (p *Payload) Sound(sound string) *Payload {
93+
func (p *Payload) Sound(sound interface{}) *Payload {
8894
p.aps().Sound = sound
8995
return p
9096
}
@@ -274,6 +280,26 @@ func (p *Payload) URLArgs(urlArgs []string) *Payload {
274280
return p
275281
}
276282

283+
// SoundName sets the name value on the aps sound dictionary.
284+
// This function makes the notification a critical alert, which should be pre-approved by Apple.
285+
// See: https://developer.apple.com/contact/request/notifications-critical-alerts-entitlement/
286+
//
287+
// {"aps":{"sound":{"critical":1,"name":name,"volume":1.0}}}
288+
func (p *Payload) SoundName(name string) *Payload {
289+
p.aps().sound().Name = name
290+
return p
291+
}
292+
293+
// SoundVolume sets the volume value on the aps sound dictionary.
294+
// This function makes the notification a critical alert, which should be pre-approved by Apple.
295+
// See: https://developer.apple.com/contact/request/notifications-critical-alerts-entitlement/
296+
//
297+
// {"aps":{"sound":{"critical":1,"name":"default","volume":volume}}}
298+
func (p *Payload) SoundVolume(volume float32) *Payload {
299+
p.aps().sound().Volume = volume
300+
return p
301+
}
302+
277303
// MarshalJSON returns the JSON encoded version of the Payload
278304
func (p *Payload) MarshalJSON() ([]byte, error) {
279305
return json.Marshal(p.content)
@@ -289,3 +315,10 @@ func (a *aps) alert() *alert {
289315
}
290316
return a.Alert.(*alert)
291317
}
318+
319+
func (a *aps) sound() *sound {
320+
if _, ok := a.Sound.(*sound); !ok {
321+
a.Sound = &sound{Critical: 1, Name: "default", Volume: 1.0}
322+
}
323+
return a.Sound.(*sound)
324+
}

payload/builder_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ func TestSound(t *testing.T) {
4444
assert.Equal(t, `{"aps":{"sound":"Default.caf"}}`, string(b))
4545
}
4646

47+
func TestSoundDictionary(t *testing.T) {
48+
payload := NewPayload().Sound(map[string]interface{}{
49+
"critical": 1,
50+
"name": "default",
51+
"volume": 0.8,
52+
})
53+
b, _ := json.Marshal(payload)
54+
assert.Equal(t, `{"aps":{"sound":{"critical":1,"name":"default","volume":0.8}}}`, string(b))
55+
}
56+
4757
func TestContentAvailable(t *testing.T) {
4858
payload := NewPayload().ContentAvailable()
4959
b, _ := json.Marshal(payload)
@@ -154,6 +164,18 @@ func TestURLArgs(t *testing.T) {
154164
assert.Equal(t, `{"aps":{"url-args":["a","b"]}}`, string(b))
155165
}
156166

167+
func TestSoundName(t *testing.T) {
168+
payload := NewPayload().SoundName("test")
169+
b, _ := json.Marshal(payload)
170+
assert.Equal(t, `{"aps":{"sound":{"critical":1,"name":"test","volume":1}}}`, string(b))
171+
}
172+
173+
func TestSoundVolume(t *testing.T) {
174+
payload := NewPayload().SoundVolume(0.5)
175+
b, _ := json.Marshal(payload)
176+
assert.Equal(t, `{"aps":{"sound":{"critical":1,"name":"default","volume":0.5}}}`, string(b))
177+
}
178+
157179
func TestCombined(t *testing.T) {
158180
payload := NewPayload().Alert("hello").Badge(1).Sound("Default.caf").Custom("key", "val")
159181
b, _ := json.Marshal(payload)

0 commit comments

Comments
 (0)