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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With