flutter_local_notifications Android前台通知不显示问题求助
Flutter前台悬浮通知不显示(MIUI 13 + Android 12)解决思路
1. 先搞定MIUI专属的通知权限
小米的MIUI对前台通知卡得严,得手动开「悬浮通知」权限:
- 打开手机设置 → 应用设置 → 找到你的Flutter应用 → 通知管理
- 先把「允许通知」打开,然后找到「悬浮通知」,设为「允许」
- 顺带检查「锁屏通知」「横幅通知」也开着,有些MIUI版本这些设置是关联的
2. 补全Android 12+的前台服务权限
Android 12及以上对前台服务要求更高,得在AndroidManifest.xml里加权限:
<!-- 前台服务基础权限 --> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <!-- 通知类前台服务专属权限 --> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_NOTIFICATION" />
同时发通知时,必须把importance设为Importance.high,不然前台弹不出来:
var androidDetails = AndroidNotificationDetails( 'your_channel_id', 'your_channel_name', channelDescription: 'your_channel_desc', importance: Importance.high, // 这个是关键,必须设high priority: Priority.high, ongoing: true, // 标记成持续通知,防止系统自动清掉 );
3. 确保前台服务和通知绑定对了
Flutter里前台通知得和前台服务绑定,只发普通通知可能不行。可以试试先启动前台服务再发通知:
// 调用Android专属的前台服务启动API await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.startForegroundService( 1, // 通知ID NotificationDetails(android: androidDetails), );
4. 关掉MIUI的省电限制
MIUI的省电模式会掐前台通知:
- 打开设置 → 电池与性能 → 把省电模式关了
- 进「应用智能省电」,找到你的应用,设成「无限制」,别让系统后台限制通知
5. 检查通知渠道配置(Android 8+必做)
Android 8以上必须建通知渠道,而且渠道重要性得是high:
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel( AndroidNotificationChannel( 'your_channel_id', 'your_channel_name', description: 'your_channel_desc', importance: Importance.high, // 渠道重要性也得是high showBadge: true, ), );
渠道建好后得重启应用才生效,不用每次启动都创建,插件会自动处理,但第一次得确保创建成功。
6. 先测后台通知是否正常
如果后台状态下通知能正常显示,那就是前台权限或MIUI的限制问题;如果后台也不显示,先排查基础配置,比如渠道有没有创建、权限有没有申请到位。
内容的提问来源于stack exchange,提问作者Moaz El-sawaf
相关产品推荐
相关产品推荐

