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

Android使用FCM Data类型推送通知空白、onMessageReceived未触发如何解决?

FCM Data类型推送展示异常问题修复方案

问题背景

使用FCM服务整体运行正常,通过Data类型下发推送通知,但展示环节出现异常,暂时无法定位问题原因。

相关代码

客户端(Android)代码

public class CloudMessaging extends FirebaseMessagingService {

String NOTIFICATION_CHANNEL_ID  = "Messages_Channel_ID";

@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    createNotificationChannel();

    Map<String, String> data = remoteMessage.getData();
    final String title = data.get("title");
    final String body = data.get("body");
    final String conversation = data.get("conversation");

    if (conversation == null) return;

    NotificationCompat.Builder builder = new 
    NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID);
    builder.setColor(Color.CYAN)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true);

    NotificationManager notificationManager = (NotificationManager)
            getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());

}

private void createNotificationChannel() {
   
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.message_channel_name);
        String description = getString(R.string.message_channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel messagesChannel = new 
        NotificationChannel(getString(R.string.Message_Notification_ID), name, importance);
        messagesChannel.setDescription(description);
        messagesChannel.enableVibration(true);
        messagesChannel.setVibrationPattern(new long[]{1000, 200, 500});
        messagesChannel.enableLights(true);
        messagesChannel.setLightColor(Color.CYAN);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(messagesChannel);
    }
}

服务端代码

const data = {
    token: context.params.token,
    data: {
      title: document.username,
      body: document.message,
      conversation: document.conversationid,
    },
    android: {
      notification: {
        channel_id: "Messages_Channel_ID",
      },
    },
  };

问题表现

  • 所有SDK版本的设备上收到的通知都呈现异常状态
  • 无论应用处于前台还是后台运行状态,onMessageReceived方法始终不会被触发,已在清单文件中完成该服务类的声明
  • 当前展示的空白通知是因为清单文件中配置了默认的通知颜色和图标,若未配置该参数,大概率不会显示任何通知

修复方案

1. 修正通知通道ID不匹配问题

代码中定义的通知通道常量NOTIFICATION_CHANNEL_ID值为Messages_Channel_ID,但创建通道时传入的是R.string.Message_Notification_ID对应的字符串,二者不一致会导致通知无法匹配到正确的通道,无法正常展示。修改创建通道的代码,统一使用已定义的常量即可:

// 替换原创建通道的入参为已定义的常量
NotificationChannel messagesChannel = new 
NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);

2. 移除服务端payload中的android.notification配置

目前需求是通过纯Data类型下发推送,由客户端自行处理通知展示,但服务端代码中同时添加了android.notification配置,这种混合类型的推送在任何状态下都不会触发客户端的onMessageReceived方法,而是直接由FCM SDK在系统托盘中生成通知,因没有在notification节点配置title和body,所以只会展示默认配置的空白通知。修改服务端代码,移除android.notification字段即可:

const data = {
    token: context.params.token,
    data: {
      title: document.username,
      body: document.message,
      conversation: document.conversationid,
    }
  };

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 04:39:03