fix(ios): mapstructure for sound field. (#385)

This commit is contained in:
Bo-Yi Wu
2018-11-20 17:01:35 +08:00
committed by GitHub
parent 375eec1d90
commit a7a451e363
10 changed files with 558 additions and 105 deletions

View File

@@ -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)
}

View File

@@ -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) {