Skip to content

Commit 3f70ab0

Browse files
chemidyhiranya911
authored andcommitted
add CriticalSound support for APS (#196)
* add CriticalSound support for APS * change type of critical from int to bool
1 parent d661738 commit 3f70ab0

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# Unreleased
22

3+
- [added] `messaging.Aps` type now supports critical sound in its payload.
4+
35
# v3.5.0
46

57
- [added] `messaging.AndroidNotification` type now supports `channel_id`.
68
- [dropped] Dropped support for Go 1.8 and earlier.
79
- [fixed] Fixing error handling in FCM. The SDK now checks the key
810
`type.googleapis.com/google.firebase.fcm.v1.FcmError` to set error code.
911
- [added] `messaging.ApsAlert` type now supports subtitle in its payload.
12+
- [added] `messaging.WebpushConfig` type now supports fcmOptions in its payload.
1013

1114
# v3.4.0
1215

messaging/messaging.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ type Aps struct {
350350
Alert *ApsAlert
351351
Badge *int
352352
Sound string
353+
CriticalSound *CriticalSound
353354
ContentAvailable bool
354355
MutableContent bool
355356
Category string
@@ -374,7 +375,9 @@ func (a *Aps) standardFields() map[string]interface{} {
374375
if a.Badge != nil {
375376
m["badge"] = *a.Badge
376377
}
377-
if a.Sound != "" {
378+
if a.CriticalSound != nil {
379+
m["sound"] = a.CriticalSound
380+
} else if a.Sound != "" {
378381
m["sound"] = a.Sound
379382
}
380383
if a.Category != "" {
@@ -395,6 +398,13 @@ func (a *Aps) MarshalJSON() ([]byte, error) {
395398
return json.Marshal(m)
396399
}
397400

401+
// CriticalSound is the sound payload that can be included in an Aps.
402+
type CriticalSound struct {
403+
Critical bool `json:"critical,omitempty"`
404+
Name string `json:"name,omitempty"`
405+
Volume float64 `json:"volume,omitempty"`
406+
}
407+
398408
// ApsAlert is the alert payload that can be included in an Aps.
399409
//
400410
// See https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html

messaging/messaging_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,60 @@ var validMessages = []struct {
329329
"topic": "test-topic",
330330
},
331331
},
332+
{
333+
name: "APNSAlertCrticalSound",
334+
req: &Message{
335+
APNS: &APNSConfig{
336+
Headers: map[string]string{
337+
"h1": "v1",
338+
"h2": "v2",
339+
},
340+
Payload: &APNSPayload{
341+
Aps: &Aps{
342+
AlertString: "a",
343+
Badge: &badge,
344+
Category: "c",
345+
CriticalSound: &CriticalSound{
346+
Critical: true,
347+
Name: "n",
348+
Volume: 0.7,
349+
},
350+
ThreadID: "t",
351+
ContentAvailable: true,
352+
MutableContent: true,
353+
},
354+
CustomData: map[string]interface{}{
355+
"k1": "v1",
356+
"k2": true,
357+
},
358+
},
359+
},
360+
Topic: "test-topic",
361+
},
362+
want: map[string]interface{}{
363+
"apns": map[string]interface{}{
364+
"headers": map[string]interface{}{"h1": "v1", "h2": "v2"},
365+
"payload": map[string]interface{}{
366+
"aps": map[string]interface{}{
367+
"alert": "a",
368+
"badge": float64(badge),
369+
"category": "c",
370+
"sound": map[string]interface{}{
371+
"critical": true,
372+
"name": "n",
373+
"volume": float64(0.7),
374+
},
375+
"thread-id": "t",
376+
"content-available": float64(1),
377+
"mutable-content": float64(1),
378+
},
379+
"k1": "v1",
380+
"k2": true,
381+
},
382+
},
383+
"topic": "test-topic",
384+
},
385+
},
332386
{
333387
name: "APNSBadgeZero",
334388
req: &Message{

messaging/messaging_utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ func validateAps(aps *Aps) error {
108108
if aps.Alert != nil && aps.AlertString != "" {
109109
return fmt.Errorf("multiple alert specifications")
110110
}
111+
if aps.CriticalSound != nil && aps.Sound != "" {
112+
return fmt.Errorf("multiple sound specifications")
113+
}
111114
m := aps.standardFields()
112115
for k := range aps.CustomData {
113116
if _, contains := m[k]; contains {

0 commit comments

Comments
 (0)