我想创建多个通知来启动(或刷新) Activity 以显示产品描述。
Notification notification = new Notification(R.drawable.applicationicon,
Resources.getString("NewSaleNotification", context),
System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, MainApplication.class);
intent.putExtra("saleid", saleid);
// to be sure the activity won't be restarted
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, SaleTitle, SaleMessage, pendingIntent);
notificationManager.notify(saleid, notification);
当我创建 PendingIntent 时,我有 4 个选择:FLAG_CANCEL_CURRENT、FLAG_NO_CREATE、FLAG_ONE_SHOT 和 FLAG_UPDATE_CURRENT。
最后一个( http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT )的定义是我想要做的,但它不能正常工作。如果我创建 2 个通知,它们都具有相同的“saleid”额外信息,这是最新的。如何使用不同的“saleid”额外发出多个通知?
最佳答案
but it doesn't work as it should
是的,确实如此。
If I create 2 notifications, they both have the same 'saleid' extra which is the latest one.
这正是the documentation说应该发生。
How can I make more than one notification with differents 'saleid' extra?
选项 #1:在每个 Intents
中放置不同的操作字符串。这将使它们不同(从 filterEquals()
的角度来看)并赋予它们单独的 PendingIntents
。但是,由于您在 Intent
(MainApplication.class
) 中指定组件,因此该操作不会影响 Intent
的路由方式。
选项 #2:在您的 getActivity()
调用中使用不同的 requestCode
(第二个参数)。虽然这被记录为“当前未使用”,但它确实会导致返回不同的 PendingIntent
对象。但是,由于这种行为没有记录在案,因此将来可能会发生变化。
关于android - 在创建 android 通知时,在 PendingIntent 中使用 FLAG_UPDATE_CURRENT 时会重复 Intent 额外内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10537006/