Cordova安卓应用FCM推送通知接收/显示异常求助
问题根源
- 崩溃原因:
BackgroundServiceStartNotAllowedException是Android 12(API 31)及以上系统的强制限制——后台状态下不允许启动传统后台服务。你的应用里的GCMBroadcastReceiver在应用后台/关闭时触发GCMIntentService启动,直接触发系统拦截,导致推送接收流程崩溃,消息直接丢失。 - 推送送达问题:FCM返回成功但客户端收不到,一是因为接收组件崩溃导致消息没被处理;二是Android 12+对后台/闲置状态的应用限制极严,旧的GCM兼容组件根本没法正常唤醒处理消息。
解决步骤
1. 替换旧FCM插件
当前使用的FCM插件依赖已废弃的GCM组件,必须换成适配Android 12+的版本:
- 先卸载旧插件:
cordova plugin remove 你的旧FCM插件名 - 安装适配新版系统的插件(已移除GCM依赖):
cordova plugin add cordova-plugin-fcm-with-dependecy-updated
2. 清理并更新AndroidManifest.xml配置
删掉所有GCM相关声明,替换为FCM标准配置:
- 移除以下旧代码:
<receiver android:name="com.google.android.gms.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE"/> <action android:name="com.google.android.c2dm.intent.REGISTRATION"/> <category android:name="你的应用包名"/> </intent-filter> </receiver> <service android:name="你的GCMIntentService实现类"/> - 添加FCM官方接收组件(新版插件会自动生成,建议手动核对):
<receiver android:name="com.google.firebase.messaging.FirebaseMessagingReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE"/> <action android:name="com.google.android.c2dm.intent.REGISTRATION"/> <category android:name="${applicationId}"/> </intent-filter> </receiver> <service android:name="com.google.firebase.messaging.FirebaseMessagingService" android:exported="false"> <intent-filter android:priority="-500"> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> - 补充Android 13+必需的通知权限:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
3. 适配后台消息处理逻辑
Android 12+禁止后台启动传统服务,处理自定义数据消息时需调整实现:
- 自定义消息处理类继承
JobIntentService,在onHandleWork方法中处理消息,避免直接启动后台服务。 - 发送数据消息时设置
priority: "high",提升送达优先级(通知消息的优先级由系统控制)。
4. 优化系统层面限制
- 引导用户为应用开启忽略电池优化权限:Android 12+设备闲置时会限制后台应用唤醒,开启该权限能提升推送送达率。
- Android 13+设备需主动请求
POST_NOTIFICATIONS权限,否则系统会拦截通知显示。
验证方法
- 编译更新后的应用,安装到Android 12+设备上。
- 关闭应用并闲置设备一段时间,发送FCM通知测试送达情况。
- 查看Crashlytics,确认
BackgroundServiceStartNotAllowedException崩溃是否消失。
内容的提问来源于stack exchange,提问作者fez
相关产品推荐
相关产品推荐

