是否可以检查通知是否可见或已取消?

编程入门 行业动态 更新时间:2024-10-20 08:33:31
本文介绍了是否可以检查通知是否可见或已取消?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想更新通知数据,但我找到的唯一方法是启动一个具有相同 ID 的新通知.

I would like to update notification data, but the only way I found is to launch a new one with the same Id.

问题是如果原来的被取消了,我不想再提出一个新的.有没有办法判断通知是否可见或已取消?或者只有在通知存在时才更新通知的方法?

The problem is that I don't want to raise a new one if the original has beed canceled. Is there a way to tell if a notification is visible or canceled? Or a way to update a notification only if it exists?

推荐答案

我是这样解决的:

private boolean isNotificationVisible() { Intent notificationIntent = new Intent(context, MainActivity.class); PendingIntent test = PendingIntent.getActivity(context, MY_ID, notificationIntent, PendingIntent.FLAG_NO_CREATE); return test != null; }

这是我生成通知的方式:

This is how I generate the notification:

/** * Issues a notification to inform the user that server has sent a message. */ private void generateNotification(String text) { int icon = R.drawable.notifiaction_icon; long when = System.currentTimeMillis(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(icon, text, when); String title = context.getString(R.string.app_name); Intent notificationIntent = new Intent(context, MainActivity.class); // set intent so it does not start a new activity //notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity(context, MY_ID, notificationIntent, 0); notification.setLatestEventInfo(context, title, text, intent); notification.flags |= Notification.FLAG_AUTO_CANCEL; //PendingIntent.FLAG_ONE_SHOT notificationManager.notify(MY_ID, notification); }

更多推荐

是否可以检查通知是否可见或已取消?

本文发布于:2023-11-27 09:13:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637452.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:或已   通知

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!