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

Firebase Legacy FCM API迁移HTTP v1遇JSON请求400错误求助

解决FCM HTTP v1 API 400错误:调整data与平台配置结构

问题核心是FCM v1 API对消息结构做了严格拆分,旧版API允许在data字段里混放平台专属配置(android、webpush),但v1 API要求这些配置必须放在对应的顶级节点下,message.data只能存纯键值对的自定义业务数据。

具体调整步骤:

  • 拆分原请求的data内容:
    • 把业务相关的自定义键值对(比如业务ID、状态标识)保留在message.data中,格式必须是string: string的键值对,不能嵌套复杂对象
    • 原data里的android配置(比如优先级、通道ID、本地通知样式)移到message.android节点下
    • 原data里的webpush配置(比如推送优先级、浏览器通知选项)移到message.webpush节点下

错误与正确请求结构对比:

错误结构(导致400):

{
  "message": {
    "token": "xxx",
    "notification": { "title": "测试", "body": "测试内容" },
    "data": {
      "android": { "priority": "high" },
      "webpush": { "headers": { "Urgency": "high" } },
      "order_id": "12345"
    }
  }
}

正确结构:

{
  "message": {
    "token": "xxx",
    "notification": { "title": "测试", "body": "测试内容" },
    "data": {
      "order_id": "12345"
    },
    "android": {
      "priority": "high",
      "notification": { "channel_id": "order_notify" }
    },
    "webpush": {
      "headers": { "Urgency": "high" }
    }
  }
}

C#代码示例:

用匿名类型构建符合要求的请求对象:

var fcmPayload = new
{
    message = new
    {
        token = "DEVICE_TOKEN",
        notification = new
        {
            title = "订单更新",
            body = "您的订单已发货"
        },
        data = new Dictionary<string, string>
        {
            { "order_id", "12345" },
            { "status", "shipped" }
        },
        android = new
        {
            priority = "high",
            notification = new
            {
                channel_id = "order_status_channel"
            }
        },
        webpush = new
        {
            headers = new
            {
                Urgency = "high"
            }
        }
    }
};

// 序列化并发送请求(示例用HttpClient)
var json = JsonSerializer.Serialize(fcmPayload);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("https://fcm.googleapis.com/v1/projects/YOUR_PROJECT_ID/messages:send", content);

额外注意:

如果旧版请求中data里包含平台专属的嵌套data(比如android.data),不要放到message.data,而是移到message.android.data节点下,保持平台配置的独立性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 16:07:43