单击后删除通知

问题描述 投票:113回答:5

我想要在用户点击后关闭通知。我看到每个人都说使用标志,但我无法在任何地方找到标志,因为我使用的是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());
android eclipse notifications
5个回答
306
投票

简单,简单地称之为:

mBuilder.setAutoCancel(true);

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

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

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

6
投票

这是通知:

    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
投票

您可以在通知中添加标记:

http://developer.android.com/reference/android/app/Notification.html#FLAG_AUTO_CANCEL

这将在点击时解除它。


0
投票

在kotlin中,您可以使用:

mBuilder.build().flags.and(Notification.FLAG_AUTO_CANCEL)
© www.soinside.com 2019 - 2024. All rights reserved.