Skip to content

Commit

Permalink
Support iOS 12 Critical Alerts (#113)
Browse files Browse the repository at this point in the history
* Support sound dictionaries for critical alerts
  • Loading branch information
Vidul Parthasarathy authored and sideshow committed Aug 14, 2018
1 parent 656cc74 commit 223c93f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
37 changes: 35 additions & 2 deletions payload/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type aps struct {
Category string `json:"category,omitempty"`
ContentAvailable int `json:"content-available,omitempty"`
MutableContent int `json:"mutable-content,omitempty"`
Sound string `json:"sound,omitempty"`
Sound interface{} `json:"sound,omitempty"`
ThreadID string `json:"thread-id,omitempty"`
URLArgs []string `json:"url-args,omitempty"`
}
Expand All @@ -34,6 +34,12 @@ type alert struct {
TitleLocKey string `json:"title-loc-key,omitempty"`
}

type sound struct {
Critical int `json:"critical,omitempty"`
Name string `json:"name,omitempty"`
Volume float32 `json:"volume,omitempty"`
}

// NewPayload returns a new Payload struct
func NewPayload() *Payload {
return &Payload{
Expand Down Expand Up @@ -84,7 +90,7 @@ func (p *Payload) UnsetBadge() *Payload {
// This will play a sound from the app bundle, or the default sound otherwise.
//
// {"aps":{"sound":sound}}
func (p *Payload) Sound(sound string) *Payload {
func (p *Payload) Sound(sound interface{}) *Payload {
p.aps().Sound = sound
return p
}
Expand Down Expand Up @@ -274,6 +280,26 @@ func (p *Payload) URLArgs(urlArgs []string) *Payload {
return p
}

// SoundName sets the name value on the aps sound dictionary.
// This function makes the notification a critical alert, which should be pre-approved by Apple.
// See: https://developer.apple.com/contact/request/notifications-critical-alerts-entitlement/
//
// {"aps":{"sound":{"critical":1,"name":name,"volume":1.0}}}
func (p *Payload) SoundName(name string) *Payload {
p.aps().sound().Name = name
return p
}

// SoundVolume sets the volume value on the aps sound dictionary.
// This function makes the notification a critical alert, which should be pre-approved by Apple.
// See: https://developer.apple.com/contact/request/notifications-critical-alerts-entitlement/
//
// {"aps":{"sound":{"critical":1,"name":"default","volume":volume}}}
func (p *Payload) SoundVolume(volume float32) *Payload {
p.aps().sound().Volume = volume
return p
}

// MarshalJSON returns the JSON encoded version of the Payload
func (p *Payload) MarshalJSON() ([]byte, error) {
return json.Marshal(p.content)
Expand All @@ -289,3 +315,10 @@ func (a *aps) alert() *alert {
}
return a.Alert.(*alert)
}

func (a *aps) sound() *sound {
if _, ok := a.Sound.(*sound); !ok {
a.Sound = &sound{Critical: 1, Name: "default", Volume: 1.0}
}
return a.Sound.(*sound)
}
22 changes: 22 additions & 0 deletions payload/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ func TestSound(t *testing.T) {
assert.Equal(t, `{"aps":{"sound":"Default.caf"}}`, string(b))
}

func TestSoundDictionary(t *testing.T) {
payload := NewPayload().Sound(map[string]interface{}{
"critical": 1,
"name": "default",
"volume": 0.8,
})
b, _ := json.Marshal(payload)
assert.Equal(t, `{"aps":{"sound":{"critical":1,"name":"default","volume":0.8}}}`, string(b))
}

func TestContentAvailable(t *testing.T) {
payload := NewPayload().ContentAvailable()
b, _ := json.Marshal(payload)
Expand Down Expand Up @@ -154,6 +164,18 @@ func TestURLArgs(t *testing.T) {
assert.Equal(t, `{"aps":{"url-args":["a","b"]}}`, string(b))
}

func TestSoundName(t *testing.T) {
payload := NewPayload().SoundName("test")
b, _ := json.Marshal(payload)
assert.Equal(t, `{"aps":{"sound":{"critical":1,"name":"test","volume":1}}}`, string(b))
}

func TestSoundVolume(t *testing.T) {
payload := NewPayload().SoundVolume(0.5)
b, _ := json.Marshal(payload)
assert.Equal(t, `{"aps":{"sound":{"critical":1,"name":"default","volume":0.5}}}`, string(b))
}

func TestCombined(t *testing.T) {
payload := NewPayload().Alert("hello").Badge(1).Sound("Default.caf").Custom("key", "val")
b, _ := json.Marshal(payload)
Expand Down

0 comments on commit 223c93f

Please sign in to comment.