简体   繁体   English

触摸其他通知时,已重新发送已取消的通知。 Android的

[英]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). 用户触摸的第一个通知是唯一会一直传递到我待定意图的通知,直到设备重新启动为止(然后第一个触摸的通知将成为新的Hotel California通知)。 通知栏

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 : 然后我在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 . 无论我触摸哪个通知,即使我触摸的通知的ID为56789 ,日志中也会始终显示Touched notification ID: 1234

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. 我正在使用带有KitKat 4.4.4的2013 Nexus 7和Dalvik运行时(不是ART)Build KTU84P。

Pass your notificationId as the request code here: 在此处传递您的notificationId作为request code

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. 比较两个Intent实例时,不比较包含的Bundles So, from android's point of view, the two intents are the same. 因此,从android的角度来看,这两个意图是相同的。

Interestingly, the reqCode (second argument in PendingIntent.getActivity(...) ) is compared. 有趣的是, reqCode (在第二个参数PendingIntent.getActivity(...) 比较。 And this should render your Intents unique. Intents您的Intents变得独一无二。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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