如何在Android 5(Lollipop)的锁定屏幕上禁止通知,但是让它在通知区域?

Tom*_*lek 17 notifications android android-notifications android-5.0-lollipop

升级到Android 5.0 Lollipop后,它开始在锁定屏幕上自动显示持续通知.

有时用户不希望看到所有这些,因此他们要求开发人员如何在状态区域中发布通知,但将其隐藏在锁定屏幕上.

我找到的唯一方法是强制用户使用屏幕锁定(例如,手势或PIN)并以编程方式将setVisibility()设置VISIBILITY_SECRET.但并非所有人都想使用屏幕锁定.

是否有通知的标志(或标志组合):在锁定屏幕上不可见但在通知区域中可见?

Sam*_*Sam 14

使用可见性和优先级

本答案所述,VISIBILITY_SECRET当用户拥有安全键盘锁(不仅是滑动或没有键盘锁)并且抑制了敏感通知时,您可以使用此选项来禁止锁定屏幕上的通知.

要覆盖其余情况,您可以通过设置通知的优先级来设置锁定屏幕和状态栏中的通知,以便PRIORITY_MIN每当键盘锁定时存在,然后在没有键盘锁时重置优先级.

缺点

  • 使用Android 5模拟器,这似乎导致通知非常短暂地出现在锁定屏幕上,但随后消失.
  • 当用户没有安全锁定屏幕(例如,仅刷卡)时,不再使用Android O Developer Preview 2,因为不推荐使用通知优先级.

final BroadcastReceiver notificationUpdateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager =
            (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder =
            new NotificationCompat.Builder(context);
            .setVisibility(NotificationCompat.VISIBILITY_SECRET);

        KeyguardManager keyguardManager =
            (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);

        if (keyguardManager.isKeyguardLocked())
            builder.setPriority(NotificationCompat.PRIORITY_MIN);

        notificationManager.notify(YOUR_NOTIFICATION_ID, builder.build());
    }
};

//For when the screen might have been locked
context.registerReceiver(notificationUpdateReceiver,
    new IntentFilter(Intent.ACTION_SCREEN_OFF));

//Just in case the screen didn't get a chance to finish turning off but still locked
context.registerReceiver(notificationUpdateReceiver,
    new IntentFilter(Intent.ACTION_SCREEN_ON));

//For when the user unlocks the device
context.registerReceiver(notificationUpdateReceiver,
    new IntentFilter(Intent.ACTION_USER_PRESENT));

//For when the user changes users
context.registerReceiver(notificationUpdateReceiver,
    new IntentFilter(Intent.ACTION_USER_BACKGROUND));
context.registerReceiver(notificationUpdateReceiver,
    new IntentFilter(Intent.ACTION_USER_FOREGROUND));
Run Code Online (Sandbox Code Playgroud)


Jim*_*tek 6

似乎VISIBILITY_SECRET是最干净的方法.根据文件:

可以发出通知VISIBILITY_SECRET,它将禁止其图标和自动收报机,直到用户绕过锁屏.

根据源(SystemUI AOSP项目中的NotificationData),VISIBILITY_SECRET是唯一的方法:

boolean shouldFilterOut(StatusBarNotification sbn) {
    if (!(mEnvironment.isDeviceProvisioned() ||
            showNotificationEvenIfUnprovisioned(sbn))) {
        return true;
    }

    if (!mEnvironment.isNotificationForCurrentProfiles(sbn)) {
        return true;
    }

    if (sbn.getNotification().visibility == Notification.VISIBILITY_SECRET &&
            mEnvironment.shouldHideSensitiveContents(sbn.getUserId())) {
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

似乎过滤掉的唯一其他类型的通知是存在摘要的组中的子通知.因此,除非您有多个有正当理由的摘要,否则VISIBILITY_SECRET是目前可以完成的最佳选择.


Flo*_*ern 4

您可以将通知的优先级设置为PRIORITY_MIN。这应该会隐藏锁定屏幕上的通知。它还隐藏了状态栏中的图标(不确定您是否需要),但通知本身在通知区域中仍然可见。

  • 不,这不是我的意图。人们希望在状态栏中看到通知,而不是在锁定屏幕上看到通知。 (3认同)