Skip to content

Commit

Permalink
fix: use mutable content and content available when set.
Browse files Browse the repository at this point in the history
Content available is for background notifications hence the notification
shouldn't have an alert or sound.
Ref:
https://developer.apple.com/documentation/usernotifications/generating-a-remote-notification
  • Loading branch information
hchienjo committed Aug 14, 2024
1 parent b95fdd6 commit 059bf1a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 17 deletions.
72 changes: 62 additions & 10 deletions notify/notification_fcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 34 additions & 7 deletions notify/notification_fcm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{
Expand All @@ -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)
}

0 comments on commit 059bf1a

Please sign in to comment.