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

FCM实时数据库节点更新时推送通知不显示问题求助

排查FCM客户端无法显示通知的常见问题

嘿,既然FCM日志已经确认通知触发成功,那问题大概率出在客户端的配置或代码实现上了,咱们一步步来排查这些常见的坑:

1. 先确认有没有正确注册FirebaseMessagingService

FCM的通知接收(尤其是APP前台时的通知和纯数据消息)必须依赖一个继承自FirebaseMessagingService的子类,而且得在AndroidManifest里注册它。要是没做这一步,后台发的通知在APP前台时可能收不到,纯数据消息更是完全没法处理。

给你个示例代码参考:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        // 收到消息后手动处理通知展示
        if (remoteMessage.getNotification() != null) {
            showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
        }
        // 如果是数据消息,就处理remoteMessage.getData()里的内容
    }

    private void showNotification(String title, String body) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);

        String channelId = "default_fcm_channel";
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_notification) // 这里要替换成你自己的通知图标
                .setContentTitle(title)
                .setContentText(body)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // Android O及以上必须创建通知渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, "FCM通知渠道", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

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

然后别忘了在AndroidManifest.xml里注册这个服务:

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

2. 检查Android O+的通知渠道配置

从Android 8.0(API 26)开始,所有通知必须绑定一个通知渠道,要是没创建对应渠道,系统直接会把通知静默掉。你要确认:

  • 代码里创建的渠道ID,和FCM函数/控制台发送通知时指定的渠道ID一致(如果指定了的话)
  • 渠道的重要性不能设成IMPORTANCE_NONE,至少要IMPORTANCE_DEFAULT才能正常显示

3. 搞清楚FCM消息类型对展示的影响

FCM消息分两种,处理逻辑不一样:

  • 通知消息:APP在后台时,系统会自动帮你展示通知;但APP在前台时,必须在onMessageReceived里手动调用代码展示。
  • 数据消息:不管APP是前台还是后台,都得你自己在onMessageReceived里处理并手动弹出通知,系统不会管。

如果你的FCM函数发的是纯数据消息,那肯定得自己写展示逻辑,不然啥都看不到。

4. 确认MainActivity里的初始化是否到位

虽然Firebase一般会自动初始化,但还是建议在MainActivity的onCreate里加一段代码确认,顺便可以获取设备令牌用来测试:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    // 显式初始化Firebase(可选,但能避免一些奇怪的初始化问题)
    FirebaseApp.initializeApp(this);
    
    // 获取FCM设备令牌,用来测试发送通知
    FirebaseMessaging.getInstance().getToken()
            .addOnCompleteListener(task -> {
                if (!task.isSuccessful()) {
                    Log.w("FCM_TAG", "获取令牌失败", task.getException());
                    return;
                }
                String token = task.getResult();
                Log.d("FCM_TAG", "设备令牌:" + token);
            });
}

5. 检查权限配置是否齐全

AndroidManifest里必须加这些权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <!-- Android 13+ 必须 -->

而且对于Android 13及以上的设备,还得动态申请通知权限,不然系统会直接拒绝:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
        registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
            if (isGranted) {
                // 权限拿到了,可以正常接收通知
            } else {
                // 用户拒绝了权限,得提示开启
            }
        }).launch(Manifest.permission.POST_NOTIFICATIONS);
    }
}

6. 最后检查设备端的设置

有时候问题不在代码里,在设备上:

  • 去设备设置里确认你的APP通知权限已经开启(设置 → 应用 → 你的APP → 通知)
  • 看看是不是开了省电模式或者后台限制,这些会阻止FCM服务在后台接收消息
  • 用刚才获取的设备令牌手动发一条测试通知,看看能不能收到,能收到令牌说明FCM连接是正常的

要是以上步骤都排查完还是不行,你可以把你的MyFirebaseMessagingService完整代码,还有FCM函数发送消息的代码贴出来,这样能更精准定位问题~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:36:16