问题
Android app is running in the background state. Created a notification message in the Firebase Console with payload in the Advanced options. After receiving the notification and shown on the device's system tray, clicking on the notification just goes back to the app and the data payload was not delivered in the extras on the intent of the launcher activity. The notification does not open the app launcher by default as specified in the documentations. Why is it that the notification in the device's system tray is launching the Activity, but sometimes it just goes to the app as it was clicking on the app icon normally?
回答1:
What are you using in MainActivity, if you are sending the notification and the app is in foreground, then it will take you to that very specific page. But if your app is background, then clicking on the notification tray will just launch your app. Here, if you want that on clicking the notification it shouldd lead you to a particular page then you have to use this piece of Code in your MainActivity.
if (getIntent().getExtras() != null) {
Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
startActivity(intent);
}
回答2:
Check if your launcher activity has android:launchMode="singleTop" in the manifest and you are trying to read the extras in the onCreate() method.
If that is your case, only 1 instance of that class will be created, so if the activity is in the background when the user touches a notification in the tray, the method that is called is onNewIntent(). In that method you can get the extras and put them in the general intent this way:
@Override
public void onNewIntent(Intent intent){
super.onNewIntent(intent);
setIntent(intent);
}
and then onResume() you can access the extras with getIntent().getExtras() .
Happy coding!
回答3:
You will get all the data as a bundle in your launcher activity when your app is in background. To get those values, Make Sure to override onNewIntent in the launcher activity. In my case, it was the splash screen.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getExtras() != null) {
for (String key : intent.getExtras().keySet()) {
Object value = intent.getExtras().get(key);
Log.d("data ", "Key: " + key + " Value: " + value);
}
}
}
来源:https://stackoverflow.com/questions/41756394/fcm-notification-with-payload-in-devices-system-tray-not-getting-passed-to-app