1

I´ve set up my app with Firebase Cloud Messaging to recive notifications.

I created a Service that extends from FirebaseMessagingService and overwritten the method onMessageReceived. Here i created a notification that when you click on it, navigate to an activity depending on the RemoteMessage data that comes as a parameter. It's not a problem, but it only works if the app is in foreground or background.

When the application is closed, the operating system is responsible for launching the notification, and here comes the problem. When i click on it, the first activity is opened (in my case SplashActivity) and I have to read the notification data to know what activity to open next.

I don't know how to do it. Is there any way to get that data in SplashActivity?

Leandro Simonetta
  • 127
  • 1
  • 3
  • 8
  • Possible duplicate of [How to handle notification when app in background in Firebase](https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase) – MrMikimn Sep 11 '19 at 13:52

1 Answers1

1

Might be a duplicate of this.

In short, when the app is in background (or closed, for that matter), the data segment of the notification is passed to the intent extras.

From Google's documentation:

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

So, in your SplashActivity, you can do something like this:

@Override
public void onCreate(Bundle savedInstanceState) {

    if (getIntent() != null) {
         Bundle dataBundle = getIntent().getExtras();
         // dataBundle contains the notification data payload
    }
}
MrMikimn
  • 721
  • 1
  • 5
  • 19