firebase - admin.messaging(...).send 不是函数

标签 firebase firebase-realtime-database firebase-cloud-messaging google-cloud-functions firebase-admin

我正在使用 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/

相关文章:

Firebase 为用户设置角色

java - Firebase不会触发数据更改事件

ios - 查询 Firebase 嵌套数据以检查特定值 - iOS Swift

ios - Firebase 云消息传递和 IOS 的优缺点

android - 使用 firebase 创建群组聊天 react-native

android - flutter 的 FCM 通知声音在发布中不起作用

javascript - React Native 中的 Firebase 可调用函数

firebase - 如何从 firebase 检索 x 条目?

ios - 闭包无法隐式捕获变异的 self 参数

firebase - 当数据库中发生时间事件时如何发送 FCM?