迁移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
相关产品推荐
相关产品推荐

