Android 13(CompileSDK 33)下FirebaseMessagingService的onMessageReceived未触发
Android 13 下 FirebaseMessagingService onMessageReceived 未触发的解决思路
针对你升级CompileSDK 33后,仅Android 13设备上Firebase推送onMessageReceived不触发的问题,结合你已完成的配置,可从以下几个关键方向排查:
1. 升级 Firebase Messaging 版本
你当前使用的com.google.firebase:firebase-messaging:23.2.1发布于2022年,未完全适配Android 13的通知权限与系统行为。建议升级至23.3.0及以上稳定版本,新版本修复了多个Android 13相关的推送兼容问题:
implementation 'com.google.firebase:firebase-messaging:23.3.0' // 或最新稳定版
2. 完成 POST_NOTIFICATIONS 运行时权限申请
Android 13要求POST_NOTIFICATIONS必须通过运行时请求获取,仅在Manifest中声明无效。需在应用启动后主动发起权限请求(例如主Activity的onCreate中):
// Kotlin 示例 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1001) } }
若用户未授予该权限,系统会直接拦截通知类消息,导致onMessageReceived(前台场景)或通知展示(后台场景)失效。
3. 优化推送 Payload 配置
你的Payload存在冗余配置,且不符合Android 13规范:
- 移除
android.notification下的android_channel_id字段,仅保留channel_id即可; - 若需后台消息也触发
onMessageReceived,可改为纯Data消息(移除顶层notification字段),或添加android.priority: "high"配置; content_available仅适用于iOS静默推送,Android场景下可移除。
调整后的Payload示例:
{ "data": { "base64": "ewkiaWQi", "title": "XYZ ABC 2023", "body": "Bla blu blub.", "click_action": "OPENXYZAPP", "sound": "default", "tag": "2303" }, "to": "/topics/debug2023", "android": { "priority": "high", "notification": { "click_action": "OPENXYZAPP", "channel_id": "de.einszweidrei.XYZ2018.notification" } } }
4. 确保通知渠道提前创建
Android 13要求通知渠道必须在应用启动时提前创建,而非接收消息时才创建。建议在Application的onCreate中完成渠道初始化:
// Kotlin 示例 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel( "de.einszweidrei.XYZ2018.notification", "默认通知渠道", NotificationManager.IMPORTANCE_DEFAULT ) val notificationManager = getSystemService(NotificationManager::class.java) notificationManager.createNotificationChannel(channel) }
5. 检查厂商后台限制
部分Android 13设备厂商(如小米、华为)存在严格的后台管理策略,若应用被设置为“后台受限”,Firebase Messaging服务无法正常接收消息。需引导用户:
- 开启应用的“后台活动”权限;
- 关闭“电池优化”中的应用省电策略。
内容的提问来源于stack exchange,提问作者aqm
相关产品推荐
相关产品推荐

