Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification - click fires wrong PendingIntent

I have a notification with following content intent:

clickIntent = PendingIntent.getService(
                    getApplicationContext(),
                    Constants.REQUEST_CODE_NOTIFICATION_OTHER,
                    new Intent(getApplicationContext(), MainService.class)
                            .putExtra(Constants.EVENT, Constants.EVENT_TOGGLE_BLACK),
                    PendingIntent.FLAG_UPDATE_CURRENT);

and one action with following action intent:

PendingIntent closeIntent = PendingIntent.getService(
        getApplicationContext(),
        Constants.REQUEST_CODE_NOTIFICATION_OTHER,
        new Intent(getApplicationContext(), MainService.class)
                .putExtra(Constants.EVENT, Constants.EVENT_DISABLE_SERVICE),
        PendingIntent.FLAG_UPDATE_CURRENT);

When I click the notification, the close intent is fired. Why?

Here is how I create the notification:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(getString(text))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(getString(details)))
            .setSmallIcon(R.drawable.ic_not)
            .setContentIntent(clickIntent)
            .setOngoing(true);

    boolean showCloseButton = MainApp.getPrefs().enableCloseServiceButtonInNotification();
    if (showCloseButton)
        builder.addAction(R.drawable.ic_close_black_24dp, getString(R.string.stop_service), closeIntent);
like image 811
prom85 Avatar asked Jul 28 '16 22:07

prom85


People also ask

What is a PendingIntent Android?

A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it.

What is Pending Intent in notification?

In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.

How do you handle opening activity using notifications?

Build and issue the notification: Create an Intent that starts the Activity . Set the Activity to start in a new, empty task by calling setFlags() with the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK . Create a PendingIntent by calling getActivity() .

What is FLAG_ IMMUTABLE in Android?

FLAG_IMMUTABLE : Indicates the Intent inside the PendingIntent cannot be modified by other apps that pass an Intent to PendingIntent.send() . An app can always use FLAG_UPDATE_CURRENT to modify its own PendingIntents. Prior to Android 12, a PendingIntent created without this flag was mutable by default.


1 Answers

This is my guess... If does not work, I'll delete the answer...

I believe problem is that they are sharing same update number:

This is created:

clickIntent = PendingIntent.getService(
    getApplicationContext(),
    Constants.REQUEST_CODE_NOTIFICATION_OTHER,
    new Intent(getApplicationContext(), MainService.class).putExtra(Constants.EVENT, Constants.EVENT_TOGGLE_BLACK),
    PendingIntent.FLAG_UPDATE_CURRENT);

Then, this new one is created:

PendingIntent closeIntent = PendingIntent.getService(
    getApplicationContext(),
    Constants.REQUEST_CODE_NOTIFICATION_OTHER,
    new Intent(getApplicationContext(), MainService.class).putExtra(Constants.EVENT, Constants.EVENT_DISABLE_SERVICE),
    PendingIntent.FLAG_UPDATE_CURRENT);

It is similar to previous one and it is set PendingIntent.FLAG_UPDATE_CURRENT. So, the second one UPDATES the first one.

Try to use a different code for different PendingIntents. Both are using Constants.REQUEST_CODE_NOTIFICATION_OTHER.

Then, Android will use that code to differentiate them.

like image 89
W0rmH0le Avatar answered Nov 15 '22 04:11

W0rmH0le