Push notifications content not available for Android in the background.

0
I'm trying to implement Push notifications with Firebase for our users. The use case is that when the app is in the background, they receive the notification in the system tray and when the tap that, the app opens and the OON flow will trigger with a show message activity that displays $Notification/Title and $Notification/Body. This is working perfectly for iOS. But on Android the message remains empty.   I did some digging and what I've found is that for Android, FCM handles the content of the JSON differently. The JSON being sent to FCM for Android looks like this:   {{ "message": {{ "token": "{1}", "data": {{ "guid": "{5}", "actionName": "{4}" }, "android": {{ "notification": {{ "title": "{2}", "body": "{3}", "notification_count": "{6}" } } } } The Title and Body are in the 'notification' part of the message body but from what i can find, this is only available when the notification is received while the app is open. When the app is in the background only 'data' can be read. I can of course edit this JSON but there seems to be some hidden logic in the module that fills the Notification object after DS_Notification is triggered, as this always creates an empty object. There is probably some Javascript action somewhere that fills $Notification/Title and $Notification body with android.notification.title and android.notification.body instead of someting like android.notification != empty ? android.notification.title : data.title.   Am I thinking about this wrong and can there be another solution?  
asked
1 answers
1

Hi Berry van Rooijen

 

I already worked on this When the app is backgrounded, Android system consumes the notification payload and shows it in the tray. The app does not receive the notification object via the normal message callback; only the data payload is guaranteed to be delivered to your app (or made available as Intent extras when the notification is tapped). That’s why $Notification/Title and Body are empty on Android: your Mendix module is reading andriod.notification.* which isn’t available to the app when backgrounded.

 

I solved it by using by Sending title/body inside data :duplicate title and body into the data object so your app can always read them.

 

sample:

{
  "message": {
    "token":"{1}",
    "data": {
      "guid":"{5}",
      "actionName":"{4}",
      "title":"{2}",
      "body":"{3}"
    },
    "android": {
      "notification": {
        "title":"{2}",
        "body":"{3}",
        "notification_count":"{6}"
      }
    }
  }
}

Then change your Mendix JS action to use android.notification.title ?? data.title (i.e. fallback to data.title).

 

I hope this helps!!

answered