繁体   English   中英

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

[英]Cancelled Notification being redelivered when other notification is touched. Android

我遇到一个问题,无论用户触摸了什么通知,即使被取消,我也总是收到相同的通知。

用户触摸的第一个通知是唯一会一直传递到我待定意图的通知,直到设备重新启动为止(然后第一个触摸的通知将成为新的Hotel California通知)。 通知栏

这是我构造通知的方式:

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

然后我在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));

无论我触摸哪个通知,即使我触摸的通知的ID为56789 ,日志中也会始终显示Touched notification ID: 1234

我高度怀疑这是我的测试设备存在的问题,但万一有帮助, 我正在使用带有KitKat 4.4.4的2013 Nexus 7和Dalvik运行时(不是ART)Build KTU84P。

在此处传递您的notificationId作为request code

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

更改为:

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

比较两个Intent实例时,不比较包含的Bundles 因此,从android的角度来看,这两个意图是相同的。

有趣的是, reqCode (在第二个参数PendingIntent.getActivity(...) 比较。 Intents您的Intents变得独一无二。

暂无
暂无

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

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