How to set notification to clear itself on click?

北城以北 提交于 2019-12-11 03:53:41

问题


How to set my notification to clear itself on click?

I've set autoCancel(true) but it doesn't work

My code:

Notification n  = new Notification.Builder(this)
                .setContentTitle("Update for you")
                .setContentText("Please click here to see the update information")
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), 
                                                           R.drawable.ic_launcher))
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pList)
                .setAutoCancel(true)
                .addAction(R.drawable.ic_read, "Read", pRead)
                .addAction(R.drawable.ic_list, "All Updates", pList)
                .build();

NotificationManager notificationManager = 
                      (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(0, n);

回答1:


Use FLAG_AUTO_CANCEL

n.flags |= Notification.FLAG_AUTO_CANCEL;



回答2:


setAutoCancel() of Notification.Builder is introduced in API 11. Perhaps, your minimum version is lower than API 11.

setAutoCancel() is also available in NotificationCompat.Builder class which is added in Support library

Notification n  = new NotificationCompat.Builder(this)
                .setContentTitle("Update for you")
                .setContentText("Please click here to see the update information")
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), 
                                                           R.drawable.ic_launcher))
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pList)
                .setAutoCancel(true)
                .addAction(R.drawable.ic_read, "Read", pRead)
                .addAction(R.drawable.ic_list, "All Updates", pList)
                .build();



回答3:


SetAutoCancel(true)

 NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            // .setLargeIcon(image)/*Notification icon image*/
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle("Quiz Tap")
            .setContentText(messageBody)
            /*.setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(image))*//*Notification with Image*/
            **.setAutoCancel(true)**
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);


来源:https://stackoverflow.com/questions/20558619/how-to-set-notification-to-clear-itself-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!