Flutter 锁屏通知开发:设置NotificationVisibility.public后仍不显示的问题
锁屏通知不显示的排查与修正方案
核心问题根因排查(按出现概率从高到低排序)
- Android 8.0+ 通知渠道的锁屏配置缺失
Android 8.0(API 26)之后系统将锁屏展示权限独立绑定到通知渠道的配置项上,你代码里只在单条通知层面配置了visibility: NotificationVisibility.public,首次创建渠道时如果没同步配置渠道级的锁屏可见性,系统会默认拦截该渠道的通知在锁屏的展示,哪怕单条通知的visibility设为public也不会生效。 - 固定通知ID 0被系统拦截
你当前所有通知都使用了固定ID0,部分Android定制系统会将ID为0的通知判定为内部低优先级通知,默认不允许展示在锁屏、通知栏前台位置。 - Android 13+ 运行时通知权限未申请
Android 13(API 33)之后新增了POST_NOTIFICATIONS运行时权限,未主动申请该权限的应用不仅锁屏通知无法展示,甚至连普通通知栏通知都可能被系统拦截。 - ROM层默认拦截新应用的锁屏通知
国内主流定制ROM(小米、华为、OPPO、vivo等)会默认对新安装应用关闭「锁屏通知展示」开关,哪怕系统原生通知权限已授予,ROM层的策略也会拦截通知上锁屏。 - FCM推送优先级不匹配
你是在FCM的data消息回调里手动调用本地通知弹提示,如果后端发的FCM消息优先级为normal,在应用退到后台锁屏状态下,系统会直接拦截该推送,不会触发本地通知展示逻辑。
代码修正片段
// 替换原有固定ID的showNotification方法 void showNotification(DataNotification notification) async { // 生成唯一有效通知ID,替代固定值0 final int notificationId = DateTime.now().millisecondsSinceEpoch % 2147483647; final AndroidNotificationDetails androidPlatformChannelSpecifics = await getAndroidNotificationDetails(notification); final NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics); await notificationsPlugin.show( notificationId, notification.title, notification.body, platformChannelSpecifics, ); } // 替换原有渠道配置,补充渠道级锁屏可见参数 Future<AndroidNotificationDetails> getAndroidNotificationDetails( DataNotification notification) async { switch (notification.notificationType) { case NotificationType.NEW_INVITATION: case NotificationType.NEW_MEMBERSHIP: case NotificationType.NEW_ADMIN_ROLE: case NotificationType.MEMBERSHIP_BLOCKED: case NotificationType.MEMBERSHIP_REMOVED: case NotificationType.NEW_MEMBERSHIP_REQUEST: return AndroidNotificationDetails( 'organization', 'Organization management', 'Notifications regarding your organizations and memberships.', importance: Importance.max, priority: Priority.high, showWhen: false, playSound: true, ledColor: Colors.redAccent, color: Colors.amber, category: "Organization", icon: '@mipmap/ic_launcher', visibility: NotificationVisibility.public, // 显式配置渠道级锁屏可见性 lockscreenVisibility: NotificationVisibility.public, largeIcon: DrawableResourceAndroidBitmap('@mipmap/ic_launcher'), styleInformation: await getBigPictureStyle(notification), ); case NotificationType.NONE: default: return AndroidNotificationDetails( 'general', 'General notifications', 'General notifications that are not sorted to any specific topics.', importance: Importance.max, priority: Priority.high, showWhen: false, category: "General", playSound: true, ledColor: Colors.redAccent, color: Colors.amber, icon: '@mipmap/ic_launcher', visibility: NotificationVisibility.public, // 显式配置渠道级锁屏可见性 lockscreenVisibility: NotificationVisibility.public, largeIcon: DrawableResourceAndroidBitmap('@mipmap/ic_launcher'), styleInformation: await getBigPictureStyle(notification), ); } } // 在NotificationService的init方法里补充权限申请逻辑 void init() async { // 原有初始化逻辑... // 主动申请Android 13+的通知权限 if (Platform.isAndroid) { final androidPlugin = notificationsPlugin .resolvePlatformSpecificImplementation< AndroidFlutterLocalNotificationsPlugin>(); await androidPlugin?.requestNotificationsPermission(); } }
验证步骤
- 发送一条测试通知后,长按通知进入应用通知设置页,确认「在锁屏上显示」开关处于开启状态
- 检查系统「锁屏通知」设置中,是否开启了「隐藏敏感通知内容」,如果开启,需要将当前应用加入显示白名单
- 确认后端发送FCM消息时,
android.priority字段设置为high,匹配本地通知的高优先级配置
内容的提问来源于stack exchange,提问作者Roxx
相关产品推荐
相关产品推荐

