diff --git a/notify/notification_fcm.go b/notify/notification_fcm.go index 58dff0839..0a8d7965d 100644 --- a/notify/notification_fcm.go +++ b/notify/notification_fcm.go @@ -71,29 +71,81 @@ func GetAndroidNotification(req *PushNotification) []*messaging.Message { if req.Image != "" { req.Notification.ImageURL = req.Image } + if req.MutableContent { + req.APNS = &messaging.APNSConfig{ + Payload: &messaging.APNSPayload{ + Aps: &messaging.Aps{ + MutableContent: req.MutableContent, + }, + }, + } + } } - // Check if the notification has a sound - if req.Sound != nil { - sound, ok := req.Sound.(string) - - if req.APNS == nil && ok { + // content-available is for background notifications and a badge, alert + // and sound keys should not be present. + // See: https://developer.apple.com/documentation/usernotifications/generating-a-remote-notification + if req.Badge == nil && req.Title == "" && req.Message == "" && req.ContentAvailable { + if req.Sound != nil { + _, ok := req.Sound.(string) + if !ok { + req.APNS = &messaging.APNSConfig{ + Payload: &messaging.APNSPayload{ + Aps: &messaging.Aps{ + ContentAvailable: req.ContentAvailable, + }, + }, + } + } + } else { req.APNS = &messaging.APNSConfig{ Payload: &messaging.APNSPayload{ Aps: &messaging.Aps{ - Sound: sound, + ContentAvailable: req.ContentAvailable, }, }, } } + } + + // Check if the notification has a sound + if req.Sound != nil { + sound, ok := req.Sound.(string) + if ok { + switch { + case req.APNS == nil: + req.APNS = &messaging.APNSConfig{ + Payload: &messaging.APNSPayload{ + Aps: &messaging.Aps{ + Sound: sound, + }, + }, + } + case req.APNS.Payload == nil: + req.APNS.Payload = &messaging.APNSPayload{ + Aps: &messaging.Aps{ + Sound: sound, + }, + } - if req.Android == nil && ok { - req.Android = &messaging.AndroidConfig{ - Notification: &messaging.AndroidNotification{ + case req.APNS.Payload.Aps == nil: + req.APNS.Payload.Aps = &messaging.Aps{ Sound: sound, - }, + } + default: + req.APNS.Payload.Aps.Sound = sound + + } + + if req.Android == nil { + req.Android = &messaging.AndroidConfig{ + Notification: &messaging.AndroidNotification{ + Sound: sound, + }, + } } } + } // Check if the notification is a topic diff --git a/notify/notification_fcm_test.go b/notify/notification_fcm_test.go index 98cd2a665..191396af5 100644 --- a/notify/notification_fcm_test.go +++ b/notify/notification_fcm_test.go @@ -149,13 +149,13 @@ func TestFCMMessage(t *testing.T) { func TestAndroidNotificationStructure(t *testing.T) { test := "test" req := &PushNotification{ - Tokens: []string{"a", "b"}, - Message: "Welcome", - To: test, - Priority: HIGH, - ContentAvailable: true, - Title: test, - Sound: test, + Tokens: []string{"a", "b"}, + Message: "Welcome", + To: test, + Priority: HIGH, + MutableContent: true, + Title: test, + Sound: test, Data: D{ "a": "1", "b": 2, @@ -177,6 +177,9 @@ func TestAndroidNotificationStructure(t *testing.T) { assert.Equal(t, "1", messages[0].Data["a"]) assert.Equal(t, "2", messages[0].Data["b"]) assert.Equal(t, "{\"c\":\"3\",\"d\":4}", messages[0].Data["json"]) + assert.NotNil(t, messages[0].APNS) + assert.Equal(t, req.Sound, messages[0].APNS.Payload.Aps.Sound) + assert.Equal(t, req.MutableContent, messages[0].APNS.Payload.Aps.MutableContent) // test empty body req = &PushNotification{ @@ -190,3 +193,27 @@ func TestAndroidNotificationStructure(t *testing.T) { assert.Equal(t, "", messages[0].Notification.Body) } + +func TestAndroidBackgroundNotificationStructure(t *testing.T) { + req := &PushNotification{ + Tokens: []string{"a", "b"}, + Priority: HIGH, + ContentAvailable: true, + Data: D{ + "a": "1", + "b": 2, + "json": map[string]interface{}{ + "c": "3", + "d": 4, + }, + }, + } + + messages := GetAndroidNotification(req) + + assert.Equal(t, "1", messages[0].Data["a"]) + assert.Equal(t, "2", messages[0].Data["b"]) + assert.Equal(t, "{\"c\":\"3\",\"d\":4}", messages[0].Data["json"]) + assert.NotNil(t, messages[0].APNS) + assert.Equal(t, req.ContentAvailable, messages[0].APNS.Payload.Aps.ContentAvailable) +}