点击后删除通知 [英] Remove notification after clicking

查看:98
本文介绍了点击后删除通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,该通知将关闭用户点击它后。我看到大家说用标志,但我找不到任何地方的标志,因为我使用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;

这篇关于点击后删除通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆