Skip to content

Commit f466a5d

Browse files
authored
Fixing how CriticalSound type is serialized for APNs (#205)
* Serializing sound.critical field as a number * Validation for Volume
1 parent 3f70ab0 commit f466a5d

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

messaging/messaging.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,24 @@ func (a *Aps) MarshalJSON() ([]byte, error) {
400400

401401
// CriticalSound is the sound payload that can be included in an Aps.
402402
type CriticalSound struct {
403-
Critical bool `json:"critical,omitempty"`
404-
Name string `json:"name,omitempty"`
405-
Volume float64 `json:"volume,omitempty"`
403+
Critical bool
404+
Name string
405+
Volume float64
406+
}
407+
408+
// MarshalJSON marshals a CriticalSound into JSON (for internal use only).
409+
func (cs *CriticalSound) MarshalJSON() ([]byte, error) {
410+
m := make(map[string]interface{})
411+
if cs.Critical {
412+
m["critical"] = 1
413+
}
414+
if cs.Name != "" {
415+
m["name"] = cs.Name
416+
}
417+
if cs.Volume != 0 {
418+
m["volume"] = cs.Volume
419+
}
420+
return json.Marshal(m)
406421
}
407422

408423
// ApsAlert is the alert payload that can be included in an Aps.

messaging/messaging_test.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ var validMessages = []struct {
368368
"badge": float64(badge),
369369
"category": "c",
370370
"sound": map[string]interface{}{
371-
"critical": true,
371+
"critical": float64(1),
372372
"name": "n",
373373
"volume": float64(0.7),
374374
},
@@ -651,6 +651,57 @@ var invalidMessages = []struct {
651651
},
652652
want: "locKey is required when specifying locArgs",
653653
},
654+
{
655+
name: "MultipleSoundSpecifications",
656+
req: &Message{
657+
APNS: &APNSConfig{
658+
Payload: &APNSPayload{
659+
Aps: &Aps{
660+
Sound: "s",
661+
CriticalSound: &CriticalSound{
662+
Name: "s",
663+
},
664+
},
665+
},
666+
},
667+
Topic: "topic",
668+
},
669+
want: "multiple sound specifications",
670+
},
671+
{
672+
name: "VolumeTooLow",
673+
req: &Message{
674+
APNS: &APNSConfig{
675+
Payload: &APNSPayload{
676+
Aps: &Aps{
677+
CriticalSound: &CriticalSound{
678+
Name: "s",
679+
Volume: -0.1,
680+
},
681+
},
682+
},
683+
},
684+
Topic: "topic",
685+
},
686+
want: "critical sound volume must be in the interval [0, 1]",
687+
},
688+
{
689+
name: "VolumeTooHigh",
690+
req: &Message{
691+
APNS: &APNSConfig{
692+
Payload: &APNSPayload{
693+
Aps: &Aps{
694+
CriticalSound: &CriticalSound{
695+
Name: "s",
696+
Volume: 1.1,
697+
},
698+
},
699+
},
700+
},
701+
Topic: "topic",
702+
},
703+
want: "critical sound volume must be in the interval [0, 1]",
704+
},
654705
{
655706
name: "InvalidWebpushNotificationDirection",
656707
req: &Message{

messaging/messaging_utils.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,13 @@ 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")
111+
if aps.CriticalSound != nil {
112+
if aps.Sound != "" {
113+
return fmt.Errorf("multiple sound specifications")
114+
}
115+
if aps.CriticalSound.Volume < 0 || aps.CriticalSound.Volume > 1 {
116+
return fmt.Errorf("critical sound volume must be in the interval [0, 1]")
117+
}
113118
}
114119
m := aps.standardFields()
115120
for k := range aps.CustomData {

0 commit comments

Comments
 (0)