问题
i have tried almost every solution posted here and combination of every flag but it is not working.
Following are use cases in which i am having problems.
1) When i am in application FCM notification opens my desired activity.
Data is passed to onNewIntent
in main activity. It works fine when application is foreground.
2) When in background mode(presssing home button) after clicking on notification it launches new instance of my application even though i have specified android:launchMode="singleTop"
in manifest file to my main activity but i don't know what is happening here. Desired Activity i want to open here is RateEventActivity
the strange thing happening here is that NotificationActivity
and ProfileActivity
are working perfectly even when application is in background mode but RateEventActivity
is producing all the problem as i described above. Most of application code is written by someone else i am working on RateEevent
module so i don't know what i am missing here?
Intents are lost somewhere (not passed to mainActivty
on create) when i am clicking notification from background mode in RateEventActivity
scenario.
i have also tried passing unique id to my pending intent but no luck. I have put lot of time and effort to solve this but failing every time
here is my FCM code
private void createNotification(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Log.v("Notification Title", remoteMessage.getNotification().getTitle());
Intent intent = new Intent(this, MainActivity.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (remoteMessage.getData().containsKey("notificationUser")) {
intent.putExtra("notificationUser", remoteMessage.getData().get("notificationUser"));
} else if (remoteMessage.getData().containsKey("notificationId")) {
intent.putExtra("notificationId", remoteMessage.getData().get("notificationId"));
} else if (remoteMessage.getData().containsKey("event")) {
if (remoteMessage.getNotification().getTitle().equalsIgnoreCase("Event Feedback")) {
Log.v("Notification Event", remoteMessage.getData().get("event"));
intent.putExtra("eventId", remoteMessage.getData().get("event"));
}
}
//PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent contentIntent = PendingIntent.getActivity(this, 0 , intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(largeIcon)
.setContentTitle(notification.getTitle())
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notification.getBody()))
.setContentText(notification.getBody())
.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
Main Activity Code onCreate
Method
if (getIntent().hasExtra("notificationId")) {
Intent intent = new Intent(this, NotificationActivity.class);
intent.putExtra("notificationId", getIntent().getStringExtra("notificationId"));
startActivity(intent);
} else if (getIntent().hasExtra("user")) {
Intent intent = new Intent(this, ProfileActivity.class);
intent.putExtra("userId", getIntent().getStringExtra("user"));
startActivity(intent);
} else if (getIntent().hasExtra("eventId")) {
Intent intent = new Intent(this, RateEventActivity.class);
intent.putExtra("eventId", getIntent().getStringExtra("eventId"));
startActivity(intent);
}
My Manifest File
<activity
android:name=".activities.MainActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/HomeTheme" />
<activity
android:name=".activities.ProfileActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="Profile"
android:screenOrientation="portrait" />
<activity
android:name=".activities.NotificationActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="Notifications"
android:screenOrientation="portrait" />
<activity
android:name=".activities.RateEventActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="Rate Users"
android:screenOrientation="portrait"
/>
回答1:
If you want to open your app and perform a specific action [while backgrounded], set click_action in the notification payload and map it to an intent filter in the Activity you want to launch. For example, set click_action to OPEN_ACTIVITY_1 to trigger an intent filter like the following:
As suggested in react-native-fcm docs,ask backend to send json data in the form like this,
{
"to":"some_device_token",
"content_available": true,
"notification": {
"title": "hello",
"body": "yo",
"click_action": "OPEN_ACTIVITY_1" // for intent filter in your activity
},
"data": {
"extra":"juice"
}
}
and in your mainfest file add intent-filter for your activity as below
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
When you click the notification, it will open the app and go straight to activity that you define in click_action, in this case "OPEN_ACTIVTY_1". And inside that activity you can get the data by :
Bundle b = getIntent().getExtras();// add these lines of code to get data from notification
String someData = b.getString("someData");
check out below links for more helps:
Firebase FCM notifications click_action payload
Firebase onMessageReceived not called when app in background
Firebase console: How to specify click_action for notifications
回答2:
i got this issue long ago. let me check my project code.
Ahh its here.
Try with setting flags
Intent intent = new Intent(this, MainActivity.class));
// set intent so it does not start a new activity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
and change your PendingIntent
like below
PendingIntent contentIntent = PendingIntent.getActivity(this, ,
intent, 0);
Also remove
android:launchMode="singleTop"
from your MainActivity from Manifest file
try this and let see it works.
来源:https://stackoverflow.com/questions/41296637/fcm-notification-onclick-does-not-open-desired-activity