addCancel
Makes the notification automatically dismiss when the user taps it.
By default, notifications remain in the notification shade after being tapped. This helper method adds the FLAG_AUTO_CANCEL flag, which causes the notification to be automatically removed when the user taps on it.
Best Practice: Most notifications should be auto-cancelable to avoid cluttering the notification shade. Only persistent notifications (like music playback or downloads) should remain after being tapped. Example:
public Notification onGenerateNotification(@NonNull PushMessage data) {
String channelId = addChannel(data);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext(), channelId)
.setContentTitle(data.getHeader())
.setContentText(data.getMessage())
.setSmallIcon(R.drawable.ic_notification);
Notification notification = builder.build();
addCancel(notification); // Dismiss when tapped
return notification;
}
Content copied to clipboard
Parameters
notification
The notification to make auto-cancelable
See also
Notification