notification on background firebase show dialog when apps open

2.1k views Asked by At

Sorry for my title, but let me explain this. I use firebase cloud messaging on my apps. Now, I need run the apps then show the dialog contain notification body and title when the apps not running or in background, after I touch the notification.

For default FCM is when the apps not running/in background, I touch the notification then apps running/go to foreground.

Can anyone guide me to handle it?

1

There are 1 answers

2
Akshay Bhat 'AB' On BEST ANSWER

Handle notification messages in a backgrounded app

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.

When you send a notification message with a data payload (notification and data) and the app is in the background you can retrieve the data from the extras of the intent that is launched as a result of the user tapping on the notification.

From the FCM sample which launches the MainActivity when the notification is tapped:

if (getIntent().getExtras() != null) {
    for (String key : getIntent().getExtras().keySet()) {
        String value = getIntent().getExtras().getString(key);
        Log.d(TAG, "Key: " + key + " Value: " + value);
    }
}

EDIT:
In onMessageRecieved(RemoteMessage remoteMessage) method use remoteMessage.getData() to get the data which is sent by server which returns a Map<String,String>.

Now create an Intent and specify a class to launch on click of notification and in this intent put your notification data. Like :

Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
resultIntent.putExtra("notification_data", data.getData());

Now pass this intent to a pendingIntent when you show a notification.

And when MainActivity.class is launched on click of notification just get the data like this :

Map<String,String> notificationDataMap 
      = getIntent().getSerializableExtra("notification_data");

So from map you can get the data.