public static void showNotification(Context ctx, int value1, String title, String message, int value2){
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(ctx, ActivityMain.class);
int not_id = Utils.randInt(1111, 9999);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationIntent.putExtra("key_1", value1);
notificationIntent.putExtra("key_2", value2);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx.getApplicationContext());
stackBuilder.addNextIntent(notificationIntent);
PendingIntent notifPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notificationManager.notify(not_id, createNotification(ctx, notifPendingIntent, title, message));
}
这是我发送和显示的代码notification
。我从服务发送这个。正如您所看到的,我发送了 2 个额外的值(key_1
and key_2
);当我单击通知时,我应该打开活动并查看value1
and value2
的按键。
问题是:如果在我打开其中任何一个之前收到另一个通知,value1
and value2
被覆盖。例如:
-
第一个通知发送foo
and bar
第二个通知发送faz
and baz
第三次通知发送fubar
and fobaz
所以我会在栏中收到 3 条通知。现在,无论我单击什么通知,第一个、第二个、第三个,我都会看到最后一个发送的值fubar
and fobaz
。
我想要的是当我单击特定通知时,活动显示该通知发送的值。
任何帮助将不胜感激。谢谢。
.
这是因为您正在使用FLAG_UPDATE_CURRENT
:
标志指示如果所描述的 PendingIntent 已经存在,则保留它,但用这个新 Intent 中的内容替换其额外数据。
这就是取代你的extra
数据与最后一个意图的数据。
相反,在这里为 PendingIntent 的默认行为传递 0:
PendingIntent notifPendingIntent = stackBuilder.getPendingIntent(0, 0);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)