点击后删除通知


120

我希望在用户单击通知后将其关闭。我看到每个人都在说要使用标志,但是我在任何地方都找不到标志,因为我使用的是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());

Answers:


325

容易,只需调用此:

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...。

2
当我点击通知时,此setAutoCancel(true)确实有效。但是,当我单击通知中的“操作”(就我而言)时,它不起作用!
Async-

1
请在这里查看我的问题:stackoverflow.com/questions/37595594/…–
Async-

18

试试这个....

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()已不推荐使用,现在.build()改为使用 mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
Shylendra Madda

9

这是通知:

    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)

我希望它能解决问题。



1

在Kotlin中,您可以使用:

mBuilder.build().flags.and(Notification.FLAG_AUTO_CANCEL)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.