Reputation: 227
In our app we would like to wake the screen up when notification arrives. This notification logic will be in service. We have tried to use "PARTIAL_WAKE_LOCK", but this is not working. We also tried "FULL_WAKE_LOCK", which was working but as this is deprecated we cannot use it in prod app. We have seen the alternatives for "PARTIAL_WAKE_LOCK" like "turnScreenOn" but this cannot be used from the service.
My question is do we have any other alternative to turn the screen on from the messaging service when notification arrives on the device?
Or, We have settings on the device to enable the wakelock manually right? (settings->display->lock screen->wake screen for notifications.) Can we open this settings page directly from the app somehow programatically?
Upvotes: 0
Views: 1897
Reputation: 37
I have already solved similar problem recently in my project.
Firstly ensure you have added this permission in your manifest.
<uses-permission android:name="android.permission.WAKE_LOCK" />
Once you have done that now just call this code inside your FCM service class onMessageReceived method.
private fun wakeApp() {
val pm = applicationContext.getSystemService(POWER_SERVICE) as PowerManager
val screenIsOn = pm.isInteractive // check if screen is on
if (!screenIsOn) {
val wakeLockTag = packageName + "WAKELOCK"
val wakeLock = pm.newWakeLock(
PowerManager.FULL_WAKE_LOCK or
PowerManager.ACQUIRE_CAUSES_WAKEUP or
PowerManager.ON_AFTER_RELEASE, wakeLockTag
)
//acquire will turn on the display
wakeLock.acquire()
//release will release the lock from CPU, in case of that, screen will go back to sleep mode in defined time bt device settings
wakeLock.release()
}
}
You can play around with aquire & release inside your onPause & onResume to make it work correctly.
Upvotes: 1