0

I am getting push notification via onMessageReceived(Remotemessage mesg) when my app is open state. If my app is in killed state i am getting push notification but not from onMessageReceived().

means, after push notification received, based on the data from the notification i need to redirect the page, when notification tapped. if app in foreground that works fine. When i killed, i got notification on tray but when i tap on the notification it opens my app only not that redirected page. I put log and checked that, onMessageReceived is not even called. But got notification on tray. I searched a lot and lot. Couldn't find the issue. Backend payload is below

  "to": "f2dFWIn81FY:APA91bGumzp0LRr4hvGqUb9WMOvmWqvHSqYcCBzTJlbJs2dmpfrv_tAdu",
  "priority": "high",
  "content_available": true,
  "Type": "Order",
  "notification": {
     "body": "Your [2224531] order is rejected",
     "title": "Order",
     "sound": "default"
  },
  "data": {
     "Title": "Your [2224531] order is rejected",
     "NotificationId": "efb46a45-cd9c-4cf7-881f-9510c24555a6"
  }
}

I am trying to get the data in MainActivity.java but the bundle is null!!

Image of MainActivity

1 Answers1

0

This is working as intended. onMessageReceived is not called if app is in the background/closed. The data specified within the message should be available like this:

if (intent?.hasExtra("NotificationId") == true) {
    val id = intent?.extras?.getString("NotificationId")?.toString())                    
}

In your onCreate startup activity if the app was opened from the message click intent.

Simon
  • 1,657
  • 11
  • 16
  • edited my question with the image of MainActivity.java. which has null data; – Muthukumar Subramaniam Apr 20 '20 at 05:20
  • yes, this is what i already have in MainActivity. It was not working. So i accidentally tried in SplashActivity. and its now working in some devices. I tried in OnePlus and Xiaomi absolutely fine. But Testing team using Samsung and they are not getting any notification. – Muthukumar Subramaniam Apr 22 '20 at 08:35
  • This data is only available in launcher activity (android.intent.category.LAUNCHER). If you need it in main activity, you need to pass that data manually via intent (where you are starting MainActivity. Like so: https://stackoverflow.com/a/2091482/8835675 – Simon Apr 22 '20 at 09:10