【发布时间】:2019-02-14 21:36:24
【问题描述】:
我有一个警报接收器,它会进行一些检查,然后为每次检查创建一个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);
}
我遇到的问题是,即使我创建了不同的意图,它仍然总是使用相同的附加项(apikey 和 installid)调用Activity。它总是取第一个创建的。
【问题讨论】:
标签: android notifications android-pendingintent