Firebase 管理 SDK FCM 错误

Firebase admin SDK FCM error

我在 Dialogflow fulfilments 中使用 6.0.0 版的 firebase admin 来发送 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)

您的 JSON 将 "message" 作为其顶级密钥:

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

这是无效的。 "notification" 和 "token" 应该是顶级键:

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

有效样本请参考the documentation