2

I am trying to get my app to process the DATA payload while it is in the background.

"data": {
        "aKey": "aValue",
        "customtitle": "my title",
        "custombody": "my body",
        "customimage": "my image"
    },
    "notification": {
        "title": "my title",
        "text": "some text",
        "icon": "icon to use",
        "sound": "soundtouse"
    }

Accroding to the documentation under the section Handle notification messages in a backgrounded app

..the data payload is delivered in the extras of the intent of your launcher Activity.

In my application, the onMessageReceived only receives a RemoteMessage object.

I tried overriding the onMessageReceived signature to accept an Intent but that doesn't work.

I can find no tutorial or explanation on this other than the the VERY LAST SENTENCE in the documentation.

Can someone tell me where there are examples on how to handle the data payload when the app is in the background or closed?

If the app is open, then it processes the Data payload fine, from my onMessageReceived(RemoteMessage)` method.

AL.
  • 36,815
  • 10
  • 142
  • 281
user-44651
  • 3,924
  • 6
  • 41
  • 87

1 Answers1

0

You're only encountering the expected behavior for FCM. In the documentation you linked, the table shows that when using a data payload, it will only trigger onMessageReceived() if the message payload only has data:

"data": {
        "aKey": "aValue",
        "customtitle": "my title",
        "custombody": "my body",
        "customimage": "my image"
    }

But you are using both, notification and data in your message payload. Which means that when your app is in background, the payload will be handled by the Android system itself, showing the notification details in the Notification Tray.

Then when the user taps on that Android handled notification, it should open your app (usually the MainActivity or whichever activity you set to open by using click_action) and from there, you should handle the intent like so:

    Intent intent = getIntent();
    String pos = getIntent().getStringExtra("<YOUR KEY HERE>");
    if(pos !=null){
        selectDrawerItem(navigationView.getMenu().getItem(Integer.parseInt(pos)));
    }
AL.
  • 36,815
  • 10
  • 142
  • 281
  • Let me see if I understand this correct, so if only `data` payload then `onMessageReceived()` will be called and it will work in the background/foreground/killed app even if it is sending to topics like this one https://stackoverflow.com/questions/47505629/how-can-i-send-notification-to-various-topics/47542196#47542196 – Peter Haddad Dec 27 '17 at 10:35
  • @PeterHaddad `onMessageReceived()` should trigger regardless if the app is in foreground/background. But for the killed/stopped state, it may differ per device. See my answer [here](https://stackoverflow.com/a/39505298/4625829) – AL. Dec 28 '17 at 04:36
  • Thank you AL. one last question (and will stop bothering you :p) when using only topics to send a notification and if the user logged in to his account from another device, will he receive the notification he is subscribed to in that device? So does the subscribed topics depend on the authenticated user or the device? – Peter Haddad Dec 30 '17 at 08:13
  • 1
    @PeterHaddad No worries. A [topic subscription is kept](https://stackoverflow.com/a/43554044/4625829), so long as the token is from the same device -- a registration token is somehow tied to the device it was generated with -- so if a user uses a *different* device, it would count as a different token, in which case you would have to re-subscribe the user to the corresponding tokens. Cheers! – AL. Dec 30 '17 at 12:39