在android中取消通知

时间:2014-07-10 14:59:33

标签: android android-intent android-notifications android-pendingintent

我有一个提醒应用程序,它会触发几个任务的通知,假设我删除或打开通知任务我想清除该特定通知

我试过这个解决方案

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);

在这里,总是在打开任务时我需要使用通知ID调用此方法,这是否会给我的应用程序带来问题?

如果有,是否有办法检查身份证的通知是否存在&然后清除它?

2 个答案:

答案 0 :(得分:2)

试试这个

        NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notify = new Notification(R.drawable.ic_launcher,
                "tittle", System.currentTimeMillis());
        PendingIntent pending = PendingIntent.getActivity(
                getApplicationContext(), 0, new Intent(), 0);
        notify.setLatestEventInfo(getApplicationContext(), "subject",
                "body", pending);
        notify.flags |= Notification.FLAG_AUTO_CANCEL;
        notif.notify(0, notify);

答案 1 :(得分:1)

您可以通过设置notificationId(在生成通知时使用)来清除通知 或者将其设置为autoCancel,以便当用户执行通知任务时,它将自动从通知托盘中消失。你也可以这样做:

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setAutoCancel(true);