点击后,Android通知不会消失

时间:2016-02-12 15:48:44

标签: android android-notifications

我的android studio项目中有一段代码:

private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        // .setSmallIcon(R.drawable.ic_stat_gcm)
                        .setContentTitle("New updates from StoriesCity!")
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .setContentText(msg);


        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

它工作正常,我看到了通知,但是当我点击它时它不会消失..

也许是因为我正在使用.notify? 请帮我修改代码,以便在点击时显示失败。

我是初学者:)

2 个答案:

答案 0 :(得分:2)

使用:

.setAutoCancel(true);
  

setAutoCancel

答案 1 :(得分:0)

设置标记FLAG_CANCEL_CURRENT而不是0

 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);

另外

.setAoutoCancel(true)