'Full screen notifications unwantedly triggering keyguard
I'm making an app that shows full-screen notifications when the device is locked.
Expected behavior: when a notification is launched, the activity shows over the lock screen without prompting the user to unlock the device. It should be the same as an alarm clock notification, just without waking up the screen.
Code: in my onCreate() I'm calling:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true)
val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
keyguardManager.requestDismissKeyguard(this,null)
} else {
this.window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)
this.window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)
}
Problem: in some devices (e.g. Pixel 6 API 31), when the full-screen intent is launched, the keyguard is shown on top of my activity asking the user to enter their credentials. If I dismiss the keyguard, my activity remains on the screen (so the device didn't have to be unlocked in the first place!). What I want to avoid is having the keyguard show and having to dismiss it! In other devices (e.g. old Huawei tablet, API 27), the full-screen intent launches smoothly without requesting to unlock the device, which is what I want.
I tried simply not calling keyguardManager.requestDismissKeyguard(). This solves the issue on my Pixel 6 API 31 (which now works like the Huawei API 27 did before). But now the Huawei API 27 does not launch the full-screen intent at all.
I have no idea if this is an API thing or a device/ROM-specific thing. I checked the documentation but didn't find anything helpful. Has anyone come across this issue before? Any ideas will be appreciated!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|