android - 多个通知的未决意向

标签 android notifications android-pendingintent

我有发送通知的应用程序,我将此代码用于未决 Intent

  Intent myIntent = new Intent(getApplicationContext(),MainActivity.class);
  myIntent.putExtra("link",Link);

  PendingIntent intent2 =  
  PendingIntent.getActivity(getApplicationContext(),1,myIntent, 
  PendingIntent.FLAG_ONE_SHOT);

它第一次运行良好,但我在每 1 分钟调用一次的方法中使用了这段代码。

问题是 link 变量从一个变为另一个。

当我在 MainActivity 中获取数据时,我只找到了最后一个链接,所有创建的通知都有最后一个链接。

我不知道如何避免这种情况。

最佳答案

您正在为未决 Intent 发送相同的请求代码。这些代码被定义为您的待定 Intent 声明的第二个参数

改变

PendingIntent intent2 =  
  PendingIntent.getActivity(getApplicationContext(),1,myIntent, 
  PendingIntent.FLAG_ONE_SHOT);

收件人:

PendingIntent intent2 =  
  PendingIntent.getActivity(getApplicationContext(),UNIQUE_INT_VALUE_FOR_EVERY_CALL,myIntent, 
  PendingIntent.FLAG_ONE_SHOT);

如果你使用相同的id,intent会被重用,你只会得到最后的数据,而不是每次调用都得到新的数据。

关于android - 多个通知的未决意向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45564356/

相关文章:

android - getParcelableExtra 在 Pending Intent 上总是 null,但不是 Intent

android - 可以像 RadioGroup 一样将 CheckedTextView 放入 Group 中吗?

android - 如何从自身中删除 fragment

仅 Android 状态栏图标

iphone - 我们可以使用phonegap框架在iphone中进行推送通知吗?

Android - PendingIntent.getActivity 中的上下文参数

android - Pending Intent 请求代码可以为负数吗?

安卓工作室 : Layout Preview gives "native typeface cannot be made"

安卓 P : `NotificationManager.cancel` does not work if user has replied to notification

android - Activity 不是从状态栏通知开始的