单击后删除通知

时间:2013-02-27 19:36:50

标签: android eclipse notifications

我希望用户点击后会关闭通知。我看到每个人都说使用标志,但我无法在任何地方找到标志,因为我使用的是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 个答案:

答案 0 :(得分:305)

简单,只需称呼:

mBuilder.setAutoCancel(true);

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

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

答案 1 :(得分: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());

答案 2 :(得分:5)

这是通知:

jq

取消点击的关键是:

$ python test.py | jq . 
{
  "floor1": {
    "room2": [
      "2people"
    ],
    "room3": [
      "3people"
    ],
    "room1": [
      "note1",
      "note2",
      "note3"
    ]
  },
  "floor2": {
    "room4": [
      "6people",
      "projector"
    ],
    "room5": [
      "3people",
      "projector"
    ]
  },
  "floor3": {
    "room6": [
      "1person"
    ]
  }
}

我希望它能解决问题。

答案 3 :(得分:1)

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

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

这会在点击时将其解雇。

答案 4 :(得分:0)

在Kotlin中,您可以使用:

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