After upgrade to Android 5.0 Lollipop it started showing automatically ongoing notification on lock screen.
Sometimes users don\'t want to see all of them so they a
As covered by this answer, you can use VISIBILITY_SECRET to suppress the notification on the lock screen when the user has a secure keyguard (not just swipe or no keyguard) and has sensitive notifications suppressed.
To cover the rest of the cases, you can programmatically hide the notification from the lock screen and status bar by setting the notification's priority to PRIORITY_MIN whenever the keyguard is present and then reset the priority whenever the keyguard is absent.
final BroadcastReceiver notificationUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context, YOUR_NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.your_icon)
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
KeyguardManager keyguardManager =
(KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
if (keyguardManager.isKeyguardLocked())
builder.setPriority(NotificationCompat.PRIORITY_MIN);
notificationManager.notify(YOUR_NOTIFICATION_ID, builder.build());
}
};
//For when the screen might have been locked
context.registerReceiver(notificationUpdateReceiver,
new IntentFilter(Intent.ACTION_SCREEN_OFF));
//Just in case the screen didn't get a chance to finish turning off but still locked
context.registerReceiver(notificationUpdateReceiver,
new IntentFilter(Intent.ACTION_SCREEN_ON));
//For when the user unlocks the device
context.registerReceiver(notificationUpdateReceiver,
new IntentFilter(Intent.ACTION_USER_PRESENT));
//For when the user changes users
context.registerReceiver(notificationUpdateReceiver,
new IntentFilter(Intent.ACTION_USER_BACKGROUND));
context.registerReceiver(notificationUpdateReceiver,
new IntentFilter(Intent.ACTION_USER_FOREGROUND));