在创建 android 通知时在 PendingIntent 中使用 FLAG_UPDATE_CURRENT 时,意图附加项会重复 [英] intent extras are duplicated when using FLAG_UPDATE_CURRENT in PendingIntent when creating android notifications

查看:14
本文介绍了在创建 android 通知时在 PendingIntent 中使用 FLAG_UPDATE_CURRENT 时,意图附加项会重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create several notifications that launches an activity (or refresh it) to display a product description.

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.

When I create the PendingIntent, I have 4 choices : FLAG_CANCEL_CURRENT, FLAG_NO_CREATE, FLAG_ONE_SHOT and FLAG_UPDATE_CURRENT.

最后一个的定义(http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT) 是我想要做的,但它不能正常工作.如果我创建 2 个通知,它们都具有相同的saleid"额外信息,这是最新的.如何使用不同的saleid"额外发送多个通知?

The definition of the last one (http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT) is what I want to do 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. How can I make more than one notification with different 'saleid' extra?

推荐答案

但它不能正常工作

but it doesn't work as it should

是的,确实如此.

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

If I create 2 notifications, they both have the same 'saleid' extra which is the latest one.

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

This is precisely what the documentation says is supposed to happen.

如何使用不同的saleid"额外发送多个通知?

How can I make more than one notification with differents 'saleid' extra?

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

Option #1: Put a different action string in each of your Intents. This will make them different (from the standpoint of filterEquals()) and give them separate PendingIntents. However, since you are specifying the component in the Intent (MainApplication.class), the action will not affect how the Intent is routed.

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

Option #2: Use a different requestCode (2nd parameter) on your getActivity() calls. While this is documented as "currently not used", it does result in different PendingIntent objects being returned. However, since this behavior is undocumented, it may change in the future.

这篇关于在创建 android 通知时在 PendingIntent 中使用 FLAG_UPDATE_CURRENT 时,意图附加项会重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆