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

Android 7.0及以上版本FCM推送通知在Release APK中失效求助

解决Android 7.0+版本Release包Firebase推送通知失效问题

首先,结合你提供的代码和现象(Debug正常、Release失效),我整理了几个最可能的原因和对应的解决方案:

1. Firebase依赖版本过于陈旧

你使用的firebase-messaging:10.2.0是2017年的旧版本,和Android 7.0+系统的兼容性存在已知问题,尤其是通知渠道、后台消息处理等逻辑。建议立即升级到Firebase Messaging的最新稳定版(可根据Firebase官方最新版本调整,当前推荐23.0.0系列):

// 替换为最新稳定版依赖
implementation 'com.google.firebase:firebase-core:21.1.1'
implementation 'com.google.firebase:firebase-messaging:23.0.0'

升级后记得同步Gradle,同时确保根目录build.gradle中的com.google.gms:google-services插件版本匹配(建议用4.3.15及以上)。

2. Oreo+通知渠道未关联到Notification Builder

你的代码里创建了通知渠道,但没有在NotificationCompat.Builder中指定对应的channelId——这在Release包的严格模式下会直接导致通知无法显示(Debug模式下系统兼容规则较宽松,可能侥幸生效)。修改你的通知构建代码:

// 针对不同版本初始化Builder,Oreo+必须指定channelId
NotificationCompat.Builder notificationBuilder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    String channelId = "projectname"; // 和你创建的渠道ID保持一致
    notificationBuilder = new NotificationCompat.Builder(this, channelId);
} else {
    notificationBuilder = new NotificationCompat.Builder(this);
}

// 后续Builder配置保持不变
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    notificationBuilder.setSmallIcon(R.drawable.ic_launcher_transparent);
} else {
    notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
}
notificationBuilder
    .setContentTitle("Projectname")
    .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(intent.getStringExtra("gcm.notification.body")))
    .setContentText(intent.getStringExtra("gcm.notification.body"))
    .setAutoCancel(true)
    .setDefaults(Notification.DEFAULT_SOUND)
    .setSound(soundUri)
    .setContentIntent(pendingIntent)
    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

另外建议把createNotificationManager()的调用移到Application初始化阶段,因为通知渠道只需要创建一次,无需每次构建通知都执行。

3. Release包ProGuard混淆破坏Firebase核心代码

Release包默认开启ProGuard,可能会混淆Firebase Messaging的核心类,导致消息接收或通知构建逻辑失效。在你的proguard-rules.pro中添加以下规则:

# Firebase核心类保护规则
-keep class com.google.firebase.** { *; }
-keep class com.google.android.gms.** { *; }
-keep class com.google.firebase.messaging.** { *; }
-keepattributes *Annotation*
-keepattributes Signature

# 保护你的Firebase消息处理子类(替换为你自己的类路径)
-keep class com.your.package.YourFirebaseMessagingService { *; }

4. Release包的Google服务配置错误

Debug和Release包使用的签名密钥不同,你需要确保Firebase控制台中已经添加了Release包的SHA-1指纹,并且下载了对应的google-services.json文件替换到项目中:

  1. 打开Firebase控制台,找到你的项目
  2. 进入「项目设置」→「应用」→「添加指纹」,输入Release包的SHA-1值
  3. 重新下载google-services.json,替换到项目的app/目录下
  4. 清理项目并重新构建Release包

按照以上步骤逐一排查,应该可以解决Release包推送通知失效的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:41:29