我编写了以下方法来检查通知是否处于活动状态(在状态栏上可见):
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;
}
我创建的PendingIntent与我想要检查的内容完全相同(相同的' intentPopup'' id'传递给此PendingIntent),并且由于FLAG_NO_CREATE,我希望如果尚未存在,则此PendingIntent为null。我在几年前发布的帖子中读到了这个方法。
如果尚未发出通知,则该方法返回预期值(false)。当发出通知并且在状态栏中时,该方法返回true。
但是,在我解除通知后,该方法仍然返回true,就好像PendingIntent仍然存在一样。
我在这里缺少什么? PendingIntent仍然可用还是我误解了什么?
有没有更好的方法来检查特定通知是否有效(显示在状态栏中)?
编辑:这是创建通知的代码:
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);
答案 0 :(得分:1)
不幸的是,当您PendingIntent
被解除,查看或取消时,嵌入Notification
的{{1}}不会自动消失。您不能使用Notification
的存在与否(使用PendingIntent
,就像您已经完成的那样)来确定PendingIntent.FLAG_NO_CREATE
是否有效。
要确定Notification
是否有效,您可以致电Notification
。这将返回您的应用发布的所有有效NotificationManager.getActiveNotifications()
。