如何让Notifee通知触发屏幕唤醒(含锁屏场景)?
解决Notifee通知无法唤醒锁屏屏幕的问题
问题说明
使用Notifee推送通知时,期望设备在锁屏状态下收到通知能自动唤醒屏幕,但当前配置下屏幕仍保持休眠,现有配置代码如下:
// Display a notification await notifee.displayNotification({ id: 'default', title: notification.message?.title, body: notification.message?.body, android: { category: AndroidCategory.CALL, vibrationPattern: [300, 500], lights: ['red', 300, 600], channelId, smallIcon: 'ic_launcher', // optional, defaults to 'ic_launcher'. importance: AndroidImportance.HIGH, visibility: AndroidVisibility.PUBLIC, pressAction: { id: 'default', launchActivity: 'default', // launchActivity: 'com.awesome.app.CustomActivity', }, fullScreenAction: { id: 'full-screen', mainComponent: 'custom-component', // For Android Activity other than the default: launchActivity: 'com.awesome.app.FullScreenActivity', }, // asForegroundService: true, actions: [ { title: 'Answer', pressAction: {id: 'answer', launchActivity: 'default'}, }, { title: 'Cancel', pressAction: { id: 'decline', }, }, ], }, });
解决方案
要实现锁屏唤醒,需在Android配置中补充关键属性,并确保权限配置正确:
1. 补充通知核心配置
在android配置节点下添加showWhenLocked和turnScreenOn两个属性,直接控制通知触发时的锁屏显示与唤醒逻辑:
android: { // 保留原有配置... showWhenLocked: true, turnScreenOn: true, // 保留原有配置... }
2. 配置Android权限
在项目的AndroidManifest.xml中添加必要权限:
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
3. 完整修改后的代码示例
// Display a notification await notifee.displayNotification({ id: 'default', title: notification.message?.title, body: notification.message?.body, android: { category: AndroidCategory.CALL, vibrationPattern: [300, 500], lights: ['red', 300, 600], channelId, smallIcon: 'ic_launcher', importance: AndroidImportance.HIGH, visibility: AndroidVisibility.PUBLIC, // 新增唤醒相关配置 showWhenLocked: true, turnScreenOn: true, pressAction: { id: 'default', launchActivity: 'default', }, fullScreenAction: { id: 'full-screen', mainComponent: 'custom-component', launchActivity: 'com.awesome.app.FullScreenActivity', }, actions: [ { title: 'Answer', pressAction: {id: 'answer', launchActivity: 'default'}, }, { title: 'Cancel', pressAction: { id: 'decline', }, }, ], }, });
关键说明
showWhenLocked:允许通知在锁屏状态下直接显示turnScreenOn:触发通知时自动唤醒设备屏幕- 配合已设置的
category: AndroidCategory.CALL和importance: AndroidImportance.HIGH,确保通知优先级足够高,避免被系统拦截 - Android 12+设备需
USE_FULL_SCREEN_INTENT权限以支持全屏通知唤醒逻辑
内容的提问来源于stack exchange,提问作者Pavel Shilyaev
相关产品推荐
相关产品推荐

