【问题标题】:Android old notifications are invalid by a new oneAndroid旧通知对新通知无效
【发布时间】:2015-05-28 19:21:23
【问题描述】:

我正在开发一个 Android 应用程序。它接收通知 A,并在用户单击通知时显示内容(与 A 相关)。问题是:如果用户在收到多个通知时单击其中一个旧通知,它会显示与最新通知相关的内容,而不是它本身。比如我收到A、B、C、D(A是最新的)通知后,如果我点击通知A,它显示内容A,但如果我点击B(或C或D),它仍然显示内容A。

我用来推送通知的代码如下:

Intent intent = new Intent(this, MainActivity.class);// start the main activity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(MainActivity.EXTRA_TITLE, title);
intent.putExtra(MainActivity.EXTRA_URL, url);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
  .setWhen(System.currentTimeMillis())
  .setTicker(title)
  .setContentTitle(title)
  .setContentText(beacon.getDetail())
  .setAutoCancel(true)
  .setOngoing(false) // if it is true, the user cannot dismiss it
  .setSmallIcon(R.drawable.ic_launcher)
  .setOnlyAlertOnce(true)
  .setDefaults(Notification.DEFAULT_ALL)
  .setLights(0xff00ff00, 1000, 300)
  .setSound(Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "6"))
  .setVibrate(new long[] {
    0, 100, 200, 300
  })
  //.addAction(0,"Cancel", dismissIntent)
  //.addAction(0,"Open App", pi)
  .build();
notification.contentIntent = pi;
notificationManager.notify(id, notification);

我不确定这是否是 Android 上的正常情况。如果不是,任何人都可以帮我解决它吗?如果是正常情况,谁能建议我实现上述要求的解决方案?提前谢谢你。

编辑: 十分感谢大家。在您的帮助下,我发现了问题所在。根据PendingIntent doc,如果两个意图除了它们的“额外”之外是相同的,并且它们关联的挂起意图也相同,那么“两个”挂起意图实际上只是一个挂起意图,因为旧的挂起意图在创建时被重用新的待定意图。为了避免这种情况(实现我的要求),我需要通过修改它们的操作、数据、类型、类和类别来确保两个意图不相同,或者通过修改它们的请求来确保两个待处理的意图不同代码。就我而言,我选择了第二种解决方案:

PendingIntent pi = PendingIntent.getActivity(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

我将通知的 id 分配给其对应的未决意图的请求代码。尽管intents仍然是相同的(filterEquals),但是它们对应的pendingintents是不同的,因为它们有不同的请求码。不同通知的id不一样,我想展示不同的内容。

通过实验,它可以正常工作。

【问题讨论】:

    标签: android notifications android-pendingintent


    【解决方案1】:

    来自PendingIntent docs

    知道两个 Intent 何时被认为是相同的很重要 用于检索 PendingIntent。人们常犯的错误 make 是创建多个带有 Intent 的 PendingIntent 对象 只是它们的“额外”内容有所不同,期望得到不同的 PendingIntent 每次。这不会发生。意图的部分 用于匹配的与定义的相同 Intent.filterEquals。如果您使用两个等效的 Intent 对象 根据 Intent.filterEquals,那么您将获得相同的 PendingIntent 对他们俩来说。

    有两种典型的处理方法。

    如果您确实需要多个不同的 PendingIntent 对象在 同时(例如用作两个都显示的通知 同时),那么你需要确保有一些东西 他们的不同之处在于将它们与不同的 待定意图。这可能是考虑的任何 Intent 属性 Intent.filterEquals,或提供给的不同请求代码整数 getActivity(上下文,int,Intent,int),getActivity(上下文,int, Intent[], int), getBroadcast(Context, int, Intent, int), 或 getService(Context, int, Intent, int).

    【讨论】:

    • 感谢您的回答。你解决了我的问题。受 pendingIntent 文档的启发,我为我的问题添加了一个可行的答案。
    【解决方案2】:

    试试这个,它将取代旧的通知

    notificationManager.notify(id, notification); <---make id the same integer for all notifications
    

    【讨论】:

    • 感谢您的回答。如果通知的 id 相同,则旧通知将被新通知替换,并且只显示一个通知。我想要的是同时显示多个通知。因此用户可以选择旧通知。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多