Android 通知被解除后,PendingEvent将继续存在

Android 通知被解除后,PendingEvent将继续存在,android,android-intent,android-notifications,android-pendingintent,Android,Android Intent,Android Notifications,Android Pendingintent,我编写了以下方法来检查通知是否在状态栏上可见: public static boolean isNotificationActive(Context context, int id) { Intent intentPopup = new Intent(context, PopupActivity.class); PendingIntent test = PendingIntent.getActivity(context, id, intentPopup, PendingIntent.FL

我编写了以下方法来检查通知是否在状态栏上可见:

public static boolean isNotificationActive(Context context, int id) {
  Intent intentPopup = new Intent(context, PopupActivity.class);
  PendingIntent test = PendingIntent.getActivity(context, id, intentPopup, PendingIntent.FLAG_NO_CREATE);
  return test != null;
}
我创建了一个与我想要检查的内容完全相同的PendingEvent,相同的“intentPopup”和“id”被传递给该PendingEvent,并且,由于标志“NO”create,如果该PendingEvent尚不存在,我希望该PendingEvent为null。我在几年前发布的帖子中读到了这个方法

如果通知尚未发出,该方法将返回预期值false。当发出通知并在状态栏中时,该方法返回true

但是,在我取消通知后,该方法仍然返回true,就好像PendingEvent仍然存在一样

我错过了什么?吊挂帐篷还可以用吗?还是我误解了什么

是否有更好的方法来检查状态栏中是否显示了特定的活动通知

编辑:以下是创建通知的代码:

    Intent intentPopup = new Intent(context, PopupActivity.class);
            intentPopup.putExtra("id", id);
            intentPopup.putExtra("timeQ", timeQ);

    PendingIntent pendingIntentPopup = PendingIntent.getActivity(context, id, intentPopup, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                    .setContentIntent(pendingIntentPopup)
                    .setSmallIcon(R.drawable.q_notification_icon)
                    .setContent(contentView)
                    .setSound(Uri.parse("android.resource://" + context.getPackageName() + "/raw/notification_sound"))
                    .setVibrate(vibrate_pattern);

            Notification notification = mBuilder.build();
            notification.flags |= Notification.FLAG_NO_CLEAR;
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(id, notification);
不幸的是,当通知被解除、查看或取消时,嵌入在通知中的挂起内容不会自动消失。您不能使用PendingEvent.FLAG_NO_CREATE来使用PendingEvent是否存在PendingEvent来确定通知是否处于活动状态


要确定通知是否处于活动状态,可以调用NotificationManager.getActiveNotifications。这将返回应用程序发布的所有活动通知。

发布用于创建通知的代码Notification@DavidWasser我知道了。不幸的是,getActiveNotifications是在API 23中引入的,我的应用程序的minsdk是19。。。不管怎样,谢谢你,@DavidWasser。我会继续寻找替代方案。另一个替代方案是使用NotificationListenerService。这是在API 18中引入的,因此您可以使用它。这有点复杂,但应该给你你想要的。有一个方法getActiveNotifications。