Enhance document and android payload
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
@@ -47,11 +47,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"`
|
||||
Priority string `json:"priority,omitempty"`
|
||||
ContentAvailable bool `json:"content_available,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"`
|
||||
Extend []ExtendJSON `json:"extend,omitempty"`
|
||||
|
||||
// Android
|
||||
ApiKey string `json:"api_key,omitempty"`
|
||||
@@ -65,15 +68,13 @@ type PushNotification struct {
|
||||
Notification gcm.Notification `json:"notification,omitempty"`
|
||||
|
||||
// iOS
|
||||
Expiration int64 `json:"expiration,omitempty"`
|
||||
ApnsID string `json:"apns_id,omitempty"`
|
||||
Topic string `json:"topic,omitempty"`
|
||||
Badge int `json:"badge,omitempty"`
|
||||
Sound string `json:"sound,omitempty"`
|
||||
Category string `json:"category,omitempty"`
|
||||
URLArgs []string `json:"url-args,omitempty"`
|
||||
Extend []ExtendJSON `json:"extend,omitempty"`
|
||||
Alert Alert `json:"alert,omitempty"`
|
||||
Expiration int64 `json:"expiration,omitempty"`
|
||||
ApnsID string `json:"apns_id,omitempty"`
|
||||
Topic string `json:"topic,omitempty"`
|
||||
Badge int `json:"badge,omitempty"`
|
||||
Category string `json:"category,omitempty"`
|
||||
URLArgs []string `json:"url-args,omitempty"`
|
||||
Alert Alert `json:"alert,omitempty"`
|
||||
}
|
||||
|
||||
func CheckPushConf() error {
|
||||
@@ -185,8 +186,8 @@ func GetIOSNotification(req PushNotification) *apns.Notification {
|
||||
|
||||
// Alert dictionary
|
||||
|
||||
if len(req.Alert.Title) > 0 {
|
||||
payload.AlertTitle(req.Alert.Title)
|
||||
if len(req.Title) > 0 {
|
||||
payload.AlertTitle(req.Title)
|
||||
}
|
||||
|
||||
if len(req.Alert.TitleLocKey) > 0 {
|
||||
@@ -308,6 +309,15 @@ 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
|
||||
if len(req.Data) > 0 {
|
||||
notification.Data = req.Data
|
||||
}
|
||||
@@ -319,6 +329,14 @@ func GetAndroidNotification(req PushNotification) gcm.HttpMessage {
|
||||
notification.Notification.Body = req.Message
|
||||
}
|
||||
|
||||
if len(req.Title) > 0 {
|
||||
notification.Notification.Title = req.Title
|
||||
}
|
||||
|
||||
if len(req.Sound) > 0 {
|
||||
notification.Notification.Sound = req.Sound
|
||||
}
|
||||
|
||||
return notification
|
||||
}
|
||||
|
||||
|
||||
@@ -128,6 +128,8 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
|
||||
|
||||
test := "test"
|
||||
req := PushNotification{
|
||||
Message: "Welcome",
|
||||
Title: test,
|
||||
Alert: Alert{
|
||||
Action: test,
|
||||
ActionLocKey: test,
|
||||
@@ -135,7 +137,6 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
|
||||
LaunchImage: test,
|
||||
LocArgs: []string{"a", "b"},
|
||||
LocKey: test,
|
||||
Title: test,
|
||||
TitleLocArgs: []string{"a", "b"},
|
||||
TitleLocKey: test,
|
||||
},
|
||||
@@ -190,12 +191,21 @@ func TestAndroidNotificationStructure(t *testing.T) {
|
||||
TimeToLive: 100,
|
||||
RestrictedPackageName: test,
|
||||
DryRun: true,
|
||||
Data: map[string]interface{}{
|
||||
"a": "1",
|
||||
"b": "2",
|
||||
Title: test,
|
||||
Sound: test,
|
||||
Extend: []ExtendJSON{
|
||||
{
|
||||
Key: "key1",
|
||||
Value: "1",
|
||||
},
|
||||
{
|
||||
Key: "key2",
|
||||
Value: "2",
|
||||
},
|
||||
},
|
||||
Notification: gcm.Notification{
|
||||
Title: test,
|
||||
Color: test,
|
||||
Tag: test,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -210,7 +220,37 @@ func TestAndroidNotificationStructure(t *testing.T) {
|
||||
assert.Equal(t, test, notification.RestrictedPackageName)
|
||||
assert.True(t, notification.DryRun)
|
||||
assert.Equal(t, test, notification.Notification.Title)
|
||||
assert.Equal(t, test, notification.Notification.Sound)
|
||||
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"])
|
||||
}
|
||||
|
||||
func TestPushToIOS(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user