I have an alarm receiver that does some checks and then creates a Notification
for each check. This means it can create more than one Notification
. This all works fine. However, I have an Intent
connected to the notification to start an activity when the notification is tapped.
This is the notification code:
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());
The method that creates the PendingIntent
is:
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);
}
Problem I have, is that even I create a different intent, it still always calls the Activity
with the same extras (apikey and installid). It always takes the first one created.
It seems that the problem is caused by the arguments you passed to the getActivity
method.
Try the following code
PendingIntent.getActivity(context, <unique_value_per_every_call>, intent, 0);
The second argument is requestCode
so it should be unique.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With