19

我想创建几个启动活动(或刷新活动)以显示产品描述的通知。

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”额外发出多个通知?

4

1 回答 1

28

但它不能正常工作

是的,它确实。

如果我创建 2 个通知,它们都具有相同的“saleid”额外信息,这是最新的。

这正是文档所说的应该发生的事情。

如何使用不同的“saleid”额外发出多个通知?

选项 #1:在每个Intents. 这将使它们不同(从 的角度来看filterEquals())并将它们分开PendingIntents。但是,由于您在Intent( MainApplication.class) 中指定组件,因此该操作不会影响Intent路由的方式。

选项 #2:在您的调用中使用不同的requestCode(第二个参数) 。getActivity()虽然这被记录为“当前未使用”,但它确实会导致PendingIntent返回不同的对象。但是,由于此行为未记录在案,因此将来可能会发生变化。

于 2012-05-10T16:37:26.293 回答