Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress notification on lock screen in Android 5 (Lollipop) but let it in notification area?

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?

like image 200
Tomáš Hubálek Avatar asked Nov 20 '14 08:11

Tomáš Hubálek


People also ask

How do I get my lock screen to not show notifications until unlocked?

Open your phone's Settings app. Notifications. Under 'Lock screen', tap Notifications on lock screen or On lock screen. Choose Don't show notifications.


Video Answer


3 Answers

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

  • Using an Android 5 emulator, this seems to result in the notification very briefly appearing on the lock screen but then disappearing.
  • No longer working as of Android O when the user doesn't have a secure lock screen (eg swipe only) since notification priorities are deprecated.

Example

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));
like image 164
Sam Avatar answered Oct 06 '22 00:10

Sam


Seems like VISIBILITY_SECRET does the cleanest approach. As per the documentation:

a notification can be made VISIBILITY_SECRET, which will suppress its icon and ticker until the user has bypassed the lockscreen.

Per the source (NotificationData in the SystemUI AOSP project), VISIBILITY_SECRET is the only way to do it:

boolean shouldFilterOut(StatusBarNotification sbn) {
    if (!(mEnvironment.isDeviceProvisioned() ||
            showNotificationEvenIfUnprovisioned(sbn))) {
        return true;
    }

    if (!mEnvironment.isNotificationForCurrentProfiles(sbn)) {
        return true;
    }

    if (sbn.getNotification().visibility == Notification.VISIBILITY_SECRET &&
            mEnvironment.shouldHideSensitiveContents(sbn.getUserId())) {
        return true;
    }
    return false;
}

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.

like image 22
Jim Vitek Avatar answered Oct 05 '22 22:10

Jim Vitek


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.

like image 36
Floern Avatar answered Oct 05 '22 23:10

Floern