我正在使用 firebase-admin 通过 Cloud Functions 发送推送通知,但我不断收到此错误:
TypeError: admin.messaging(...).send is not a function
at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:43:27)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36)
at /var/tmp/worker/worker.js:700:26
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
知道为什么它不起作用吗?我使用的是 firebase-admin 版本 5.9.1,所以它是最新版本。它应该包含 .send 函数,但它仍然不起作用。我已经卸载了该插件并重新安装了它,但仍然不起作用。
这是我正在使用的代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.cert({
projectId: "xxx",
clientEmail: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cab2b2b28ab2b2b2e4a3aba7e4adb9afb8bca3a9afaba9a9a5bfa4bee4a9a5a7" rel="noreferrer noopener nofollow">[email protected]</a>',
privateKey: '-----BEGIN PRIVATE KEY-----<KEY>-----END PRIVATE KEY-----\n'
}),
databaseURL: "https://xxx.firebaseio.com"
});
exports.sendNotification = functions.database.ref("xxx").onWrite((event) => {
console.log("notify");
const registrationToken = "xxx";
// See documentation on defining a message payload.
var message = {
notification: {
title: "You have a new service request",
body: "this is the main body"
},
data: {
score: '850',
time: '2:45'
},
token: registrationToken
};
return admin.messaging().send(message)
.then(function (response) {
console.log("Successfully sent message:", response);
})
.catch(function (error) {
console.log("Error sending message:", error);
});
});
我做错了什么?
最佳答案
应该是sendToDevice
而不是send
,也许是这样的:
var message = {
notification: {
title: "You have a new service request",
body: "this is the main body"
},
data: {
score: '850',
time: '2:45'
}
};
return admin.messaging().sendToDevice(registrationToken, message)
.then(function (response) {
console.log("Successfully sent message:", response);
})
.catch(function (error) {
console.log("Error sending message:", error);
});
关于firebase - admin.messaging(...).send 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49120814/