21

I am trying to write a demo of using setVisibility() to control what shows up on the Android 5.0 lockscreen for a Notification. However, there seems to be no effect:

  • the default VISIBILITY_PRIVATE still shows the private Notification, not its public counterpart

  • VISIBILITY_SECRET notifications still show up on the lockscreen

IOW, everything behaves as though VISIBILITY_PUBLIC were in force, at least when I test on a Nexus 7 running the Android 5.0 image we were given shortly after Android 5.0 was released (build LPX13D). So I don't know if the problem is tied to my code, to this device, or to bugs in Android.

I have two editions of the same sample application:

  • One uses NotificationCompat and NotificationManagerCompat

  • The other uses Notification and NotificationManager with a minSdkVersion of 21 and a targetSdkVersion of 21

(note that these projects are primarily for use within Android Studio; Eclipse users can import the projects but they may require minor fixups, particularly for references to the support-v13 library for the first sample)

The samples use AlarmManager to trigger the Notification work, mostly so you have a chance to get back to the lockscreen to see the results. Here is the BroadcastReceiver that is triggered by AlarmManager (showing the NotificationCompat version):

/***
 Copyright (c) 2014 CommonsWare, LLC
 Licensed under the Apache License, Version 2.0 (the "License"); you may not
 use this file except in compliance with the License. You may obtain a copy
 of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
 by applicable law or agreed to in writing, software distributed under the
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License.

 From _The Busy Coder's Guide to Android Development_
 http://commonsware.com/Android
 */

package com.commonsware.android.lollipopnotify;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;

public class AlarmReceiver extends BroadcastReceiver {
  private static final int NOTIFY_ID=1337;
  static final String EXTRA_TYPE="type";

  @Override
  public void onReceive(Context ctxt, Intent i) {
    NotificationManagerCompat mgr=NotificationManagerCompat.from(ctxt);

    switch (i.getIntExtra(EXTRA_TYPE, -1)) {
      case 0:
        notifyPrivate(ctxt, mgr);
        break;

      case 1:
        notifyPublic(ctxt, mgr);
        break;

      case 2:
        notifySecret(ctxt, mgr);
        break;

      case 3:
        notifyHeadsUp(ctxt, mgr);
        break;
    }
  }

  private void notifyPrivate(Context ctxt, NotificationManagerCompat mgr) {
    Notification pub=buildPublic(ctxt).build();

    mgr.notify(NOTIFY_ID, buildNormal(ctxt).setPublicVersion(pub).build());
  }

  private void notifyPublic(Context ctxt, NotificationManagerCompat mgr) {
    mgr.notify(NOTIFY_ID,
        buildNormal(ctxt)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .build());
  }

  private void notifySecret(Context ctxt, NotificationManagerCompat mgr) {
    mgr.notify(NOTIFY_ID,
        buildNormal(ctxt)
            .setVisibility(NotificationCompat.VISIBILITY_SECRET)
            .build());
  }

  private void notifyHeadsUp(Context ctxt, NotificationManagerCompat mgr) {
    mgr.notify(NOTIFY_ID,
        buildNormal(ctxt)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .build());
  }

  private NotificationCompat.Builder buildNormal(Context ctxt) {
    NotificationCompat.Builder b=new NotificationCompat.Builder(ctxt);

    b.setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL)
        .setContentTitle(ctxt.getString(R.string.download_complete))
        .setContentText(ctxt.getString(R.string.fun))
        .setContentIntent(buildPendingIntent(ctxt, Settings.ACTION_SECURITY_SETTINGS))
        .setSmallIcon(android.R.drawable.stat_sys_download_done)
        .setTicker(ctxt.getString(R.string.download_complete))
        .addAction(android.R.drawable.ic_media_play,
            ctxt.getString(R.string.play),
            buildPendingIntent(ctxt, Settings.ACTION_SETTINGS));

    return(b);
  }

  private NotificationCompat.Builder buildPublic(Context ctxt) {
    NotificationCompat.Builder b=new NotificationCompat.Builder(ctxt);

    b.setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL)
        .setContentTitle(ctxt.getString(R.string.public_title))
        .setContentText(ctxt.getString(R.string.public_text))
        .setContentIntent(buildPendingIntent(ctxt, Settings.ACTION_SECURITY_SETTINGS))
        .setSmallIcon(android.R.drawable.stat_sys_download_done)
        .addAction(android.R.drawable.ic_media_play,
            ctxt.getString(R.string.play),
            buildPendingIntent(ctxt, Settings.ACTION_SETTINGS));

    return(b);
  }

  private PendingIntent buildPendingIntent(Context ctxt, String action) {
    Intent i=new Intent(action);

    return(PendingIntent.getActivity(ctxt, 0, i, 0));
  }
}

The EXTRA_TYPE is being set from a Spinner in the activity. That logic seems to be OK, because the heads-up Notification scenario works just fine. And if I step through the code (e.g., breakpoint in onReceive()), I see it going through the right paths (e.g., calling setVisibility(NotificationCompat.VISIBILITY_SECRET) in notifySecret() when I choose to raise a secret Notification).

Hence, I'm at a bit of a loss as to why I am not getting the visibility effects on the Android 5.0 lockscreen.

Any suggestions?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491

3 Answers3

10

The behavior you are describing is consistent with the behavior I experience when I set my lockscreen notification preference to "show all notification content."

This setting has three options:

  • Show all notification content makes all notifications (regardless of visibility) effectively public.

  • Hide sensitive notification content respects the new visibility types.

  • Don't show notifications at all will make all notifications effectively secret.

The option to change your lockscreen notification visiblity is in the device settings under Sound & Notifications > "When device is locked", as shown below.

As Selvin noted in his answer, the option to hide sensitive content is only available if you have set some sort of device lock (such as a PIN or pattern lock). If you can unlock your device with a simple swipe of the lockscreen, this option is not available.

Community
  • 1
  • 1
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • That's interesting, though I do not have "hide sensitive notification content" as an option. I have "show all notification content" and "don't show notifications at all" as my only two choices when tapping on the "When device is locked" preference. – CommonsWare Nov 14 '14 at 15:23
  • As noted in Selvin's answer, "hide sensitive notification content" appears if you have a PIN or password set (or pattern, apparently). – CommonsWare Nov 14 '14 at 15:30
  • hehe I'm just wondering if it is stated somewhere in documentation/manual :) – Selvin Nov 14 '14 at 15:31
  • Ahh, interesting. Definitely feels like it should be documented better, but this actually makes sense. Private notifications when you have almost no barrier to unlocking the device wouldn't be terribly effective at hiding content. – Bryan Herbst Nov 14 '14 at 15:35
  • Yeah i did experiment this, visibility will have effect when "Hide sensitive notification content respects the new visibility types." http://android.stackexchange.com/questions/88303/i-dont-see-the-app-notification-sensitive-option – NitZRobotKoder Jan 05 '15 at 08:12
  • is it possible to somehow get the value of that setting? I'm building a lockscreen myself and i'd like the notifications to follow that setting. – Mario Lenci Jun 30 '17 at 09:07
0

Setting NotificationBuilder#setPriority(Notification.PRIORITY_MIN); also prevents a notification from being shown on the lock screen.

Surprising, but at least it was well documented.

nmr
  • 16,625
  • 10
  • 53
  • 67
-3

In addition to Tanis.7x's answer: You have to chose any Lockscreen guard other than Swipe to "Hide sensitive notification content" option appear under Sound & Notifications

Selvin
  • 6,598
  • 3
  • 37
  • 43
  • The "hide sensitive notification content" option only appears if you have a PIN or password. However, if it is just swipe, the user has the choice of "all" or "none" in terms of what shows up on the lockscreen -- just not the middle ground of "hide sensitive". – CommonsWare Nov 14 '14 at 15:28