使用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();
对于VISIBILITY_SECRET
:
通知可见性:不透露任何部分不通过安全的锁屏。
对于PRIORITY_MIN
:
最低优先级;除特殊情况外,这些项目可能不会显示给用户,例如详细的通知日志。
如果你想仍然显示在状态栏上的图标,对于没有选择,但你可以通过订阅USER_PRESENT
& SCREEN_OFF
事件构建一个简单的解决方法:
- 取消当您收到通知时
SCREEN_OFF
事件
- 当您收到时重新通知
USER_PRESENT
寄存器BroadcastReceiver
&通知默认的通知:
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);
在BroadcastReceiver
取消SCREEN_OFF
,通知上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);
}
}
};
的可能的复制【如何抑制Android的5锁定屏幕上的通知(棒棒堂),但让它在通知区域?](http://stackoverflow.com/questions/27035202/how-to-suppress-notification-on-lock-screen-in-android-5-lollipop-but-let-it-我) –