在创建Android通知时,在PendingIntent中使用FLAG_UPDATE_CURRENT时会复制intent附加内容

时间:2012-05-10 15:11:02

标签: android android-intent notifications

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

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'额外制作多个通知?

1 个答案:

答案 0 :(得分:27)

  

但它不能正常工作

是的,确实如此。

  

如果我创建2个通知,它们都具有相同的'saleid'额外费用,这是最新的通知。

这正是the documentation所说的应该发生的事情。

  

如何使用不同的'saleid'额外制作多个通知?

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

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

相关问题