There are several notifications and clicking on notifications i want to go to specific activity. Say a Reward notification appear and by clicking will go to RewardActivity and new friend added then clicking on newFriend notification will go to friendlistActivity whether the app is in foreground or background.
but in my case if any notification appear it going to same activity instead of different activity.
private void handleDataMessage(String noti_title,String noti_message,String noti_click_action) {
try {
Intent intent = new Intent(this, RewardActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("title", noti_title);
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
//image.compress(Bitmap.CompressFormat.PNG, 50, _bs);
//intent.putExtra("img", image);
intent.putExtra("msg", noti_message);
intent.putExtra("click_action", noti_click_action);
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, "Default")
//.setLargeIcon(R.mipmap.ic_launcher)/*Notification icon image*/
.setSmallIcon(R.mipmap.ic_launcher)
//.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setPriority(Notification.PRIORITY_HIGH)
.setChannelId("Default")
.setVibrate(new long[]{1000, 1000})
.setContentIntent(pendingIntent);
notificationBuilder.setContentTitle(noti_title);
notificationBuilder.setContentText(noti_message);
notificationBuilder.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
FCM automatically displays the message to end-user devices on behalf of the client app. When user click on notification there is two conditions created
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.
if you want to open desired activity when app is in background and notification is contain only notification and without data playload then it is not posible.
But, if if you want to open desired activity when app is in background and notification is contain data playload then you can navigate the user to desired activity.
See Below Example for Open Desired Activity when Messages with data payload, both background and foreground Condition.
in my AndroidManifest launcher Activity is SplashActivity
You can test notification on pushtry.com from Google FCM Tester. Here is the format of playload data when send through pushtry.com
MyFirebaseMessagingService Class:
Now, when you recieved pushnotification in background condition and clicked on notification, notification is delivered to the device’s system tray and Extras pass to your launcher Activity. Check launcher Activity is with extras or empty and than navigate the user to desired Activity.
if you want to show your activity to only logged in user only than SplashActivity looks like Below:
Logic in SplashActivty for navigate the user in different Activity as per Condition.
When user open application in normal flow without clicking on pushnotification, getIntent().hasExtra(“pushnotification”) is null so command goes to catch block and CheckLogin() method checked for already login or not. But user entered by clicking pushnotification then getIntent().hasExtra(“pushnotification”) is not null and he will go to desired Activity.