android - 通知中的意图不会改变
问题描述
我有一个警报接收器,它会进行一些检查,然后Notification
为每次检查创建一个。这意味着它可以创建多个Notification
. 这一切都很好。但是,当点击通知时,我已Intent
连接到通知以启动活动。
这是通知代码:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_OUTPUT_LEVELS)
.setSmallIcon(icon)
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(longmessage))
.setContentIntent(getPendingIntent(solarEdge, reason))
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
// notificationId is a unique int for each notification that you must define
// we use the installation's ID to make sure all notifications get sent
notificationManager.notify(solarEdge.getInfo().getId(), mBuilder.build());
创建的方法PendingIntent
是:
private PendingIntent getPendingIntent(SolarEdge solarEdge, int reason) {
String apikey = solarEdge.getApikey();
int installationId = solarEdge.getInfo().getId();
Intent intent = new Intent(context, InstallationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(EXTRA_API_KEY, apikey);
intent.putExtra(EXTRA_INSTALLATION_ID, installationId);
intent.putExtra(EXTRA_REASON, reason);
return PendingIntent.getActivity(context, 0, intent, 0);
}
我遇到的问题是,即使我创建了不同的意图,它仍然总是Activity
使用相同的附加功能(apikey 和 installid)调用 。它总是需要创建的第一个。
解决方案
似乎问题是由您传递给该getActivity
方法的参数引起的。
试试下面的代码
PendingIntent.getActivity(context, <unique_value_per_every_call>, intent, 0);
第二个论点是requestCode
它应该是唯一的。
推荐阅读
- php - 如何在 MYSQL 的同一 INSERT QUERY 的另一列中使用自动增量 ID?
- javascript -
HTML 标题中的标记会破坏 jQuery DataTables 中的 pdfmake 导出 - machine-learning - 有什么方法可以限制用openblas编译的pycaffe的CPU使用率?
- java - Java SHA1withRSA 验证总是假的,为什么?
- flutter - 如何在颤振测试中打开抽屉
- php - 如何使用服务器 IP 而不是用户 IP 将用户重定向到 API 端点(不同的域)?
- ruby-on-rails - Capybara::ElementNotFound: 找不到 css "#transaction_form"
- c# - 获取用户注册的来源/引荐来源网址
- oop - 在 OOP 中封装集合的顺序
- php - PHPMailer 链接问题