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

迁移FCM HTTP V1 API时,已设Topic仍触发必填字段错误求助

解决FCM Admin SDK发送Topic消息时的"Exactly one of topic, token or condition is required"错误

问题出在你混淆了FCM HTTP V1 API的请求格式和Firebase Admin SDK的消息参数格式:

  • FCM HTTP V1 API的请求体确实需要外层的message嵌套字段
  • 但Firebase Admin SDK的messaging().send()方法不需要这一层嵌套,直接传入包含topic、notification等核心字段的对象即可

你的代码里给send()方法传了一个包含message子对象的结构,导致SDK无法识别到topic字段,触发了参数验证错误。

修正后的云函数代码

const admin = require('firebase-admin');

admin.initializeApp({
    credential: admin.credential.applicationDefault(),
});

exports.myUpdate = onDocumentUpdated("{mypaths}", (event) => {
  
 let message = {
            "topic": "news",
            "notification": {
                "title": "Breaking News",
                "body": "New news story available."
            },
            "data": {
                "story_id": "story_12345"
            },
            "android": {
                "notification": {
                    "click_action": "TOP_STORY_ACTIVITY"
                }
            },
            "apns": {
                "payload": {
                    "aps": {
                        "category" : "NEW_MESSAGE_CATEGORY"
                    }
                }
            }
    }

    admin.messaging().send(message).then((response) => {
        console.log('Successfully sent message:', response);
    }).catch((error) => {
        console.log('Error sending message:', error);
    });
})

额外注意事项

  • 确保Firebase Admin SDK为最新版本,避免旧版本兼容性问题
  • 确认设备已成功订阅news主题,可通过messaging().subscribeToTopic()的返回结果验证
  • 替换触发器路径{mypaths}为实际的Firestore集合路径

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 05:00:54