点击后删除通知通知

2023-09-05 08:19:10 作者:克里斯是王

我想,该通知将关闭用户点击它后。我看到大家说用标志,但我找不到任何地方的标志,因为我使用NotificationCompat.Builder类而不是通知类。有人有任何想法如何通过她的自我,使通知删除? 这是我的code,当我设置了通知:

  NotificationCompat.Builder mBuilder =
            新NotificationCompat.Builder(本)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(新问题)
            .setContentText(+ QuestionData.getAuthor()getUserName()+:+ QuestionData.getQuestion()+);

    意图openHomePageActivity =新的意向书(com.example.ihelp.HOMEPAGEACTIVITY);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(本);
    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());
 

解决方案

很简单,只需要调用这样的:

  mBuilder.setAutoCancel(真正的);
 

另外,虽然这不是真的有必要,如果你真的想用 FLAG_AUTO_CANCEL ,只需拨打这个你打电话之前 mNotificationManager.notify

  mBuilder.getNotification()标志| = Notification.FLAG_AUTO_CANCEL。
 
手机一直显示下载东西怎么回事

I want that the notification will be closed after the user is clicking on it. I saw that everybody saying to use flags, but I can't find the flags anywhere because I'm using NotificationCompat.Builder class and not Notification class. Someone have any idea how to make the notification remove by her self? Here is my code when I'm setting the 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());

解决方案

Easy, simply call this:

mBuilder.setAutoCancel(true);

Also, while it's not really necessary, if you really want to use FLAG_AUTO_CANCEL, just call this before you call mNotificationManager.notify:

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