问题
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