强制杀死/停止应用后无法收到 FCM 推送

描述

App 首次安装未启动时,或 App 被强制停止后,无法收到 FCM 推送。

09-17 11:50:34.298 W/GCM ( 2025): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.rongcloud.fcm_demo (has extras) }

log 中显示 result=CANCELLED,表示广播的intent发送回调是 CANCELLED 状态,也就是说这个广播还没发送到 App / SDK 层,就已经被系统拦截了。

排除 FCM 的权限问题、manifest 配置问题、和 App 的通知权限问题后,问题依然存在。

分析(根因分析、需求分析)

Android 官方表示该行为符合预期。首次安装但尚未启动的应用以及被用户手动停止(在“管理应用”中)时,应用会处于停止状态。 系统会将 FLAG_EXCLUDE_STOPPED_PACKAGES 添加到所有广播 intent 。这样做是为了防止来自后台服务的广播无意中或不必要地启动已停止应用的组件。详见 Android 官方文档: 对已停止的应用启动控制

Stack Overflow 上的以下答案同样解释了该问题:

It looks like you have an issue when your app is force stopped or killed. Actually, this is working as intended. The Android framework advises that apps that have been stopped (i.e. killed/force-stopped from Settings) should not be started without explicit user interaction. FCM follows this recommendation and thus its services will not start as well. This also means that messages will not be received (FirebaseMessagingService will not be called) when the app is in “killed” state. Here are some useful links so you could have a better understanding on this topic:

解决方案

以下方案来自 Android 官方文档: 对已停止的应用启动控制

请注意,系统会将 FLAG_EXCLUDE_STOPPED_PACKAGES 添加到所有广播 intent 。这样做是为了防止来自后台服务的广播无意中或不必要地启动已停止应用的组件。后台服务或应用可以通过向应允许激活已停止应用的广播 intent 添加 FLAG_INCLUDE_STOPPED_PACKAGES 标志来替换此行为。

更多支持

如有疑问,欢迎提交工单