简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM