From 5cb40cf5b451d306f4ef8b07691317f2cc2b1e69 Mon Sep 17 00:00:00 2001 From: Sebastien Melki Date: Mon, 20 Jan 2020 17:14:07 +0200 Subject: [PATCH] =?UTF-8?q?[Important]=20allow=20sending=20data=20ONLY=20n?= =?UTF-8?q?otifications=20for=20FCM=20notifi=E2=80=A6=20(#453)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- gorush/notification.go | 18 +++++++++--------- gorush/notification_fcm.go | 22 ++++++++++++---------- gorush/notification_fcm_test.go | 6 +++++- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/gorush/notification.go b/gorush/notification.go index 3f77a99..c7d068e 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -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"` diff --git a/gorush/notification_fcm.go b/gorush/notification_fcm.go index 6f48c65..d6202e6 100644 --- a/gorush/notification_fcm.go +++ b/gorush/notification_fcm.go @@ -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 diff --git a/gorush/notification_fcm_test.go b/gorush/notification_fcm_test.go index 60c4aad..b999625 100644 --- a/gorush/notification_fcm_test.go +++ b/gorush/notification_fcm_test.go @@ -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)