简体   繁体   中英

How can I create several PendingIntent?

How can I create several 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.

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.

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.

Look at the CANCEL_CURRENT flag it does what you have described. 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. My advice is to encode an id in the data. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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