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 are asking developers how to let notification in status area but hide them on lock screen.
Only way I found is to force users use screen lock (eg. Gesture or PIN) and programatically setVisibility() to VISIBILITY_SECRET. But not all them want to use screen lock.
Is there any flag (or combination of flags) saying to notification: don't be visible on Lock screen but be visible in notification area?
Seems like VISIBILITY_SECRET does the cleanest approach. As per the documentation:
Per the source (NotificationData in the SystemUI AOSP project), VISIBILITY_SECRET is the only way to do it:
The only other type of notifications that appear to be filtered out are child notifications in a group where a summary is present. So unless you have multiple with a valid reason for a summary, VISIBILITY_SECRET is the best that can currently be done.
You could set the notification's priority to PRIORITY_MIN. This should hide the notification on the lock screen. It also hides the icon from the statusbar (not sure if you want that), but the notification itself is still visible in the notification area.
Use visibility and priority
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.Disadvantages
Example
I've created a 'LockscreenIntentReceiver' for my ongoing notification that looks like this:
This code will basically remove the ongoing notification when the user locks the phone (removal will be visible very briefly). And once the user unlocks the phone, the ongoing notification will be restored after the delay time (800 ms here). enableNotification() is a method which will create the notification, and call startForeground(). Currently verified to work on Android 7.1.1 .
You only have to remember to register & unregister the receiver accordingly.