PendingIntent 对于第一个通知工作正常,但对于其余通知工作不正确
protected void displayNotification(String response) {
Intent intent = new Intent(context, testActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
Notification notification = new Notification(R.drawable.icon, "Upload Started", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Upload", response, pendingIntent);
nManager.notify((int)System.currentTimeMillis(), notification);
}
该函数将被多次调用。我希望每个通知
在单击时启动testActivity。不幸的是,只有第一个通知启动 testActivity。单击其余部分会使通知窗口最小化。
额外信息:函数 displayNotification()
位于名为 UploadManager
的类中。 Context
从实例化的 activity
传递到 UploadManager
中。函数 displayNotification()
会从在 AsyncTask
中运行的 UploadManager 中的函数多次调用。
编辑 1:我忘了提及我将字符串响应作为额外
传递到Intent意图
中。
protected void displayNotification(String response) {
Intent intent = new Intent(context, testActivity.class);
intent.putExtra("response", response);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
这有很大的不同,因为我需要额外的“响应”来反映创建通知时的字符串响应。相反,使用 PendingIntent.FLAG_UPDATE_CURRENT
,额外的“响应”反映了上次调用 displayNotification()
时的字符串响应。
我通过阅读 FLAG_UPDATE_CURRENT
上的文档知道了为什么会这样。但是,我目前不确定如何解决它。
protected void displayNotification(String response) {
Intent intent = new Intent(context, testActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
Notification notification = new Notification(R.drawable.icon, "Upload Started", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Upload", response, pendingIntent);
nManager.notify((int)System.currentTimeMillis(), notification);
}
This function will be called multiple times. I would like for each notification
to launch testActivity when clicked. Unfortunately, only the first notification launches testActivity. Clicking on the rest cause the notification window to minimize.
Extra information: Function displayNotification()
is in a class called UploadManager
. Context
is passed into UploadManager
from the activity
that instantiates. Function displayNotification()
is called multiple times from a function, also in UploadManager, that is running in an AsyncTask
.
Edit 1: I forgot to mention that I am passing String response into Intent intent
as an extra
.
protected void displayNotification(String response) {
Intent intent = new Intent(context, testActivity.class);
intent.putExtra("response", response);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
This makes a big difference because I need the extra "response" to reflect what String response was when the notification was created. Instead, using PendingIntent.FLAG_UPDATE_CURRENT
, the extra "response" reflects what String response was on the last call to displayNotification()
.
I know why this is from reading the documentation on FLAG_UPDATE_CURRENT
. However, I am not sure how to work around it at the moment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我遇到了同样的问题,并且能够通过将标志更改为来解决它:
I had the same problem, and was able to fix it by changing the flag to:
为了更加正确地发送数据,您应该发送带有挂起意图的通知 ID,如下所示:
PendingIntentendingIntent = PendingIntent.getActivity(context,(int)System.currentTimeMillis(),intent,PendingIntent.FLAG_UPDATE_CURRENT);
to send data extra correctly you should send with pending intent the notification id like this:
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
我有同样的问题,并使用 PendingIntent.html.FLAG_UPDATE_CURRENT 来修复它。
我检查了源代码。在 ActivityManagerService 中.java,关键方法如下。当标志为 PendingIntent.FLAG_UPDATE_CURRENT 并且 updateCurrent 为 true 时。一些额外的将被新的替换,我们将得到替换的 PendingIntent。
I have the same problem, and use the PendingIntent.html.FLAG_UPDATE_CURRENT to fix it.
I have checked the source code. In ActivityManagerService.java, the key method is as follows. When the flag is PendingIntent.FLAG_UPDATE_CURRENT and the updateCurrent is true. Some extras will be replaced by new and we will get an replaced PendingIntent.
在创建 PendingIntent 对象时,我们调用 PendingIntent.getActivity(mContext, requestCode, Intent, PendingIntent.FLAG_UPDATE_CURRENT);
因此,这里使用下面的代码来生成 PendingIntent 并将此意图发送给通知。因此,当您收到另一个通知时,该通知将具有唯一的 requestCode 来仅获取该通知的数据。
While create the PendingIntent object we call the PendingIntent.getActivity(mContext, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
So here use the below code to generate the PendingIntent and send this intent to notification. So when you get another notification that will have unique requestCode to get the data of that notification only.
PendingIntent 中有两个 int 参数,第二个和最后一个。第二个是“请求代码”,它必须是唯一的数字(例如通知的 ID),否则如果(如您的示例中它等于零,它总是会被覆盖)。
In PendingIntent is two int parameters, second one and the last one. Second one is "request code" and it must be unicue number (for example id of your notifiction), else if (as in your example it equals zero, it always will be overwritten).
我遇到了同样的问题,我通过以下步骤修复了它
1)清除意图的任何标志
2)通过以下代码插入intent.setAction
3)对于 Pendingintent,插入以下代码
我希望与您合作
I had the same problem and i fixed it by the below steps
1) Clear any flag for intent
2) insert intent.setAction by the below code
3) for Pendingintent ,insert the below code
I hope to work with you
Fwiw,我使用
PendingIntent.FLAG_CANCEL_CURRENT
的运气比使用PendingIntent.FLAG_UPDATE_CURRENT
的运气要好。Fwiw, I have had better luck with
PendingIntent.FLAG_CANCEL_CURRENT
than withPendingIntent.FLAG_UPDATE_CURRENT
.正如文档所说,使用唯一的请求代码:
As documentation said use unique request code:
我遇到了同样的问题,并且能够通过将标志更改为来解决它:
I had the same problem, and was able to fix it by changing the flag to:
我看到答案但没有解释。此外,没有一个答案能够解决所有可能的解决方案,所以我会尽力澄清这一点。
文档:
问题原因:
您创建了 2 个带有 2 个待处理意图的通知。每个待处理的意图都与一个意图相关联:
但是,这两个意图是相同的,因此当您的第二个通知到达时,它将启动第一个意图。
解决方案:
您必须使每个意图都是唯一的,以便所有待处理的意图都不会相同。如何使意图独一无二?不是通过使用
putExtra()
添加的额外内容。即使附加内容不同,意图可能仍然相同。要使每个意图唯一,您必须为意图操作、数据、类型、类、类别或请求代码设置唯一值:(其中任何一个都可以)intent.setAction(. ..)
intent.setData(...)
intent.setType(...)
intent.setClass( ...)
intent.addCategory(...)
PendingIntent.getActivity(context, YOUR_UNIQUE_CODE, Intent, Intent.FLAG_ONE_SHOT);
注意:设置唯一的请求代码可能会很棘手,因为您需要一个 int,而
System.currentTimeMillis()
返回 long,这意味着一些数字将被删除。因此,我建议使用类别或操作并设置一个唯一的字符串。I see answers but no explanations. Also none of the answers address all possible solutions, so I'll try to make that clear.
Documentation:
Cause of the problem:
You create 2 notifications with 2 pending intents. Each pending intent is associated with an intent:
However, these 2 intents are equal, therefore when your 2nd notification arrives it will launch the first intent.
Solution:
You have to make each intent unique, so that no pending intents will ever be equal. How do you make the intents unique? Not by the extras you put with
putExtra()
. Even if the extras are different, the intents might still be equal. To make each intent unique, you must set a unique value to the intent action, or data, or type, or class, or category, or request code: (any of those will work)intent.setAction(...)
intent.setData(...)
intent.setType(...)
intent.setClass(...)
intent.addCategory(...)
PendingIntent.getActivity(context, YOUR_UNIQUE_CODE, intent, Intent.FLAG_ONE_SHOT);
Note: Setting a unique request code might be tricky because you need an int, while
System.currentTimeMillis()
returns long, which means that some digits will be removed. Therefore I would recommend to either go with the category or the action and setting a unique string.设置行动为我解决了这个问题。这是我对这种情况的理解:
我有多个小部件,每个小部件都附加了一个 PendingIntent。每当一个更新时,它们都会更新。标志用于描述与完全相同的 PendingIntents 发生的情况。
FLAG_UPDATE_CURRENT 描述现在读起来好多了:
如果您正在创建的相同 PendingIntent 已经存在,则将所有旧的更新为您正在创建的新 PendingIntent。
完全相同的定义着眼于整个 PendingIntent 除了额外的内容。因此,即使你对每个意图有不同的附加功能(对我来说,我添加了 appWidgetId),然后到 android,它们是相同的。
添加 .setAction 和一些虚拟的唯一字符串告诉操作系统。这些是完全不同的,并且不会更新任何内容。最后,这是我的实现,它按我想要的方式工作,其中每个小部件都附加了自己的配置意图:
更新
如果您正在使用广播,则更好的解决方案。唯一的 PendingIntents 也由唯一的请求代码定义。这是我的解决方案:
Set Action Solved this for me. Here's my understanding of the situation:
I have multiple widgets that have a PendingIntent attached to each. Whenever one got updated they all got updated. The Flags are there to describe what happens with PendingIntents that are exactly the same.
FLAG_UPDATE_CURRENT description reads much better now:
If the same PendingIntent you are making already exists, then update all the old ones to the new PendingIntent you are making.
The definition of exactly the same looks at the whole PendingIntent EXCEPT the extras. Thus even if you have different extras on each intent (for me I was adding the appWidgetId) then to android, they're the same.
Adding .setAction with some dummy unique string tells the OS. These are completely different and don't update anything. In the end here's my implementation which works as I wanted, where each Widget has its own configuration Intent attached:
UPDATE
Even better solution in case you're working with broadcasts. Unique PendingIntents are also defined by unique request codes. Here's my solution:
正在努力处理
RemoteViews
和HomeScreen
小部件上每个Button
的几个不同的Intents
。添加这些后起作用:
1.
intent.setAction(Long.toString(System.currentTimeMillis()));
2.
PendingIntent .FLAG_UPDATE_CURRENT
Was struggling with
RemoteViews
and several differentIntents
for eachButton
onHomeScreen
Widget.Worked when added these:
1.
intent.setAction(Long.toString(System.currentTimeMillis()));
2.
PendingIntent.FLAG_UPDATE_CURRENT
不要将
Intent.FLAG_ACTIVITY_NEW_TASK
用于 PendingIntent.getActivity,请使用 FLAG_ONE_SHOT 而是从评论中复制:
然后在 Intent 上设置一些虚拟操作,否则多余的内容会被丢弃。例如
Don't use
Intent.FLAG_ACTIVITY_NEW_TASK
for PendingIntent.getActivity, use FLAG_ONE_SHOT insteadCopied from comments:
Then set some dummy action on the Intent, otherwise extras are dropped. For example