我想创建多个通知,用于启动(或刷新)活动以显示产品描述。
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)的定义是我想要做的事情,但它不能正常工作。如果我创建两个通知,它们都有相同的'saleid‘额外的,这是最新的。如何使用不同的'saleid‘额外发出多个通知?
发布于 2012-05-11 00:37:26
,但它不能正常工作
是的,确实如此。
如果我创建两个通知,它们都有相同的'saleid‘额外的,这是最新的一个。
这正是the documentation所说的应该发生的事情。
如何使用不同的'saleid‘额外发出多个通知?
选项#1:在每个Intents
中放置不同的操作字符串。这将使它们不同(从filterEquals()
的角度来看),并为它们提供独立的PendingIntents
。但是,由于您是在Intent
(MainApplication.class
)中指定组件,因此该操作不会影响Intent
的路由方式。
选项#2:在getActivity()
调用中使用不同的requestCode
(第二个参数)。虽然这被记录为“当前未使用”,但它确实会导致返回不同的PendingIntent
对象。但是,由于此行为未记录在案,因此将来可能会发生变化。
https://stackoverflow.com/questions/10537006
复制相似问题