0

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:

  1. 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?

  2. 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?

sukural
  • 251
  • 2
  • 4

2 Answers2

0

Add a TaskStackBuilder, replace:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

with this:

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

EDIT: Obviously in your MainActivity you should have logic to retrieve the the data from the Intent and update the Activity

Guanaco Devs
  • 1,822
  • 2
  • 21
  • 38
0

I solved it, after going through several posts and blogs. The best one and detailed, that helped me a lot is the answer from Mukesh Rana in this post Clicking on notification doesn't open mentioned activity

I am sending "data" payload from the server now using REST api, which is always picked up irrespective of whether the app is in foreground or background. And in case of app being in background, onClick of notification always launches the Splash screen (launcher). In onMessageReceived I get the 'data' using remoteMessage.getData(). In the intent I use intent.putExtra(key, value) to pass the values to the intent (Splash screen by default for app in background). From there I am passing it across to the MainActivity and getting the values using getStringExtra(key).

sukural
  • 251
  • 2
  • 4