是否有可能检查的通知可见或取消? [英] Is it possible to check if a notification is visible or canceled?

查看:191
本文介绍了是否有可能检查的通知可见或取消?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新通知数据,但我发现的唯一方法是推出一个新的具有相同的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;
}

这是怎么生成通知:

    /**
 * 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);
}

这篇关于是否有可能检查的通知可见或取消?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆