Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent in Notification does not change

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.

like image 884
Bart Friederichs Avatar asked Feb 14 '19 21:02

Bart Friederichs


1 Answers

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.

like image 57
CROSP Avatar answered Oct 03 '22 04:10

CROSP