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);
}
}
};