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

Flutter FCM使用问题:AndroidManifest配置、通知延迟及后台处理异常

你遇到的AndroidManifest.xml9处报错属于非正常问题,主要由旧版FCM冗余配置、必需配置项缺失导致,以下是所有问题的对应解决方案:

1. AndroidManifest.xml报错与「Missing Default Notification Channel」错误同步修复

你当前的配置文件中冗余了两个旧版FCM才需要的INSTANCE_ID_EVENT服务配置,同时缺失Android 8.0+强制要求的默认通知通道配置,删除冗余项+补充缺失项即可解决两类问题。
修正后的完整配置如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.stammi">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- 补充FCM必要权限,可选但能提升推送到达率 -->
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

   <application
        android:label="orga"
        android:icon="@mipmap/ic_launcher">
        <!-- 补充默认通知通道配置,解决Missing Default Notification Channel错误 -->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="default_notification_channel" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@mipmap/ic_launcher" />

        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
         
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <!-- 原有的两个INSTANCE_ID_EVENT服务是旧版FCM冗余配置,直接删除即可解决属性报错 -->
        
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

补充说明:修改配置后需要在Dart代码中提前创建ID为default_notification_channel的通知通道,Android 8.0+系统才能正常展示通知。

2. 通知偶尔延迟数分钟送达修复

导致该问题的常见原因和对应解决方式如下:

  • 推送时将Android消息优先级设置为high,高优先级消息会被FCM优先下发,不会进入节能延迟队列
  • 尽量使用data类型消息而非notification类型消息,收到消息后本地触发通知,notification类型消息会被系统施加更多节能限制
  • 国内ROM需要引导用户开启应用自启动权限、加入后台白名单、关闭电池优化,避免系统杀死应用进程阻断FCM长连接

3. onBackgroundMessage空指针异常修复

该异常90%以上是初始化逻辑错误导致,按以下规则调整即可:

  • 必须先等待Firebase.initializeApp()执行完成,再注册onBackgroundMessage回调
  • onBackgroundMessage的回调函数必须是顶级静态函数,不能是类实例方法、匿名函数
  • 不要在回调中对RemoteMessage参数直接使用空断言!,先做非空判断再处理字段内容

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 01:36:00