feat(ios): Support iOS 12 Critical Alerts (#367)

fix https://github.com/appleboy/gorush/issues/366
This commit is contained in:
Bo-Yi Wu
2018-08-28 11:02:13 +08:00
committed by GitHub
parent 880752d88c
commit 0c89fd1d81
11 changed files with 140 additions and 43 deletions

View File

@@ -50,16 +50,16 @@ type RequestPush struct {
// PushNotification is single notification request
type PushNotification struct {
// Common
Tokens []string `json:"tokens" binding:"required"`
Platform int `json:"platform" binding:"required"`
Message string `json:"message,omitempty"`
Title string `json:"title,omitempty"`
Priority string `json:"priority,omitempty"`
ContentAvailable bool `json:"content_available,omitempty"`
MutableContent bool `json:"mutable_content,omitempty"`
Sound string `json:"sound,omitempty"`
Data D `json:"data,omitempty"`
Retry int `json:"retry,omitempty"`
Tokens []string `json:"tokens" binding:"required"`
Platform int `json:"platform" binding:"required"`
Message string `json:"message,omitempty"`
Title string `json:"title,omitempty"`
Priority string `json:"priority,omitempty"`
ContentAvailable bool `json:"content_available,omitempty"`
MutableContent bool `json:"mutable_content,omitempty"`
Sound interface{} `json:"sound,omitempty"`
Data D `json:"data,omitempty"`
Retry int `json:"retry,omitempty"`
wg *sync.WaitGroup
log *[]LogPushEntry
@@ -86,6 +86,8 @@ type PushNotification struct {
Alert Alert `json:"alert,omitempty"`
Production bool `json:"production,omitempty"`
Development bool `json:"development,omitempty"`
SoundName string `json:"name,omitempty"`
SoundVolume float32 `json:"volume,omitempty"`
}
// WaitDone decrements the WaitGroup counter.

View File

@@ -14,6 +14,13 @@ import (
"github.com/sideshow/apns2/token"
)
// Sound sets the aps sound on the payload.
type Sound struct {
Critical int `json:"critical,omitempty"`
Name string `json:"name,omitempty"`
Volume float32 `json:"volume,omitempty"`
}
// InitAPNSClient use for initialize APNs Client.
func InitAPNSClient() error {
if PushConf.Ios.Enabled {
@@ -183,8 +190,16 @@ func GetIOSNotification(req PushNotification) *apns2.Notification {
payload.MutableContent()
}
if len(req.Sound) > 0 {
payload.Sound(req.Sound)
if _, ok := req.Sound.(Sound); ok {
payload.Sound(&req.Sound)
}
if len(req.SoundName) > 0 {
payload.SoundName(req.SoundName)
}
if req.SoundVolume > 0 {
payload.SoundVolume(req.SoundVolume)
}
if req.ContentAvailable {

View File

@@ -56,13 +56,17 @@ func TestIOSNotificationStructure(t *testing.T) {
expectBadge := 0
message := "Welcome notification Server"
req := PushNotification{
ApnsID: test,
Topic: test,
Expiration: time.Now().Unix(),
Priority: "normal",
Message: message,
Badge: &expectBadge,
Sound: test,
ApnsID: test,
Topic: test,
Expiration: time.Now().Unix(),
Priority: "normal",
Message: message,
Badge: &expectBadge,
Sound: Sound{
Critical: 1,
Name: test,
Volume: 1.0,
},
ContentAvailable: true,
Data: D{
"key1": "test",
@@ -78,13 +82,14 @@ func TestIOSNotificationStructure(t *testing.T) {
data := []byte(string(dump))
if err := json.Unmarshal(data, &dat); err != nil {
log.Println(err)
panic(err)
}
alert, _ := jsonparser.GetString(data, "aps", "alert")
badge, _ := jsonparser.GetInt(data, "aps", "badge")
sound, _ := jsonparser.GetString(data, "aps", "sound")
soundName, _ := jsonparser.GetString(data, "aps", "sound", "name")
soundCritical, _ := jsonparser.GetInt(data, "aps", "sound", "critical")
soundVolume, _ := jsonparser.GetFloat(data, "aps", "sound", "volume")
contentAvailable, _ := jsonparser.GetInt(data, "aps", "content-available")
category, _ := jsonparser.GetString(data, "aps", "category")
key1 := dat["key1"].(interface{})
@@ -99,7 +104,9 @@ func TestIOSNotificationStructure(t *testing.T) {
assert.Equal(t, message, alert)
assert.Equal(t, expectBadge, int(badge))
assert.Equal(t, expectBadge, *req.Badge)
assert.Equal(t, test, sound)
assert.Equal(t, test, soundName)
assert.Equal(t, 1.0, soundVolume)
assert.Equal(t, int64(1), soundCritical)
assert.Equal(t, 1, int(contentAvailable))
assert.Equal(t, "test", key1)
assert.Equal(t, 2, int(key2.(float64)))
@@ -108,6 +115,48 @@ func TestIOSNotificationStructure(t *testing.T) {
assert.Contains(t, urlArgs, "b")
}
func TestIOSSoundAndVolume(t *testing.T) {
var dat map[string]interface{}
test := "test"
message := "Welcome notification Server"
req := PushNotification{
ApnsID: test,
Topic: test,
Priority: "normal",
Message: message,
Sound: Sound{
Critical: 2,
Name: test,
Volume: 1.0,
},
SoundName: "foo",
SoundVolume: 2.0,
}
notification := GetIOSNotification(req)
dump, _ := json.Marshal(notification.Payload)
data := []byte(string(dump))
if err := json.Unmarshal(data, &dat); err != nil {
panic(err)
}
alert, _ := jsonparser.GetString(data, "aps", "alert")
soundName, _ := jsonparser.GetString(data, "aps", "sound", "name")
soundCritical, _ := jsonparser.GetInt(data, "aps", "sound", "critical")
soundVolume, _ := jsonparser.GetFloat(data, "aps", "sound", "volume")
assert.Equal(t, test, notification.ApnsID)
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, int64(1), soundCritical)
}
// Silent Notification which payloads aps dictionary must not contain the alert, sound, or badge keys.
// ref: https://goo.gl/m9xyqG
func TestSendZeroValueForBadgeKey(t *testing.T) {

View File

@@ -70,8 +70,8 @@ func GetAndroidNotification(req PushNotification) *fcm.Message {
notification.Notification.Title = req.Title
}
if len(req.Sound) > 0 {
notification.Notification.Sound = req.Sound
if v, ok := req.Sound.(string); ok && len(v) > 0 {
notification.Notification.Sound = v
}
return notification