简体   繁体   English

单击后Android通知不会消失

[英]Android notifications does not disappear after click

I have a piece of code on my android studio project: 我的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());
    }

It's working fine and i see the notification, but when i click on it it does not go away.. 它工作正常,我看到通知,但是当我单击它时,它不会消失。

Maybe it's because i'm using .notify ? 也许是因为我正在使用.notify吗? Please help me modify the code to makeit disapeair on click. 请帮助我修改代码,使其在点击时不起作用。

I'm a beginner :) 我是初学者:)

Use: 采用:

.setAutoCancel(true);

setAutoCancel setAutoCancel

Set flag FLAG_CANCEL_CURRENT instead of 0 设置标志FLAG_CANCEL_CURRENT而不是0

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

Also

.setAoutoCancel(true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM