单击后删除通知

Yos*_*oof 115 eclipse notifications android

我想要在用户点击后关闭通知.我看到每个人都说使用标志,但我无法在任何地方找到标志,因为我使用的是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());
Run Code Online (Sandbox Code Playgroud)

ing*_*.am 311

简单,简单地称之为:

mBuilder.setAutoCancel(true);
Run Code Online (Sandbox Code Playgroud)

此外,虽然它并非真的有必要,但如果您真的想使用FLAG_AUTO_CANCEL,请在致电之前致电mNotificationManager.notify:

mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
Run Code Online (Sandbox Code Playgroud)

  • 不推荐使用`getNotifications`,使用:`mBuilder.build().flags | = Notification.FLAG_AUTO_CANCEL;` (16认同)
  • 这是最好的答案.. flag_auto_cancel没有用......你救了我的一天! (2认同)
  • 当我点击通知时,这个setAutoCancel(true)的确有效.但是,当我点击通知中的操作(在我的情况下调用)时,它不起作用! (2认同)

Sil*_*uti 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());
Run Code Online (Sandbox Code Playgroud)


Ali*_*waz 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);
Run Code Online (Sandbox Code Playgroud)

取消点击的关键是:

            .setAutoCancel(true)
Run Code Online (Sandbox Code Playgroud)

我希望它能解决问题。


Jox*_*aex 3

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

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

单击后将关闭它。