Android 点击后通知不会消失

Android notifications does not disappear after click

我的 android 工作室项目中有一段代码:

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 ? 请帮我修改代码,让它在点击时消失。

我是初学者:)

使用:

.setAutoCancel(true);

setAutoCancel

设置标志 FLAG_CANCEL_CURRENT 而不是 0

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

还有

.setAoutoCancel(true)