Enhance document and android payload
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
7c551da226
commit
08339e36d7
41
README.md
41
README.md
|
@ -183,6 +183,9 @@ Send multiple notifications as below:
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Request body
|
||||||
|
|
||||||
Request body must has a notifications array. The following is a parameter table for each notification.
|
Request body must has a notifications array. The following is a parameter table for each notification.
|
||||||
|
|
||||||
|name|type|description|required|note|
|
|name|type|description|required|note|
|
||||||
|
@ -192,6 +195,9 @@ Request body must has a notifications array. The following is a parameter table
|
||||||
|message|string|message for notification|o||
|
|message|string|message for notification|o||
|
||||||
|priority|string|Sets the priority of the message.|-||
|
|priority|string|Sets the priority of the message.|-||
|
||||||
|content_available|bool|data messages wake the app by default.|-||
|
|content_available|bool|data messages wake the app by default.|-||
|
||||||
|
|sound|string|sound type|-||
|
||||||
|
|title|string|notification title|-||
|
||||||
|
|extend|string array|extensible partition|-||
|
||||||
|api_key|string|Android api key|-|only Android|
|
|api_key|string|Android api key|-|only Android|
|
||||||
|to|string|The value must be a registration token, notification key, or topic.|-|only Android|
|
|to|string|The value must be a registration token, notification key, or topic.|-|only Android|
|
||||||
|collapse_key|string|a key for collapsing notifications|-|only Android|
|
|collapse_key|string|a key for collapsing notifications|-|only Android|
|
||||||
|
@ -200,17 +206,42 @@ Request body must has a notifications array. The following is a parameter table
|
||||||
|restricted_package_name|string|the package name of the application|-|only Android|
|
|restricted_package_name|string|the package name of the application|-|only Android|
|
||||||
|dry_run|bool|allows developers to test a request without actually sending a message|-|only Android|
|
|dry_run|bool|allows developers to test a request without actually sending a message|-|only Android|
|
||||||
|data|string array|data payload of a GCM message|-|only Android|
|
|data|string array|data payload of a GCM message|-|only Android|
|
||||||
|notification|string array|payload of a GCM message|-|only Android|
|
|notification|string array|payload of a GCM message|-|only Android. See the [detail]()|
|
||||||
|expiration|int|expiration for notification|-|only iOS|
|
|expiration|int|expiration for notification|-|only iOS|
|
||||||
|apns_id|string|A canonical UUID that identifies the notification|-|only iOS|
|
|apns_id|string|A canonical UUID that identifies the notification|-|only iOS|
|
||||||
|topic|string|topic of the remote notification|-|only iOS|
|
|topic|string|topic of the remote notification|-|only iOS|
|
||||||
|badge|int|badge count|-|only iOS|
|
|badge|int|badge count|-|only iOS|
|
||||||
|sound|string|sound type|-|only iOS|
|
|
||||||
|category|string|the UIMutableUserNotificationCategory object|-|only iOS|
|
|category|string|the UIMutableUserNotificationCategory object|-|only iOS|
|
||||||
|extend|string array|extensible partition|-|only iOS|
|
|alert|string array|payload of a iOS message|-|only iOS. See the [detail]()|
|
||||||
|alert|string array|payload of a iOS message|-|only iOS|
|
|
||||||
|
|
||||||
See more detail [APNs](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/APNsProviderAPI.html#//apple_ref/doc/uid/TP40008194-CH101-SW1) and [GCM](https://developers.google.com/cloud-messaging/http-server-ref#send-downstream) reference.
|
### iOS alert structure
|
||||||
|
|
||||||
|
|name|type|description|required|note|
|
||||||
|
|-------|-------|--------|--------|---------|
|
||||||
|
|action|||||
|
||||||
|
|action-loc-key|||||
|
||||||
|
|launch-image|||||
|
||||||
|
|loc-args|||||
|
||||||
|
|loc-key|||||
|
||||||
|
|title-loc-args|||||
|
||||||
|
|title-loc-key|||||
|
||||||
|
|
||||||
|
See more detail about [APNs Provider API](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/APNsProviderAPI.html#//apple_ref/doc/uid/TP40008194-CH101-SW1).
|
||||||
|
|
||||||
|
### Android notification structure
|
||||||
|
|
||||||
|
|name|type|description|required|note|
|
||||||
|
|-------|-------|--------|--------|---------|
|
||||||
|
|icon|string|Indicates notification icon.|-||
|
||||||
|
|tag|string|Indicates whether each notification message results in a new entry on the notification center on Android.|-||
|
||||||
|
|color|string|Indicates color of the icon, expressed in #rrggbb format|-||
|
||||||
|
|click_action|string|The action associated with a user click on the notification.|-||
|
||||||
|
|body_loc_key|string|Indicates the key to the body string for localization.|-||
|
||||||
|
|body_loc_args|string|Indicates the string value to replace format specifiers in body string for localization.|-||
|
||||||
|
|title_loc_key|string|Indicates the key to the title string for localization.|-||
|
||||||
|
|title_loc_args|string|Indicates the string value to replace format specifiers in title string for localization.|-||
|
||||||
|
|
||||||
|
See more detail about [GCM server reference](https://developers.google.com/cloud-messaging/http-server-ref#send-downstream).
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|
|
@ -47,11 +47,14 @@ type RequestPush struct {
|
||||||
|
|
||||||
type PushNotification struct {
|
type PushNotification struct {
|
||||||
// Common
|
// Common
|
||||||
Tokens []string `json:"tokens" binding:"required"`
|
Tokens []string `json:"tokens" binding:"required"`
|
||||||
Platform int `json:"platform" binding:"required"`
|
Platform int `json:"platform" binding:"required"`
|
||||||
Message string `json:"message" binding:"required"`
|
Message string `json:"message" binding:"required"`
|
||||||
Priority string `json:"priority,omitempty"`
|
Title string `json:"title,omitempty"`
|
||||||
ContentAvailable bool `json:"content_available,omitempty"`
|
Priority string `json:"priority,omitempty"`
|
||||||
|
ContentAvailable bool `json:"content_available,omitempty"`
|
||||||
|
Sound string `json:"sound,omitempty"`
|
||||||
|
Extend []ExtendJSON `json:"extend,omitempty"`
|
||||||
|
|
||||||
// Android
|
// Android
|
||||||
ApiKey string `json:"api_key,omitempty"`
|
ApiKey string `json:"api_key,omitempty"`
|
||||||
|
@ -65,15 +68,13 @@ type PushNotification struct {
|
||||||
Notification gcm.Notification `json:"notification,omitempty"`
|
Notification gcm.Notification `json:"notification,omitempty"`
|
||||||
|
|
||||||
// iOS
|
// iOS
|
||||||
Expiration int64 `json:"expiration,omitempty"`
|
Expiration int64 `json:"expiration,omitempty"`
|
||||||
ApnsID string `json:"apns_id,omitempty"`
|
ApnsID string `json:"apns_id,omitempty"`
|
||||||
Topic string `json:"topic,omitempty"`
|
Topic string `json:"topic,omitempty"`
|
||||||
Badge int `json:"badge,omitempty"`
|
Badge int `json:"badge,omitempty"`
|
||||||
Sound string `json:"sound,omitempty"`
|
Category string `json:"category,omitempty"`
|
||||||
Category string `json:"category,omitempty"`
|
URLArgs []string `json:"url-args,omitempty"`
|
||||||
URLArgs []string `json:"url-args,omitempty"`
|
Alert Alert `json:"alert,omitempty"`
|
||||||
Extend []ExtendJSON `json:"extend,omitempty"`
|
|
||||||
Alert Alert `json:"alert,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckPushConf() error {
|
func CheckPushConf() error {
|
||||||
|
@ -185,8 +186,8 @@ func GetIOSNotification(req PushNotification) *apns.Notification {
|
||||||
|
|
||||||
// Alert dictionary
|
// Alert dictionary
|
||||||
|
|
||||||
if len(req.Alert.Title) > 0 {
|
if len(req.Title) > 0 {
|
||||||
payload.AlertTitle(req.Alert.Title)
|
payload.AlertTitle(req.Title)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(req.Alert.TitleLocKey) > 0 {
|
if len(req.Alert.TitleLocKey) > 0 {
|
||||||
|
@ -308,6 +309,15 @@ func GetAndroidNotification(req PushNotification) gcm.HttpMessage {
|
||||||
notification.DryRun = true
|
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 {
|
if len(req.Data) > 0 {
|
||||||
notification.Data = req.Data
|
notification.Data = req.Data
|
||||||
}
|
}
|
||||||
|
@ -319,6 +329,14 @@ func GetAndroidNotification(req PushNotification) gcm.HttpMessage {
|
||||||
notification.Notification.Body = req.Message
|
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
|
return notification
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -128,6 +128,8 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
|
||||||
|
|
||||||
test := "test"
|
test := "test"
|
||||||
req := PushNotification{
|
req := PushNotification{
|
||||||
|
Message: "Welcome",
|
||||||
|
Title: test,
|
||||||
Alert: Alert{
|
Alert: Alert{
|
||||||
Action: test,
|
Action: test,
|
||||||
ActionLocKey: test,
|
ActionLocKey: test,
|
||||||
|
@ -135,7 +137,6 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
|
||||||
LaunchImage: test,
|
LaunchImage: test,
|
||||||
LocArgs: []string{"a", "b"},
|
LocArgs: []string{"a", "b"},
|
||||||
LocKey: test,
|
LocKey: test,
|
||||||
Title: test,
|
|
||||||
TitleLocArgs: []string{"a", "b"},
|
TitleLocArgs: []string{"a", "b"},
|
||||||
TitleLocKey: test,
|
TitleLocKey: test,
|
||||||
},
|
},
|
||||||
|
@ -190,12 +191,21 @@ func TestAndroidNotificationStructure(t *testing.T) {
|
||||||
TimeToLive: 100,
|
TimeToLive: 100,
|
||||||
RestrictedPackageName: test,
|
RestrictedPackageName: test,
|
||||||
DryRun: true,
|
DryRun: true,
|
||||||
Data: map[string]interface{}{
|
Title: test,
|
||||||
"a": "1",
|
Sound: test,
|
||||||
"b": "2",
|
Extend: []ExtendJSON{
|
||||||
|
{
|
||||||
|
Key: "key1",
|
||||||
|
Value: "1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Key: "key2",
|
||||||
|
Value: "2",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Notification: gcm.Notification{
|
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.Equal(t, test, notification.RestrictedPackageName)
|
||||||
assert.True(t, notification.DryRun)
|
assert.True(t, notification.DryRun)
|
||||||
assert.Equal(t, test, notification.Notification.Title)
|
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, "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) {
|
func TestPushToIOS(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue