多个未决意图?

Multiple Pending Intents?

我的 activity 创建了几个通知。

这是我目前在不同的 resultIntents 上的做法:

PendingIntent resultPendingIntent =
  PendingIntent.getActivity(
    context,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
  );

现在,由于旗帜是强制性的,我不得不从 the four flags select。如果我想让它们都独立工作,并且较新的通知不受前一个通知的影响,我该怎么办。

在此处找到解决方案:here

您必须对唯一值的意图使用 setAction,这样就不会有匹配的 PendingIntents

这是我使用的:

setAction(Long.toString(System.currentTimeMillis()))

来自官方文档

If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent.filterEquals, or different request code integers supplied to getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).

Intent.filterEquals 文档:

Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

如果您需要无限量的 PendingIntents,我建议使用 Intent.setData 来获得某种唯一值。如果您可以将它们分组,请使用 setTypeaddCategory 来减少所需的唯一 PendingIntents 的数量。