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

更换Firebase账号后Android FCM推送通知无法接收排查

FCM Android推送无法接收问题排查

问题描述

  • 已完成Firebase依赖添加、控制台项目创建、应用关联,初始阶段推送功能可正常运行
  • 切换关联至第二个Firebase账号后出现无法接收通知的问题,删除第二个账号下对应项目、替换回原账号的google-services.json配置文件、重新关联原账号后,推送功能仍无法使用
  • 全程无报错抛出,已确认Gradle无依赖缺失问题

现有实现代码

消息接收服务代码

package com.crazy.pushnotifications

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import android.widget.RemoteViews
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

class FirestoreNotification : FirebaseMessagingService() {

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        if (remoteMessage.getNotification() != null) {
            // 从FCM直连推送中获取标题和内容
            showNotification(
                remoteMessage.notification!!.title!!,
                remoteMessage.notification!!.body!!
            )
        }

    }

    private fun getCustomDesign(title: String, message: String): RemoteViews {
        val remoteViews = RemoteViews(
            applicationContext.packageName,
            R.layout.layout_notification
        )
        remoteViews.setTextViewText(R.id.title, title);
        remoteViews.setTextViewText(R.id.message, message);
        remoteViews.setImageViewResource(
            R.id.icon,
            R.drawable.ic_baseline_fingerprint_24
        );
        return remoteViews;
    }

    private fun showNotification(title: String, body: String) {
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val channelId = "notification_channel"
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
        var builder = Notification.Builder(applicationContext, channelId)
            .setSmallIcon(R.drawable.ic_baseline_fingerprint_24)
            .setAutoCancel(true)
            .setOnlyAlertOnce(true)
            .setContentIntent(pendingIntent)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            builder = builder.setContent(
                getCustomDesign(title, body)
            );
        } else {
            builder = builder.setContentTitle(title)
                .setContentText(body)
                .setSmallIcon(R.drawable.ic_baseline_fingerprint_24)
        }

        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        // Android 8.0及以上需要创建通知渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationChannel = NotificationChannel(
                channelId, "CTQ",
                NotificationManager.IMPORTANCE_HIGH
            )
            notificationManager.createNotificationChannel(notificationChannel)

            notificationManager.notify(0, builder.build())
        }
    }
}

MainActivity代码

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

AndroidManifest.xml配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.crazy.pushnotifications">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Pushnotifications"
        tools:targetApi="31">

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".FirestoreNotification"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>

</manifest>

问题修复点

按优先级逐一排查修复即可:

  • 通知展示逻辑bug:现有showNotification方法把通知弹出的notify()调用写在了Android 8.0+的版本判断分支里,8.0以下的设备完全不会执行通知弹出逻辑。需要把notificationManager.notify(0, builder.build())挪到版本判断外面,渠道创建逻辑仅保留给8.0及以上系统即可。
  • FCM令牌失效:切换Firebase账号的操作会导致本地缓存的旧FCM注册令牌和原项目的绑定关系失效,切回原账号后如果没有刷新令牌,服务端向旧令牌推送自然无法送达。在应用启动时主动调用FirebaseMessaging.getInstance().token获取当前最新的有效令牌,测试推送时使用该新令牌即可。
  • 构建缓存残留:替换google-services.json文件后如果没有清理构建缓存,Android Studio可能仍会把旧账号的配置打包进APK。先执行Clean Project、Rebuild Project操作,同时卸载测试设备上已安装的旧版本应用,再重新安装新构建的APK测试。
  • 高版本通知权限缺失:Android 13(API 33)及以上系统需要动态申请POST_NOTIFICATIONS运行时权限,否则应用默认没有通知栏展示权限,即使收到消息也无法弹出通知。需要在MainActivity中补充该权限的申请逻辑,用户授权后再测试推送。
  • PendingIntent适配问题:现有PendingIntent的创建逻辑没有适配Android 12及以上系统的可变性要求,高版本系统上可能创建失败导致通知无法弹出。创建PendingIntent时补充对应flag即可,参考写法:
val pendingIntentFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
} else {
    PendingIntent.FLAG_UPDATE_CURRENT
}
val pendingIntent = PendingIntent.getActivity(this, 0, intent, pendingIntentFlags)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 17:39:43