File tree Expand file tree Collapse file tree 3 files changed +77
-6
lines changed Expand file tree Collapse file tree 3 files changed +77
-6
lines changed Original file line number Diff line number Diff line change @@ -400,9 +400,24 @@ func (a *Aps) MarshalJSON() ([]byte, error) {
400
400
401
401
// CriticalSound is the sound payload that can be included in an Aps.
402
402
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 )
406
421
}
407
422
408
423
// ApsAlert is the alert payload that can be included in an Aps.
Original file line number Diff line number Diff line change @@ -368,7 +368,7 @@ var validMessages = []struct {
368
368
"badge" : float64 (badge ),
369
369
"category" : "c" ,
370
370
"sound" : map [string ]interface {}{
371
- "critical" : true ,
371
+ "critical" : float64 ( 1 ) ,
372
372
"name" : "n" ,
373
373
"volume" : float64 (0.7 ),
374
374
},
@@ -651,6 +651,57 @@ var invalidMessages = []struct {
651
651
},
652
652
want : "locKey is required when specifying locArgs" ,
653
653
},
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
+ },
654
705
{
655
706
name : "InvalidWebpushNotificationDirection" ,
656
707
req : & Message {
Original file line number Diff line number Diff line change @@ -108,8 +108,13 @@ func validateAps(aps *Aps) error {
108
108
if aps .Alert != nil && aps .AlertString != "" {
109
109
return fmt .Errorf ("multiple alert specifications" )
110
110
}
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
+ }
113
118
}
114
119
m := aps .standardFields ()
115
120
for k := range aps .CustomData {
You can’t perform that action at this time.
0 commit comments