Monday, 10 April 2023

Handling data notification only in both iOS and Android fails

I have a server that sends push notifications. For Android, I override onMessageReceived to handle the message. For iOS, I use NotificationExtension. I need to change the incoming push notification (I don't know the title and everything server side, the client knows this). What is odd is that I can not find a way to make it work for both platforms. Here's the thing:

  • For Android, the payload of the push notification must NOT contain a notification block. If there is a notification block present, the title and body of the notification will be shown when the app is in the background and the onMessageReceived is never called. This is not desired. Removing the notification block and just keep data works for Android for both background and foreground mode.
  • When I omit the notification block for iOS, I get an error back from Firebase: InvalidParameters: Invalid notification payload.

Examples

Payload without notification (works fully for Android on background + foreground mode, fails to send (and thus retrieve) for iOS):

{
 "to" : "-",
 "data" : {
     "body" : "Notification Body",
     "title": "Notification Title",
 },
    "mutable_content": true
}

Payload with notification, this works fully for iOS but does not call the onMessageReceived method on Android when the app is in the background:

{
 "to" : "-",
 "data" : {
     "body" : "Notification Body",
     "title": "Notification Title",
 },
    "mutable_content": true,
    "notification": { "title": "test" }
}

How can I make both platforms work on foreground + background data push messages? According to https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#Notification, it states that it should be possible to send data notifications only to iOS, I tried to set the header but I still get the same error: "When sending payloads containing only data fields to iOS devices, only normal priority ("apns-priority": "5") is allowed in ApnsConfig."



from Handling data notification only in both iOS and Android fails

No comments:

Post a Comment