title: Lollipop Notification setVisibility() Does Not Work?
body: <p>I am trying to write a demo of using <code>setVisibility()</code> to control what shows up on the Android 5.0 lockscreen for a <code>Notification</code>. However, there seems to be no effect:</p>
  
  <ul>
  <li><p>the default <code>VISIBILITY_PRIVATE</code> still shows the private <code>Notification</code>, not its public counterpart</p></li>
  <li><p><code>VISIBILITY_SECRET</code> notifications still show up on the lockscreen</p></li>
  </ul>
  
  <p>IOW, everything behaves as though <code>VISIBILITY_PUBLIC</code> 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.</p>
  
  <p>I have two editions of the same sample application:</p>
  
  <ul>
  <li><p><a href="https://github.com/commonsguy/cw-omnibus/tree/master/Notifications/Lollipop" rel="noreferrer">One</a> uses <code>NotificationCompat</code> and <code>NotificationManagerCompat</code></p></li>
  <li><p><a href="https://github.com/commonsguy/cw-omnibus/tree/master/Notifications/LollipopNonCompat" rel="noreferrer">The other</a> uses <code>Notification</code> and <code>NotificationManager</code> with a <code>minSdkVersion</code> of 21 and a <code>targetSdkVersion</code> of 21</p></li>
  </ul>
  
  <p>(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 <code>support-v13</code> library for the first sample)</p>
  
  <p>The samples use <code>AlarmManager</code> to trigger the <code>Notification</code> work, mostly so you have a chance to get back to the lockscreen to see the results. Here is the <code>BroadcastReceiver</code> that is triggered by <code>AlarmManager</code> (showing the <code>NotificationCompat</code> version):</p>
  
  <pre><code>/***
   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));
    }
  }
  </code></pre>
  
  <p>The <code>EXTRA_TYPE</code> is being set from a <code>Spinner</code> in the activity. That logic seems to be OK, because the heads-up <code>Notification</code> scenario works just fine. And if I step through the code (e.g., breakpoint in <code>onReceive()</code>), I see it going through the right paths (e.g., calling <code>setVisibility(NotificationCompat.VISIBILITY_SECRET)</code> in <code>notifySecret()</code> when I choose to raise a secret <code>Notification</code>).</p>
  
  <p>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.</p>
  
  <p>Any suggestions?</p>