通知PendingIntent另一个通知将覆盖Intent额外内容 [英] Notification PendingIntent Intent extras are overridden by another notification
问题描述
使用新的PendingIntent创建新通知时,其意图中的附加功能将覆盖以前通知中的任何PendingIntent Intent附加功能.
When creating a new notification with a new PendingIntent, the extras in its intent override any previous notification's PendingIntent Intent extras.
例如,假设我使用PendingIntent1创建Notification1,其中包含Intent1及其附加功能.
For example, lets say I create Notification1 with PendingIntent1, which has Intent1 and its extras.
当我使用PendingIntent2创建Notification2时,它的Intent2具有自己的不同附加功能,Intent1现在将具有与Intent2相同的附加功能.为什么会这样呢?我该如何解决?
When I create Notification2 with PendingIntent2, which has Intent2 with its own different extras, Intent1 will now have the same extras as Intent2. Why is this happening? How do I work around this?
推荐答案
有两种解决方法:
一种是对Intent设置不同的操作.因此,在您的示例中,您可以设置Intent1.setAction("Intent1")
和Intent2.setAction("Intent2")
.由于操作不同,因此Android不会覆盖意图上的其他功能.
One is to set a different action on the Intent. So in your example, you could set Intent1.setAction("Intent1")
and Intent2.setAction("Intent2")
. Since the action is different, Android will not override the extras on the intent.
但是,在某些情况下,您实际上可能需要为此目的设置特定的操作(即您的操作对应于特定的广播接收器).在这种情况下,最好的做法是在每个PendingIntent中将请求代码设置为不同的内容:
However, there may be a case where you actually need to set a specific action on this intent (i.e. your action corresponds to a specific broadcast receiver). In this case, the best thing to do is set the request code to something different in each PendingIntent:
PendingIntent pendingIntent1 = PendingIntent.getActivity(context,
(int) System.currentTimeMillis() /* requestCode */, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context,
(int) System.currentTimeMillis() /* requestCode */, intent2,
PendingIntent.FLAG_UPDATE_CURRENT);
通过在PendingIntent上设置新的请求代码,Android不会覆盖其对应的每个intent上的额外功能.
By setting a new request code on the PendingIntent, Android will not override the extras on each of their corresponding intents.
这篇关于通知PendingIntent另一个通知将覆盖Intent额外内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!