我有一个告警接收器,它做一些检查,然后为每个检查创建一个 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);
}
我遇到的问题是,即使我创建了不同的意图(intent),它仍然总是调用具有相同额外信息(apikey和installid)的
Activity
。它总是选择第一个被创建的。
PendingIntent
,需要调用PendingIntent.getActivity()
并提供要么独特的requestCode 要么独特的Intent
。要使Intent
唯一,必须提供要么独特的ACTION、DATA或COMPONENT。因此,有几种方法可以解决这个问题。 - David Wasser