replace Extend with data field.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
@@ -10,10 +10,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type ExtendJSON struct {
|
||||
Key string `json:"key"`
|
||||
Value string `json:"val"`
|
||||
}
|
||||
type D map[string]interface{}
|
||||
|
||||
const (
|
||||
// PriorityLow will tell APNs to send the push message at a time that takes
|
||||
@@ -47,14 +44,14 @@ type RequestPush struct {
|
||||
|
||||
type PushNotification struct {
|
||||
// Common
|
||||
Tokens []string `json:"tokens" binding:"required"`
|
||||
Platform int `json:"platform" binding:"required"`
|
||||
Message string `json:"message" binding:"required"`
|
||||
Title string `json:"title,omitempty"`
|
||||
Priority string `json:"priority,omitempty"`
|
||||
ContentAvailable bool `json:"content_available,omitempty"`
|
||||
Sound string `json:"sound,omitempty"`
|
||||
Extend []ExtendJSON `json:"extend,omitempty"`
|
||||
Tokens []string `json:"tokens" binding:"required"`
|
||||
Platform int `json:"platform" binding:"required"`
|
||||
Message string `json:"message" binding:"required"`
|
||||
Title string `json:"title,omitempty"`
|
||||
Priority string `json:"priority,omitempty"`
|
||||
ContentAvailable bool `json:"content_available,omitempty"`
|
||||
Sound string `json:"sound,omitempty"`
|
||||
Data D `json:"data,omitempty"`
|
||||
|
||||
// Android
|
||||
ApiKey string `json:"api_key,omitempty"`
|
||||
@@ -64,7 +61,6 @@ type PushNotification struct {
|
||||
TimeToLive uint `json:"time_to_live,omitempty"`
|
||||
RestrictedPackageName string `json:"restricted_package_name,omitempty"`
|
||||
DryRun bool `json:"dry_run,omitempty"`
|
||||
Data gcm.Data `json:"data,omitempty"`
|
||||
Notification gcm.Notification `json:"notification,omitempty"`
|
||||
|
||||
// iOS
|
||||
@@ -178,9 +174,9 @@ func GetIOSNotification(req PushNotification) *apns.Notification {
|
||||
payload.ContentAvailable()
|
||||
}
|
||||
|
||||
if len(req.Extend) > 0 {
|
||||
for _, extend := range req.Extend {
|
||||
payload.Custom(extend.Key, extend.Value)
|
||||
if len(req.Data) > 0 {
|
||||
for k, v := range req.Data {
|
||||
payload.Custom(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -309,17 +305,12 @@ func GetAndroidNotification(req PushNotification) gcm.HttpMessage {
|
||||
notification.DryRun = true
|
||||
}
|
||||
|
||||
if len(req.Extend) > 0 {
|
||||
notification.Data = make(map[string]interface{})
|
||||
|
||||
for _, extend := range req.Extend {
|
||||
notification.Data[extend.Key] = extend.Value
|
||||
}
|
||||
}
|
||||
|
||||
// overwrite Extend
|
||||
// Add another field
|
||||
if len(req.Data) > 0 {
|
||||
notification.Data = req.Data
|
||||
notification.Data = make(map[string]interface{})
|
||||
for k, v := range req.Data {
|
||||
notification.Data[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
notification.Notification = &req.Notification
|
||||
|
||||
@@ -74,15 +74,9 @@ func TestIOSNotificationStructure(t *testing.T) {
|
||||
Badge: 1,
|
||||
Sound: test,
|
||||
ContentAvailable: true,
|
||||
Extend: []ExtendJSON{
|
||||
{
|
||||
Key: "key1",
|
||||
Value: "1",
|
||||
},
|
||||
{
|
||||
Key: "key2",
|
||||
Value: "2",
|
||||
},
|
||||
Data: D{
|
||||
"key1": "test",
|
||||
"key2": 2,
|
||||
},
|
||||
Category: test,
|
||||
URLArgs: []string{"a", "b"},
|
||||
@@ -103,8 +97,8 @@ func TestIOSNotificationStructure(t *testing.T) {
|
||||
sound, _ := jsonparser.GetString(data, "aps", "sound")
|
||||
contentAvailable, _ := jsonparser.GetInt(data, "aps", "content-available")
|
||||
category, _ := jsonparser.GetString(data, "aps", "category")
|
||||
key1 := dat["key1"].(string)
|
||||
key2 := dat["key2"].(string)
|
||||
key1 := dat["key1"].(interface{})
|
||||
key2 := dat["key2"].(interface{})
|
||||
aps := dat["aps"].(map[string]interface{})
|
||||
urlArgs := aps["url-args"].([]interface{})
|
||||
|
||||
@@ -116,8 +110,8 @@ func TestIOSNotificationStructure(t *testing.T) {
|
||||
assert.Equal(t, 1, int(badge))
|
||||
assert.Equal(t, test, sound)
|
||||
assert.Equal(t, 1, int(contentAvailable))
|
||||
assert.Equal(t, "1", key1)
|
||||
assert.Equal(t, "2", key2)
|
||||
assert.Equal(t, "test", key1)
|
||||
assert.Equal(t, 2, int(key2.(float64)))
|
||||
assert.Equal(t, test, category)
|
||||
assert.Contains(t, urlArgs, "a")
|
||||
assert.Contains(t, urlArgs, "b")
|
||||
@@ -193,15 +187,9 @@ func TestAndroidNotificationStructure(t *testing.T) {
|
||||
DryRun: true,
|
||||
Title: test,
|
||||
Sound: test,
|
||||
Extend: []ExtendJSON{
|
||||
{
|
||||
Key: "key1",
|
||||
Value: "1",
|
||||
},
|
||||
{
|
||||
Key: "key2",
|
||||
Value: "2",
|
||||
},
|
||||
Data: D{
|
||||
"a": "1",
|
||||
"b": 2,
|
||||
},
|
||||
Notification: gcm.Notification{
|
||||
Color: test,
|
||||
@@ -224,33 +212,8 @@ func TestAndroidNotificationStructure(t *testing.T) {
|
||||
assert.Equal(t, test, notification.Notification.Color)
|
||||
assert.Equal(t, test, notification.Notification.Tag)
|
||||
assert.Equal(t, "Welcome", notification.Notification.Body)
|
||||
assert.Equal(t, "1", notification.Data["key1"])
|
||||
|
||||
// add data file to overwrite `Extend`
|
||||
req = PushNotification{
|
||||
Tokens: []string{"a", "b"},
|
||||
Message: "Welcome",
|
||||
To: test,
|
||||
Data: map[string]interface{}{
|
||||
"a": "1",
|
||||
"b": "2",
|
||||
},
|
||||
Extend: []ExtendJSON{
|
||||
{
|
||||
Key: "key1",
|
||||
Value: "1",
|
||||
},
|
||||
{
|
||||
Key: "key2",
|
||||
Value: "2",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
notification = GetAndroidNotification(req)
|
||||
|
||||
assert.Equal(t, "1", notification.Data["a"])
|
||||
assert.Equal(t, "2", notification.Data["b"])
|
||||
assert.Equal(t, 2, notification.Data["b"])
|
||||
}
|
||||
|
||||
func TestPushToIOS(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user