单击后删除通知

我希望通知将关闭后,用户点击它。我看到每个人都说要使用标志,但是我在任何地方都找不到标志,因为我正在使用 NotificationCompat。生成器类而不是通知类。有人知道怎么自己删除通知吗?
下面是我设置通知时的代码:

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());
90481 次浏览

您可以在通知中添加一个标志:

Http://developer.android.com/reference/android/app/notification.html#flag_auto_cancel

这将在单击时解除它。

简单,简单地称之为:

mBuilder.setAutoCancel(true);

另外,虽然这不是真正必要的,但是如果你真的想要使用 FLAG_AUTO_CANCEL,只需要在调用 mNotificationManager.notify之前调用它:

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

试试这个。

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());

以下是通知:

    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)

我希望这能解决问题。

在 Kotlin 你可以用:

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