3

How i can hide a persistent notification in my app?

Currently my notification it's shown also in the lockscreen, but I don't want to. I only want the notification to appear when I swipe down the notification menu.

I know it is possible because i have an App called WiFi ADB, and it is doing exactly that. (TouchWiz Android 5.0.1)

This is the code of my notification (it is in a Service):

@Override
public int onStartCommand(Intent intent, int _flags, int _startId) {
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    noti = new Notification.Builder(getApplicationContext())
            .setContentTitle("Title")
            .setContentText("Subtitle")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(contentIntent)
            .build();
    startForeground(1234, noti);
    mLocalBroadcastManager.sendBroadcast(new Intent(SERVICE_STARTED));
    return Service.START_STICKY;
}
Fr0z3n
  • 1,569
  • 1
  • 18
  • 39
  • Possible duplicate of [How to suppress notification on lock screen in Android 5 (Lollipop) but let it in notification area?](http://stackoverflow.com/questions/27035202/how-to-suppress-notification-on-lock-screen-in-android-5-lollipop-but-let-it-i) – David Lev Jul 31 '16 at 19:56

1 Answers1

4

Use setVisibility(NotificationCompat.VISIBILITY_SECRET) & setPriority(Notification.PRIORITY_MIN) :

mNotification = new Notification.Builder(getApplicationContext())
        .setContentTitle("Title")
        .setContentText("Subtitle")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentIntent(contentIntent)
        .setOngoing(true)
        .setVisibility(NotificationCompat.VISIBILITY_SECRET)
        .setPriority(Notification.PRIORITY_MIN)
        .build();

For VISIBILITY_SECRET :

Notification visibility: Do not reveal any part of this notification on a secure lockscreen.

For PRIORITY_MIN :

Lowest priority; these items might not be shown to the user except under special circumstances, such as detailed notification logs.


If you want to still show the icon in the status bar, there is no option for that but you can build an easy workaround by subscribing to USER_PRESENT & SCREEN_OFF events :

  • cancel notification when you receive SCREEN_OFF events
  • re-notify when you receive USER_PRESENT

register BroadcastReceiver & notify default notification :

mNotificationmanager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.USER_PRESENT");
filter.addAction("android.intent.action.SCREEN_OFF");
registerReceiver(mBroadcastReceiver, filter);

Intent notificationIntent = new Intent(this, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

mNotification = new Notification.Builder(getApplicationContext())
        .setContentTitle("Title")
        .setContentText("Subtitle")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentIntent(contentIntent)
        .setOngoing(true)
        .build();
mNotificationmanager.notify(NOTIF_ID, mNotification);

In BroadcastReceiver cancel on SCREEN_OFF, notify on USER_PRESENT :

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.v("test", intent.getAction());

        if (intent.getAction().equals("android.intent.action.SCREEN_OFF")) {
            mNotificationmanager.cancel(NOTIF_ID);
        } else if (intent.getAction().equals("android.intent.action.USER_PRESENT")) {
            mNotificationmanager.notify(NOTIF_ID, mNotification);
        }

    }
};
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • Thank you! it is working! But is there a way to still show the icon in the status bar? – Fr0z3n Jul 31 '16 at 21:13
  • I've updated with an alternative solution if you want to have notification icon in status bar, notification record in notification menu while not having it in lockscreen – Bertrand Martel Aug 01 '16 at 00:02