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

Ale*_*xis 19 notifications android android-intent

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

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);
Run Code Online (Sandbox Code Playgroud)

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

Com*_*are 27

但它不能正常工作

是的,它确实.

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

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

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

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

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

  • 选项#2绝对是我正在寻找的解决方案,在某种程度上我很想绕过Stack Overflow,并指出每个人都有这个答案.在选择时,让"不同"通知将相同的额外(最新)通知给他们的活动是非常令人沮丧的.这解决了它.目前. (3认同)