You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter后台/终止状态推送通知在AVD正常,真机异常

解决安卓真机后台/终止状态下Firebase推送接收失败的问题
  • 关闭设备电池优化
    国内安卓厂商(小米、华为、OPPO、vivo等)默认会严格限制后台应用的唤醒权限,直接导致推送无法触发。去「设置」-「电池与性能」-「应用耗电管理」找到你的App,设置为「无限制」;同时在应用权限里开启「自启动权限」,避免App被系统彻底杀死。

  • 切换FCM消息类型为数据消息
    通知消息在App后台/终止时由系统托盘处理,极易被厂商拦截;而数据消息会触发onBackgroundMessage回调,由Flutter Local Notifications手动展示,稳定性更高。推送payload示例:

    {
      "data": {
        "title": "推送标题",
        "body": "推送内容",
        "click_action": "FLUTTER_NOTIFICATION_CLICK"
      },
      "to": "真机当前的FCM令牌"
    }
    
  • 配置Android 8.0+通知渠道
    Android 8.0及以上必须创建合法的通知渠道,否则后台通知无法显示。初始化Flutter Local Notifications时务必添加渠道创建逻辑:

    const AndroidNotificationChannel channel = AndroidNotificationChannel(
      'high_importance_channel',
      '高优先级通知渠道',
      description: '用于接收重要推送通知',
      importance: Importance.max,
      playSound: true,
    );
    
    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
        FlutterLocalNotificationsPlugin();
    
    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);
    
  • 确认真机FCM令牌有效性
    真机和AVD的FCM令牌是独立的,推送时必须使用真机当前的有效令牌。在App启动时打印令牌验证:

    FirebaseMessaging.instance.getToken().then((token) {
      print('FCM Token: $token');
    });
    
  • 排查网络环境
    部分设备后台会切换到低功耗网络,导致FCM长连接断开。确保设备处于稳定的WiFi或4G网络,避免使用受限的公共网络。

  • 禁用厂商后台限制机制
    部分厂商有单独的后台管控功能,比如小米「神隐模式」、华为「后台活动限制」。进入App的应用信息页面,找到「后台活动」选项,设置为允许后台运行。

内容的提问来源于stack exchange,提问作者BigLoloPette

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 07:03:31