Firebase管理员SDK FCM错误

时间:2018-08-14 23:06:02

标签: javascript firebase firebase-cloud-messaging firebase-admin

我在Dialogflow实现中使用firebase admin的6.0.0版本发送fcm通知。

admin.messaging().send({
    "message": {
        "notification": {
            "title": "Portugal vs. Denmark",
            "body": "great match!"
        },
        "token": user_info.device_id
    }
})
.then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
})
.catch((error) => {
    console.log('Error sending message:', error);
});

这是我遇到的错误

Error: Exactly one of topic, token or condition is required
at FirebaseMessagingError.Error (native)
at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28)
at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:241:16)
at validateMessage (/user_code/node_modules/firebase-admin/lib/messaging/messaging.js:320:15)
at Messaging.send (/user_code/node_modules/firebase-admin/lib/messaging/messaging.js:461:9)
at snapshot.forEach (/user_code/index.js:130:19)
at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:4255:20
at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:3669:24
at LLRBNode.inorderTraversal (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:2606:13)

1 个答案:

答案 0 :(得分:3)

您的JSON具有“消息”作为其顶级密钥:

{
    "message": {
        "notification": {
            "title": "Portugal vs. Denmark",
            "body": "great match!"
        },
        "token": user_info.device_id
    }
}

那是无效的。 “通知”和“令牌”应该是顶级键:

{
    "notification": {
        "title": "Portugal vs. Denmark",
        "body": "great match!"
    },
    "token": user_info.device_id
}

有关有效示例,请参阅the documentation