点击后删除通知

132

我希望在用户点击通知后,通知能够关闭。我看到很多人都建议使用标志位,但是因为我正在使用NotificationCompat.Builder类而不是Notification类,所以我无法找到标志位。有人知道如何让通知自动移除吗?
这是我设置通知时的代码:

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("New Question")
            .setContentText("" + QuestionData.getAuthor().getUserName() + ": " + QuestionData.getQuestion() + "");

    Intent openHomePageActivity = new Intent("com.example.ihelp.HOMEPAGEACTIVITY");
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(openHomePageActivity);

    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);       

    mNotificationManager.notify(0, mBuilder.build());
5个回答

346

简单易行,只需调用此函数:

mBuilder.setAutoCancel(true);

另外,虽然这并不是必须的,但如果您真的想要使用FLAG_AUTO_CANCEL,只需在调用mNotificationManager.notify之前调用此函数:

mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;

2
这是最好的答案。flag_auto_cancel没有起作用。你救了我的一天! - allemattio
16
getNotifications已弃用,请使用mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;代替。该方法可以使通知自动取消。 - blueware
mBuilder.build() 也已经被弃用。 - Mansuu....
3
当我点击通知时,setAutoCancel(true)的确有效。但是当我在通知中点击操作(例如我的情况中是电话呼叫),它并不起作用! - Async-
@Async- 你可以提供更多细节吗?或者在SO上发一个新问题? - ingh.am
1
请查看我的问题: https://dev59.com/-Jffa4cB1Zd3GeqP_Kg1#37610495 - Async-

19

试试这个...

NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

 ..........
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.push_notify_icon)
            .setContentTitle("New Question!")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setAutoCancel(true).setContentText("" + QuestionData.getAuthor().getUserName() + ": " + QuestionData.getQuestion() + "");
mBuilder.setContentIntent(contentIntent);

    ..............        


mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(0, mBuilder.build());

1
.getNotification() is deprecated now use .build() instead like mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL; - Shailendra Madda

14

这里是通知:

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_calendar)
            .setContentTitle("My Firebase Push notification")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent);

取消点击背后的关键是:

            .setAutoCancel(true)

我希望它能解决问题。


我正在前台显示通知,当用户点击时,我想要取消该通知。我已经使用了.setAutoCancel(true),但对于前台通知来说,它并没有起作用。 - Arbaz.in

3

将它添加到您的通知对象的通知标志中。 - JoxTraex

2

In kotlin, you can use:

mBuilder.build().flags.and(Notification.FLAG_AUTO_CANCEL)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接