[Important] allow sending data ONLY notifications for FCM notifi… (#453)

* allow sending data ONLY notifications for FCM notifications

ran into a little issue here need this tweak to be able to send and process background notifications on android

If you want to process android notifications in the BACKGROUND, they need to be data notifications, as in not contain the "notification" key in the payload.

This little change achieves it, because omitempty on structs for the JSON encoding does not work on sub-structs except if it is a pointer to it and set to nil.
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Sebastien Melki 2020-01-20 17:14:07 +02:00 committed by Bo-Yi Wu
parent cd83da717d
commit 5cb40cf5b4
3 changed files with 26 additions and 20 deletions

View File

@ -70,15 +70,15 @@ type PushNotification struct {
Retry int `json:"retry,omitempty"`
// Android
APIKey string `json:"api_key,omitempty"`
To string `json:"to,omitempty"`
CollapseKey string `json:"collapse_key,omitempty"`
DelayWhileIdle bool `json:"delay_while_idle,omitempty"`
TimeToLive *uint `json:"time_to_live,omitempty"`
RestrictedPackageName string `json:"restricted_package_name,omitempty"`
DryRun bool `json:"dry_run,omitempty"`
Condition string `json:"condition,omitempty"`
Notification fcm.Notification `json:"notification,omitempty"`
APIKey string `json:"api_key,omitempty"`
To string `json:"to,omitempty"`
CollapseKey string `json:"collapse_key,omitempty"`
DelayWhileIdle bool `json:"delay_while_idle,omitempty"`
TimeToLive *uint `json:"time_to_live,omitempty"`
RestrictedPackageName string `json:"restricted_package_name,omitempty"`
DryRun bool `json:"dry_run,omitempty"`
Condition string `json:"condition,omitempty"`
Notification *fcm.Notification `json:"notification,omitempty"`
// iOS
Expiration *int64 `json:"expiration,omitempty"`

View File

@ -60,19 +60,21 @@ func GetAndroidNotification(req PushNotification) *fcm.Message {
}
}
notification.Notification = &req.Notification
notification.Notification = req.Notification
// Set request message if body is empty
if len(req.Message) > 0 {
notification.Notification.Body = req.Message
}
if req.Notification != nil {
// Set request message if body is empty
if len(req.Message) > 0 {
notification.Notification.Body = req.Message
}
if len(req.Title) > 0 {
notification.Notification.Title = req.Title
}
if len(req.Title) > 0 {
notification.Notification.Title = req.Title
}
if v, ok := req.Sound.(string); ok && len(v) > 0 {
notification.Notification.Sound = v
if v, ok := req.Sound.(string); ok && len(v) > 0 {
notification.Notification.Sound = v
}
}
return notification

View File

@ -235,9 +235,10 @@ func TestAndroidNotificationStructure(t *testing.T) {
"a": "1",
"b": 2,
},
Notification: fcm.Notification{
Notification: &fcm.Notification{
Color: test,
Tag: test,
Body: "",
},
}
@ -263,6 +264,9 @@ func TestAndroidNotificationStructure(t *testing.T) {
req = PushNotification{
Tokens: []string{"a", "b"},
To: test,
Notification: &fcm.Notification{
Body: "",
},
}
notification = GetAndroidNotification(req)