I have an application implemented with firebase push notifications. I have a scenario where I open a NotiticatinDetails activity on click of the notification and show the notification message in a textview. It works fine when the app is in foreground. But it doesn't navigate to NotiticatinDetails activity when the app is in kill state at the time of receiving notification. It launches MainActivity only. Instead, I want to launch NotiticatinDetails activity and show the notification message just like how I am doing it in the foreground case. And when I click on back/close button in NotiticatinDetails activity it has to navigate to previous activity (MainActivity in app killed state) instead of closing the application.
This is how I am sending notification to NotiticatinDetails activity from my FirebaseService class. Calling below method in onMessageReceived method.
private void sendNotification(String msg) {
Intent notifyIntent = new Intent(ctx, NotificationDetailsActivity.class);
notifyIntent.putExtra("NotificationMessage", msg);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
mNotificationManager = (NotificationManager)
ctx.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent notifyPendingIntent = PendingIntent.getActivity(ctx, 0,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
ctx,
NOTIFICATION_CHANNEL_ID)
.setContentText(msg)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(android.R.drawable.ic_popup_reminder)
.setAutoCancel(true)
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL);
notificationBuilder.setContentIntent(notifyPendingIntent);
mNotificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
if (NotificationDetailsActivity.isVisible) {
NotificationDetailsActivity.notificationDetailsActivity.ToastNotify(msg);
}
}
Calling onNewIntent method in NotificationDetails activity as below.
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle extras = intent.getExtras();
if(extras != null){
if(extras.containsKey("NotificationMessage")){
String msg = extras.getString("NotificationMessage");
TextView helloText = (TextView) findViewById(R.id.text_hello);
helloText.setText(msg);
}
}
}
I have added same onNewIntent method in MainActivity also but it doesn't receive the data. Please help me out to navigate to NotificationActivity and show data when app is in killed state.