有没有办法在收到来自 FCM 或其他地方的推送通知时打开 Android 屏幕?
我希望即使应用程序在后台,即使应用程序被杀死,它也会打开。
此外,我希望即使设备处于静音模式,通知也会发出声音。可以吗?
很简单,只需在您的 onMessagesReceived 方法中调用此函数即可。我也在我的项目中使用了这个。
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()
}
}
另外不要忘记在清单中添加此权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />