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

Marshmallow(API Level 23)下Firebase Messaging无法正常工作求助

解决Firebase Messaging无法触发Token刷新和消息接收的问题

Hey there, let's work through this Firebase Messaging issue step by step—this stuff trips up a lot of devs, so we'll nail it down together.

1. 先换掉废弃的类!这是最常见的坑

First off, FirebaseInstanceIdService is deprecated now. If you're still extending that class, that's probably a huge part of your problem. The modern approach uses FirebaseMessagingService instead:

  • Replace your FirebaseInstanceIdService subclass with a class extending FirebaseMessagingService
  • Use onNewToken() instead of onTokenRefresh() to handle token updates
  • onMessageReceived() also lives in FirebaseMessagingService—make sure your method is properly overridden here

Here's what the correct service class should look like (Kotlin example):

class MyFirebaseMessagingService : FirebaseMessagingService() {
    override fun onNewToken(token: String) {
        super.onNewToken(token)
        // 在这里处理Token刷新,比如打印日志、上传到你的服务器
        Log.d("FCM_DEBUG", "新Token已生成: $token")
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)
        // 处理收到的消息
        val msgTitle = remoteMessage.notification?.title ?: "无标题"
        val msgContent = remoteMessage.notification?.body ?: "无内容"
        Log.d("FCM_DEBUG", "收到消息: $msgTitle - $msgContent")
    }
}

2. 检查AndroidManifest.xml的配置是否正确

你的Service必须在Manifest里正确声明,还要加上对应的Intent Filter。确保在<application>标签内有这段代码:

<service
    android:name=".MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
  • 仔细核对android:name是否和你的Service类路径完全一致
  • 别漏了com.google.firebase.MESSAGING_EVENT这个Action——这是触发Service的关键

3. 验证Firebase配置文件(google-services.json)

  • 确保你从Firebase控制台下载的是最新的google-services.json,并且放在了项目的app/目录下
  • 检查json文件里的package_name是否和你app的applicationId完全匹配(可以在Module级别的build.gradle里查看applicationId)

4. 检查依赖版本和项目配置

在Module级别的build.gradle(app目录下)里,确保使用的是最新的Firebase Messaging依赖:

dependencies {
    implementation 'com.google.firebase:firebase-messaging:23.4.0' // 换成当前最新版本即可
}

同时,项目级别的build.gradle里要包含Google Services插件:

buildscript {
    dependencies {
        classpath 'com.google.gms:google-services:4.3.15' // 同样用最新版本
    }
}

别忘了在Module级别的build.gradle底部应用插件:

apply plugin: 'com.google.gms.google-services'

5. 用正确的方式测试Token获取和消息接收

  • 手动触发Token刷新测试:可以在Activity或App启动时调用FirebaseMessaging.getInstance().token,这会获取当前Token,同时触发onNewToken():
FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
    if (!task.isSuccessful) {
        Log.w("FCM_DEBUG", "获取Token失败", task.exception)
        return@addOnCompleteListener
    }
    val token = task.result
    Log.d("FCM_DEBUG", "当前Token: $token")
}
  • 测试消息推送时注意:
    • 前台状态下:onMessageReceived()会立即触发
    • 后台状态下:如果消息是notification payload,会直接显示在系统通知栏,只有用户点击通知时才会触发onMessageReceived();如果想让后台也触发这个方法,改用data payload(或者混合payload自己处理)

6. 排除设备/环境问题

  • 确保测试设备上的Google Play Services是安装并更新到最新版本的(FCM依赖这个)
  • 如果用模拟器,一定要选带Google APIs的镜像,不要用纯Android模拟器
  • 检查Manifest里是否有INTERNET权限(Firebase通常会自动添加,但还是确认一下):
<uses-permission android:name="android.permission.INTERNET" />

7. 检查Firebase控制台设置

  • 确认你的App已经正确注册到Firebase控制台(查看「项目设置」>「应用」)
  • 如果给特定设备发测试消息,确保用的是日志里打印的正确Token(复制Token到控制台的「发送测试消息」工具里)

试试这些步骤吧——大部分时候问题都是废弃类、Manifest配置错误,或者配置文件过时导致的。如果还有问题,随时再细化排查!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 11:03:41