Not opening specific activity on notification click when the app is in background/not running
Solution 1
As per Firebase Cloud Messaging documentation-If Activity is in foreground then onMessageReceived will get called. If Activity is in background or closed then notification message is shown in the notification center for app launcher activity. For More information Check this link
Solution 2
Notification messages 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, when received in the background. 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 (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()){
String value = getIntent().getExtras().getString(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}}
use this code to get intent datas
Solution 3
Only this thing worked for me. The thing work for me is simple. Make sure you add this in the activity that you want to open directly.
<intent-filter>
<action android:name="MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
And from the push notification you must add a new payload: click_action it will looks like this -
"notification": {
"title": "hello",
"body": "test message",
"click_action": "MAIN_ACTIVITY"
},
Note: You can name it as you want MAIN_ACTIVITY but must be same in both place.
Solution 4
I think I got the exact answer for you.
TaskStackBuilder Usage and Definition
Please don't overlook the awesome video into the above link.
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(getContext());
taskStackBuilder.addNextIntentWithParentStack(intent);
PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
backslashN
Updated on June 07, 2022Comments
-
backslashN almost 2 years
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.
I want to redirect the notification clicks to two different Activities (ApprovalDetailActivity and ConversationDetailActivity), based on their type.
I am using FCM for Push notifications. I am pasting my Manifest file and my FCMListener file here. Please help me out.
sendNotification() function in MyFirebaseMessagingService.java
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<>(); 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()); }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="mnet.mediaware.com.m_net"> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:name=".MnetApplication" android:allowBackup="true" android:icon="@drawable/mnet_icon" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <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"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".activities.MainActivity" android:configChanges="orientation|keyboardHidden|screenSize" android:label="@string/title_activity_main" android:launchMode="singleTask" android:theme="@style/AppTheme.NoActionBar" /> <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"> <intent-filter> <action android:name="mnet.mediaware.com.m_net.activities.ConversationDetailActivity" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> </intent-filter> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="mnet.mediaware.com.m_net.activities.MainActivity" /> </activity> <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"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="mnet.mediaware.com.m_net.activities.MainActivity" /> </activity> <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"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="mnet.mediaware.com.m_net.activities.MainActivity" /> </activity> <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"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="mnet.mediaware.com.m_net.activities.MainActivity" /> </activity> <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"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="mnet.mediaware.com.m_net.activities.MainActivity" /> </activity> <service android:name=".utils.firebase.MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <service android:name=".utils.firebase.MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> </application> </manifest>
-
backslashN over 7 yearsThe intent of Launcher Activity (LoginActivity) has the Extras. But, when I am launching the ConversationDetailActivity from this Activity (if the FCM extras are present), the startActivity() function does nothing. So, the ConversationDetailActivity is not opening up. What can be the reason ?
-
Aayush Thakur over 7 yearsHave you added this flag to the intent? intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
backslashN over 7 yearsYes I have tried this too, it didn't help. I have posted another question for this issue. Please answer here: link
-
Sira Lam almost 5 yearsThis is the answer; but the most important thing here is the data payload in the message will actually be in the launcher intent. So instead of parsing that data payload in the FirebaseMessasingService's onMessageReceived(), we should try to parse it in onNewIntent of the activity.
-
Aayush Thakur almost 5 years@SiraLam We can also use notifications from API/backend side just like postman. Which will be then available in FirebaseMessagingService Payload. So instead of using Firebase Console use the Firebase API in your backend or From Postman.
-
GiridharaSPK about 4 yearsSo Dont we have any other solution, still? Cant we navigate to a particular activity other than MainActivity by clicking on Notification?
-
GiridharaSPK about 4 yearsI have tried using this pendingIntent still, onclicking on push notification it is going to splash screen only. Any help? I want to go to a particular activity.