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

Firebase推送通知无法接收求助:已按教程配置仍失效

解决Firebase推送通知无法接收的问题

嘿,我看了你的代码和清单配置,一下子就发现核心问题了——你的两个Firebase服务类都是空实现,而且其中一个还已经被废弃了!这就是为什么你严格按教程走还是收不到推送的关键原因。下面给你一步步梳理问题和解决方案:

问题分析

  • 空的MyFirebaseMessagingService:这个类是处理推送消息的核心,但你没有重写关键的onMessageReceived方法,系统收到推送后根本不知道该执行什么逻辑,自然不会有任何反馈。
  • 过时的FirebaseInstanceIdService:这个类在Firebase SDK 21.0.0版本之后就被废弃了,现在已经不再推荐使用,令牌更新的逻辑需要迁移到新的回调方法中。

修正后的代码实现

1. 重写MyFirebaseMessagingService(核心!)

这个类需要实现onMessageReceived来处理收到的消息,同时替换旧的InstanceId逻辑:

package com.example.firebasenf.firebasenf;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MyFirebaseMsgService";
    private static final String CHANNEL_ID = "firebase_notification_channel";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.d(TAG, "收到推送消息: " + remoteMessage.getNotification().getBody());
        
        // 调用显示通知的方法(Android 8.0+需要先创建通知渠道)
        sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
    }

    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        Log.d(TAG, "刷新后的设备令牌: " + token);
        // 这里可以把令牌上传到你的服务器,用于定向推送
    }

    private void sendNotification(String title, String messageBody) {
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        
        // Android 8.0及以上需要创建通知渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                    "Firebase推送通知渠道",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, CHANNEL_ID)
                        .setSmallIcon(R.drawable.ic_notification) // 替换成你的通知图标
                        .setContentTitle(title)
                        .setContentText(messageBody)
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build());
    }
}

2. 删除过时的FirebaseInstanceIdService

直接删掉MyFirebaseInstanceIDService这个类,因为我们已经在MyFirebaseMessagingService里用onNewToken方法替代了它的功能。

3. 更新Manifest配置

删除原来的MyFirebaseInstanceIDService相关配置,只保留MyFirebaseMessagingService即可:

<service android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

额外检查点

  • 确认google-services.json文件已经正确放置在你的app模块根目录下
  • 检查app级build.gradle中的Firebase依赖是否正确(建议使用最新稳定版):
    implementation 'com.google.firebase:firebase-messaging:23.0.0' // 可替换为最新版本
    
  • 测试推送时注意:Android 8.0+必须创建通知渠道才能显示通知;后台推送的行为和前台略有不同,后台时系统会直接显示通知而不会触发onMessageReceived
  • 可以通过Log打印的令牌,在Firebase控制台的测试推送功能中手动输入令牌测试,确保令牌有效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:39:23