android - 在创建 android 通知时,在 PendingIntent 中使用 FLAG_UPDATE_CURRENT 时会重复 Intent 额外内容

标签 android android-intent notifications

我想创建多个通知来启动(或刷新) 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/

相关文章:

javascript - 在 android WebView 中的 java 和 javascript 之间共享一个对象(数据)

java - 如何将任何类型的文件从 Assets 文件移动/复制到 JAVA 中的范围存储 ANDROID Q?

android - 广播接收器 onReceive() 从未调用过

android - BroadcastReceiver未接收 Intent

Android Notification 重新启动应用程序但想要恢复

javascript - 无法将 websocket 连接到 service-worker

java - ImageView 未显示

android - 使用最新的Gradle版本3.2.1时找不到源Java类

java - 从非 Activity 类开始新 Activity

java - 如果触摸通知,通知就会消失