简体   繁体   中英

Cancelled Notification being redelivered when other notification is touched. Android

I am having an issue where, no matter what notification the user touches, I am always receiving the same notification, even though it is cancelled.

The first notification a user touches is the only notification that will ever be delivered to my pending intent, until device reboot (then the first touched notification becomes the new Hotel California notification). 通知栏

Here is how I am constructing the notifications:

    final Bundle extras = new Bundle();
    final int notificationId = Integer.parseInt(payload.getObjectId());

    extras.putInt(NOTIFICATION_ID, notificationId);

    final Intent intent = new Intent(context,
            PushNotificationLauncherActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    intent.putExtras(extras);

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);
    builder.setContentTitle(context.getString(R.string.app_name));
    builder.setContentText(payload.getText());
    builder.setSmallIcon(R.drawable.icon_launcher);
    builder.setAutoCancel(true);

    final PendingIntent pIntent = PendingIntent.getActivity(context, 0,
            intent, 0, extras);

    builder.setContentIntent(pIntent);

    final Notification notification;
    if (Build.VERSION.SDK_INT < 16) {
        notification = builder.getNotification();
    } else {
        notification = builder.build();
    }

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(notificationId, notification);

Then I have this in my PushNotificationLauncherActivity :

    final Bundle extras = getIntent().getExtras();

    final int notificationId = extras
            .getInt(SocialcastGoogleCloudMessageReceiver.NOTIFICATION_ID);

    final NotificationManager notificationManager = (NotificationManager) this
            .getSystemService(NOTIFICATION_SERVICE);
    // please go away
    notificationManager.cancel(notificationId);

    Log.d(PushNotificationLauncherActivity.class.getSimpleName(),
            "Touched notification ID: " + String.valueOf(notificationId));

No matter which notification I touch, in the log Touched notification ID: 1234 will always show, even if the notification I touch on has an ID of 56789 .

I highly doubt it is an issue with my test-device, but incase it helps; I am using a 2013 Nexus 7 with KitKat 4.4.4 with the Dalvik runtime (not ART) Build KTU84P.

Pass your notificationId as the request code here:

final PendingIntent pIntent = PendingIntent.getActivity(context, 0,
        intent, 0, extras);

Change this to:

final PendingIntent pIntent = PendingIntent.getActivity(context, notificationId,
        intent, 0, extras);

When comparing two Intent instances, the included Bundles are not compared. So, from android's point of view, the two intents are the same.

Interestingly, the reqCode (second argument in PendingIntent.getActivity(...) ) is compared. And this should render your Intents unique.

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