prom85
prom85

Reputation: 17878

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);

Upvotes: 9

Views: 1212

Answers (1)

guipivoto
guipivoto

Reputation: 18687

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.

Upvotes: 18

Related Questions