JavaUser
JavaUser

Reputation: 26364

FCM - How to traverse to a particular activity when system tray notification is clicked

We are using FCM to show push notification to the end user . FCM successfully raised the notification and the user is seeing the notification from the system tray.I am using FCM console Notification composer for testing.

I will keep my app in the background and send message from FCM console.

The requirement is to navigate to specific activity in the application when the tray message is clicked by the user(when the application in background). Right now , by default it opens the app launcher page.

I couldn't get proper answer from google . Most of the answers are for GCM and most are not working solution . I don't find proper document in FCM page.

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.

FCM page - 

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.

Thanks

Upvotes: 0

Views: 949

Answers (3)

Kuldeep Kulkarni
Kuldeep Kulkarni

Reputation: 796

You have to add <intent-filter> with action inside AndroidManifest.xml and set click_action in the notification payload then it will work when either app is in background or killed.

You can refer below link:

open a specific activity from firebase notification

Upvotes: 1

WoogieNoogie
WoogieNoogie

Reputation: 1257

You need to handle the push yourself, and don't allow Google to automatically add the status notification. By default, the Firebase Console will take over delivery of the status notification for you, and not allow much else to happen. In your Firebase receiver, simply build your own notification based on the data pattern (that you set up) in the push, and add a PendingIntent to the notification.

Upvotes: 0

Pawan Singh Chauhan
Pawan Singh Chauhan

Reputation: 253

put this code inside onMessageReceived() method

 private void sendNotification(String messageBody) {
            Intent intent = new Intent(this, YOUR_TARGET_ACTIVITY.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);

            Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_stat_ic_notification)
                    .setContentTitle("FCM Message")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        }

Upvotes: 0

Related Questions