Open specific Activity when notification clicked in FCM
Solution 1:
With FCM, you can send two types of messages to clients:
1. Notification messages: sometimes thought of as "display messages."
FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys.
2. Data messages: which are handled by the client app.
Client app is responsible for processing data messages. Data messages have only custom key-value pairs.
According to FCM document Receive Messages in an Android App
- Notifications delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
- Messages with both notification and data payload, both background and foreground. In this case, 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.
Set click_action
in the notification payload:
So, if you want to process the messages arrived in the background, you have to send click_action
with message.
click_action
is a parameter of the notification payload
If you want to open your app and perform a specific action, set click_action
in the notification payload and map it to an intent filter in the Activity you want to launch.
For example, set click_action
to OPEN_ACTIVITY_1
to trigger an intent filter like the following:
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
FCM payload looks like below:
{
"to":"some_device_token",
"content_available": true,
"notification": {
"title": "hello",
"body": "test message",
"click_action": "OPEN_ACTIVITY_1"
},
"data": {
"extra":"juice"
}
}
Solution 2:
When app is in background, Intent must be delivered at your launcher activity . So it opens your Launcher activity. Now you check if there is data in Intent in launcher activity then start your required Activity.
Inside your Launcher Activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
// possible launched from notification
// check if desired notification data present in extras then its
// confirmed that launched from notification
}else{
// not launched from notification
}
Solution 3:
AndroidManifest.xml
<activity android:name="YOUR_ACTIVITY">
<intent-filter>
<action android:name="com.example.yourapplication_YOUR_NOTIFICATION_NAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Your FirebaseMessagingService.java file onMessageReceived method:
public void onMessageReceived(RemoteMessage remoteMessage){
String title=remoteMessage.getNotification().getTitle();
String message=remoteMessage.getNotification().getBody();
String click_action=remoteMessage.getNotification().getClickAction();
Intent intent=new Intent(click_action);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(message);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}
Your cloud function/server code for notification must be like this:
notification: {
title: "TITLE OF NOTIFICATION",
body: "NOTIFICATION MESSAGE",
sound: "default",
click_action:"com.example.yourapplication_YOUR_NOTIFICATION_NAME"
}
Solution 4:
Open MyFirebaseMessagingService.java file
inside that file there is a sendNotification() method, there you have to specify your activity you need to navigate to in the Intent as shown below
Intent intent = new Intent(this, YourActivityName.class);
if you are sending multiple notification and you want to navigate to different Activities on click of a particular Notification you can use any conditional statement to achieve it, my suggestion is to use a switch case as shown below
private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) {
Intent intent = new Intent();
switch(condition) {
case '1': intent = new Intent(this, ActivityOne.class);
break;
case '2': intent = new Intent(this, ActivityTwo.class);
break;
case '3': intent = new Intent(this, ActivityThree.class);
break;
default : intent = new Intent(this, DefaultActivity.class);
break;
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Notification", TrueOrFalse);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
}
Using this logic you can open a specific activity on Notification click in FCM. This works perfectly for me. Thanks
Solution 5:
Here is the easiest way to understand this.
When you send a data payload with the notification payload as
notification: {
title: "Your order status.",
body: orderStatusDetail,
clickAction: "ShopFragment"
},
data: {
ORDER_ID: orderId
}
The notification clickAction will be the filter you will use to pass data into your Activity, what data? The data sent by the data: { }
object appended to the payload.
So, the clickAction will trigger the intent filter in your Manifest, so first we need to create it
<activity
android:name=".activity.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="ShopFragment"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Now we set the intent filter with the same name as our clickAction, doing this we trigger that whenever we press the notification at our notification tab, this intent filter will launch and so on the activity associated with this intent filter.
And then, just use intent.getStringExtra("ORDER_ID")
at your MainActivity to get your String extra sent at your data payload.
Make sure that ORDER_ID in this case is the key we send from the data { }
object and needs to be the same in the client to get this data.