fix(ios): mapstructure for sound field. (#385)
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/sideshow/apns2"
|
||||
"github.com/sideshow/apns2/certificate"
|
||||
"github.com/sideshow/apns2/payload"
|
||||
@@ -197,7 +198,13 @@ func GetIOSNotification(req PushNotification) *apns2.Notification {
|
||||
payload.MutableContent()
|
||||
}
|
||||
|
||||
if _, ok := req.Sound.(Sound); ok {
|
||||
switch req.Sound.(type) {
|
||||
// from http request binding
|
||||
case map[string]interface{}:
|
||||
result := &Sound{}
|
||||
_ = mapstructure.Decode(req.Sound, &result)
|
||||
payload.Sound(result)
|
||||
case Sound:
|
||||
payload.Sound(&req.Sound)
|
||||
}
|
||||
|
||||
|
||||
@@ -126,12 +126,10 @@ func TestIOSSoundAndVolume(t *testing.T) {
|
||||
Priority: "normal",
|
||||
Message: message,
|
||||
Sound: Sound{
|
||||
Critical: 2,
|
||||
Critical: 3,
|
||||
Name: test,
|
||||
Volume: 1.0,
|
||||
Volume: 4.5,
|
||||
},
|
||||
SoundName: "foo",
|
||||
SoundVolume: 2.0,
|
||||
}
|
||||
|
||||
notification := GetIOSNotification(req)
|
||||
@@ -152,9 +150,53 @@ func TestIOSSoundAndVolume(t *testing.T) {
|
||||
assert.Equal(t, test, notification.Topic)
|
||||
assert.Equal(t, ApnsPriorityLow, notification.Priority)
|
||||
assert.Equal(t, message, alert)
|
||||
assert.Equal(t, "foo", soundName)
|
||||
assert.Equal(t, 2.0, soundVolume)
|
||||
assert.Equal(t, test, soundName)
|
||||
assert.Equal(t, 4.5, soundVolume)
|
||||
assert.Equal(t, int64(3), soundCritical)
|
||||
|
||||
req.SoundName = "foobar"
|
||||
req.SoundVolume = 5.5
|
||||
notification = GetIOSNotification(req)
|
||||
dump, _ = json.Marshal(notification.Payload)
|
||||
data = []byte(string(dump))
|
||||
|
||||
if err := json.Unmarshal(data, &dat); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
soundName, _ = jsonparser.GetString(data, "aps", "sound", "name")
|
||||
soundVolume, _ = jsonparser.GetFloat(data, "aps", "sound", "volume")
|
||||
soundCritical, _ = jsonparser.GetInt(data, "aps", "sound", "critical")
|
||||
assert.Equal(t, 5.5, soundVolume)
|
||||
assert.Equal(t, int64(1), soundCritical)
|
||||
assert.Equal(t, "foobar", soundName)
|
||||
|
||||
req = PushNotification{
|
||||
ApnsID: test,
|
||||
Topic: test,
|
||||
Priority: "normal",
|
||||
Message: message,
|
||||
Sound: map[string]interface{}{
|
||||
"critical": 3,
|
||||
"name": "test",
|
||||
"volume": 4.5,
|
||||
},
|
||||
}
|
||||
|
||||
notification = GetIOSNotification(req)
|
||||
dump, _ = json.Marshal(notification.Payload)
|
||||
data = []byte(string(dump))
|
||||
|
||||
if err := json.Unmarshal(data, &dat); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
soundName, _ = jsonparser.GetString(data, "aps", "sound", "name")
|
||||
soundVolume, _ = jsonparser.GetFloat(data, "aps", "sound", "volume")
|
||||
soundCritical, _ = jsonparser.GetInt(data, "aps", "sound", "critical")
|
||||
assert.Equal(t, 4.5, soundVolume)
|
||||
assert.Equal(t, int64(3), soundCritical)
|
||||
assert.Equal(t, "test", soundName)
|
||||
}
|
||||
|
||||
func TestIOSSummaryArg(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user