问题
I have a problem intent.putExtra
is not working when the app is killed. Wake up and running mode all works. I use firebase messaging service, getData()
method, pending intent.
private PendingIntent getPendingIntent(int newsID) {
Intent intent = new Intent(this, DetailedNewsActivity.class);
intent.putExtra("newsID", newsID);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
// All the parents of SecondActivity will be added to task stack.
stackBuilder.addNextIntentWithParentStack(intent);
//PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
回答1:
I guess your onMessageReceived() function is not getting triggered. There are two types of notifications from firebase data messages and other one is notification message. Currently you are getting notification message which doesn't invoke onMessageReceived() in killed state. Use "data" messages instead.This could be done by the back end developer.Please ask him to send data messages. Notification messages trigger onMessageRecieved() only when app is in foreground but data message invoke onMessageRecieved() in both foreground and background state
Please replace "notification" key from payload to "data" key. Please refer to this link Click for more information
回答2:
Check that data first then use that
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
try {
JSONObject json = new JSONObject("{\"data\":"+remoteMessage.getData().toString()+"}");
//decode your json data and use that
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
String stingMessage = remoteMessage.getNotification().getBody();
//show the message string as notification or as your want
}
To show notification and add pending intent
Intent myintent = new Intent(this, YourActivityWantToL.class);
myintent.putExtra("message", stingMessage);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(TAG)
.setStyle(new NotificationCompat.BigTextStyle() .bigText(msg))
.setContentText(stingMessage);
mBuilder.setContentIntent(contentIntent);
but if you are targeting and running app on 26 or higher you have to use notification channel
来源:https://stackoverflow.com/questions/59764322/killed-app-notification-click-intent-putextra-method-not-working