0

In my project, I have to receive FCM push notification when the app is running onMessageReceived() is calling an app working well but the problem is When app killed or in background onMessageReceived() is not calling but FCM notification is displaying on Notification tray

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Tharun
  • 29
  • 7
  • In that case your launcher activity will handle the notification Not onMessageReceived() method. – Saurabh Vadhva Mar 21 '18 at 13:01
  • @Tharun When FCM message is send using notification type, you won't receive message in onMessageReceived() method when your app is not running – Hasif Seyd Mar 21 '18 at 13:02
  • Thank you @HasifSeyd but i have to that handle that case can you please help me on that – Tharun Mar 21 '18 at 13:14
  • @Tharun But When you click on the notification from notification tray , you will get the data as Bundle in the Launch Activity which would be assigned when sending push notification from the server – Hasif Seyd Mar 21 '18 at 13:17
  • @Tharun, Please check my [answer](https://stackoverflow.com/a/49152652/996493) – Lucifer Mar 21 '18 at 13:19
  • @HasifSeyd Yes i was try to handle the bundle in launcher activity but am not succeed on that , I think i am confuse which strings i have to take from that bundle – Tharun Mar 21 '18 at 13:22
  • @HasifSeyd i was tried one more case on In launcher activity i was taken flag values(if (flag == 335544320 || flag == 67108864 || flag == 339738624) { // my conditions on ths block }) if FCM notification is received i got various issues Some time working well and some times failure in some device – Tharun Mar 21 '18 at 13:26

1 Answers1

0

To solve this, you need to use data payload alone, example:

"data": {
"titles": "New Title",
"bodys": "body here"
}

since if you use data payload it will enter onMessageRecieved() if the application was in the foreground or background.

Then in FCM you can do the following to receive the data:

 if (remoteMessage.getData().size() > 0) {

    title = remoteMessage.getData().get("titles");
    body = remoteMessage.getData().get("bodys");
}

more info here:

https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134