I have Firebase based notification in my app. On receiving the message from the server, I can see the notification in my phone. But when I click on the message in the notification, it is always opening my Splash screen followed by LoginActivity instead of the MainActivity. I want to open the MainActivity and show the message details, for which I am passing the message body of the notification to the MainActivity using 'putExtra'.
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getTitle().toString(), remoteMessage.getNotification().getBody());
}
//This method is only generating push notification
private void sendNotification(String messageTitle, String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Title", messageTitle);
intent.putExtra("data", messageBody);
intent.putExtra("KEY", "Notify");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
numNotifications = numNotifications + 1;
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
inboxStyle.setBigContentTitle(getString(R.string.app_notification)) ;
inboxStyle.setSummaryText(getString(R.string.app_count_notifications1)
+ numNotifications + getString(R.string.app_count_notifications2));
inboxStyle.bigText(messageBody);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.q_me_notify)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(inboxStyle)
.setGroup(TAG)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
try {
notificationManager.notify(TAG, APPID, notificationBuilder.build());
} catch (Exception e) {
e.printStackTrace();
}
}
My Android manifest file looks like:
<application
android:allowBackup="true"
android:icon="@drawable/final"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:theme="@style/LoginTheme"
android:launchMode="singleTask"/>
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme"
android:launchMode="singleTask"/>
<!-- Defining Services -->
<service android:name=".services.FirebaseMessageService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".services.FirebaseInstanceService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
This is happening in my Samsung On8 (J7 Pro) with Android 7.0 - whether the app is in foreground or background (it always opens the LoginActivity). But with my Nexus 5 having Android 5.1.1 this behavior is seen only when the app in background / terminated, whereas it works fine when the app is in foreground.
So, my questions are:
How can I make the app show MainActivity when in foreground irrespective of the model and OS version, so that the notification message details are shown correctly?
When the app is in background / terminated, how can I pass the notification values across (Splash --> LoginActivity --> MainActivity), so that the message is correctly displayed in MainActivity?