Not opening specific activity on notification click when the app is in background/not running
<p>The notification-click starts specified activity only when the app is opened up and the notification-click is performed. If the app is in background/not running and the notification-click is performed, the application's MainActivity opens up. In short, it is like the app opens normally following the activity stack instead of opening the specified activity in the PendingIntent.</p>

<p>I want to redirect the notification clicks to two different Activities (ApprovalDetailActivity and ConversationDetailActivity), based on their type.</p>

<p>I am using FCM for Push notifications. I am pasting my Manifest file and my FCMListener file here. Please help me out.</p>

<p><strong>sendNotification()</strong> function in MyFirebaseMessagingService.java</p>

<pre><code>private void sendNotification(String messageBody)
    {
        Intent intent;
        System.out.println("----message body: " + messageBody);
        if(notificationBundle.getCategory().equalsIgnoreCase(Master.KEY_PUSH_NOTIFICATION_CONVERSATION))
        {
            intent = new Intent(this, ConversationDetailActivity.class);
            /*Conversation conversation = Master.notificationBundle.getConversation();
            Master.conversationsList = new ArrayList&lt;&gt;();
            Master.conversationsList.add(conversation);*/
        }
        else
        {
            intent = new Intent(this, ApprovalDetailActivity.class);
            if(notificationBundle.getApprovalType().equals("I"))
                intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_VERIFICATIONS);
            else if(notificationBundle.getApprovalType().equals("A"))
                intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_APPROVALS);
            else
                intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_COMPLETED);

            intent.putExtra(Master.KEY_IS_FROM_CONVERSATION, false);
        }

        intent.putExtra(Master.KEY_PUSH_NOTIFICATION_POST_ID , notificationBundle.getPostID());
        intent.putExtra(Master.KEY_IS_FROM_PUSH_NOTIFICATION, true);
        intent.putExtra(Master.KEY_POSITION, 0);

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

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.mnet_icon)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        int random = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
        notificationManager.notify(random, notificationBuilder.build());
    }
</code></pre>

<p><strong>AndroidManifest.xml</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="mnet.mediaware.com.m_net"&gt;

    &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
    &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
    &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
    &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;

    &lt;application
        android:name=".MnetApplication"
        android:allowBackup="true"
        android:icon="@drawable/mnet_icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"&gt;
        &lt;activity
            android:name=".activities.LoginActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;
                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
        &lt;activity
            android:name=".activities.MainActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_main"
            android:launchMode="singleTask"
            android:theme="@style/AppTheme.NoActionBar" /&gt;

        &lt;activity
            android:name=".activities.ConversationDetailActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_conversation_detail"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:launchMode="singleTask"
            android:windowSoftInputMode="stateHidden|adjustResize"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="mnet.mediaware.com.m_net.activities.ConversationDetailActivity" /&gt;
                &lt;action android:name="android.intent.action.VIEW" /&gt;
                &lt;category android:name="android.intent.category.DEFAULT" /&gt;
                &lt;data android:scheme="http" /&gt;
            &lt;/intent-filter&gt;

            &lt;meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="mnet.mediaware.com.m_net.activities.MainActivity" /&gt;
        &lt;/activity&gt;
        &lt;activity
            android:name=".activities.ApprovalDetailActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_approval_detail"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden|adjustResize"&gt;
            &lt;meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="mnet.mediaware.com.m_net.activities.MainActivity" /&gt;
        &lt;/activity&gt;
        &lt;activity
            android:name=".activities.NewConversationActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_new_conversation"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden|adjustResize"&gt;
            &lt;meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="mnet.mediaware.com.m_net.activities.MainActivity" /&gt;
        &lt;/activity&gt;
        &lt;activity
            android:name=".activities.NotificationActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_notification"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden|adjustResize"&gt;
            &lt;meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="mnet.mediaware.com.m_net.activities.MainActivity" /&gt;
        &lt;/activity&gt;
        &lt;activity
            android:name=".activities.ProfileActivity"
            android:label="@string/title_activity_profile"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme.NoActionBar"&gt;
            &lt;meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="mnet.mediaware.com.m_net.activities.MainActivity" /&gt;
        &lt;/activity&gt;

        &lt;service
            android:name=".utils.firebase.MyFirebaseMessagingService"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="com.google.firebase.MESSAGING_EVENT"/&gt;
            &lt;/intent-filter&gt;
        &lt;/service&gt;

        &lt;service
            android:name=".utils.firebase.MyFirebaseInstanceIDService"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="com.google.firebase.INSTANCE_ID_EVENT"/&gt;
            &lt;/intent-filter&gt;
        &lt;/service&gt;

    &lt;/application&gt;

&lt;/manifest&gt;
</code></pre>