简体   繁体   English

如何创建多个PendingIntent?

[英]How can I create several PendingIntent?

How can I create several PendingIntent? 如何创建多个PendingIntent? I have several notifications, and when user pressing to the last one, everything is ok, but otherwise clicking on the notification is not responding. 我有几个通知,当用户按下最后一个时,一切正常,但是否则点击通知没有响应。 My code is: 我的代码是:

Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction("ShowDialog");
notificationIntent.putExtra("args", Tools.getInstance().generateBundle(progressId, lastId));
notificationBuilder.setContentIntent(PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT));

I suppose that the reason is FLAG_CANCEL_CURRENT, but all flags of PendingIntent cannot resolve my problem. 我想原因是FLAG_CANCEL_CURRENT,但是PendingIntent的所有标志都无法解决我的问题。

From my guess, 从我的猜测,

You just need to give different unique ID to each pending intent and there shall be no any problem in your application. 您只需要为每个待处理的意图提供不同的唯一ID,并且您的应用程序中不会出现任何问题。

Like, 喜欢,
PendingIntent.getActivity(context, uniqID, nIntent, PendingIntent.FLAG_ONE_SHOT);


If you are not planning to delete or update the intent then you can use calendar.getTimeInMillis() as your unique ID. 如果您不打算删除或更新意图,则可以使用calendar.getTimeInMillis()作为唯一ID。

Look at the CANCEL_CURRENT flag it does what you have described. 看看你所描述的CANCEL_CURRENT标志。 You have to create different pending intents for different notifications, otherwise they are considered the same. 您必须为不同的通知创建不同的待处理意图,否则它们被视为相同。 Look at the IntentSender documentation to find out what you need to do to create separate pending intents. 查看IntentSender文档 ,了解创建单独的待处理意图所需执行的操作。 My advice is to encode an id in the data. 我的建议是在数据中编码一个id。 It can be extremely simple if you like, or it can have a meaning to identify the intent. 如果您愿意,它可以非常简单,或者它可以具有识别意图的含义。 Here is the snippet of special interest: 以下是特别感兴趣的片段:

If the creating application later re-retrieves the same kind of IntentSender (same operation, same Intent action, data, categories, and components, and same flags), it will receive a IntentSender representing the same token if that is still valid. 如果创建应用程序稍后重新检索相同类型的IntentSender(相同的操作,相同的Intent操作,数据,类别和组件以及相同的标志),它将接收表示相同令牌的IntentSender,如果它仍然有效。

The problem you see is caused by the fact that you reuse the same pending intent, and you cancel the previous one when you "create" a new one. 您看到的问题是由于您重复使用相同的待处理意图,并在“创建”新意图时取消之前的意图。

Note also that according to the design guide lines you should only have one notification of each kind (for example only one notification for new e-mail, new sms, ...) If there are several notifications of the same kind they should be combined into one notification. 另请注意,根据设计指南,您应该只有一种通知(例如,只有一个通知用于新电子邮件,新短信,...)如果有多个相同类型的通知,则应将它们组合在一起一个通知。 However, it is only a guide line. 但是,它只是一个指导方针。

You can find info on why the "requestCode/uniqueId solution" works in the current version of Android here 您可以在此处找到有关“requestCode / uniqueId解决方案”在当前版本的Android中的工作原理的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM