Firebase 云功能未发布

Firebase Cloud Function not Publishing

我正在尝试编写一个云函数,每次创建集合 "sensor-readings" 中的传感器读数时都会执行该函数:

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);


      exports.createNotification = functions.firestore
          .document('sensor-readings/{sensorId}')
          .onCreate((snap, context) => {

            const payload = {
              notification: {
                title: 'New news',
                body: "Body Test"
                }
            };

            // perform desired operations ...
          return  admin.messaging().sendToTopic("topic",payload);
          });

每次创建传感器读数时都会执行云函数,但是当我尝试使用 gcloud pubsub subscriptions pull --auto-ack MySub 为了测试函数的结果,没有消息发布到主题。

有什么想法吗? 谢谢

此代码与名为 Firebase Cloud Messaging 的产品配合使用,该产品用于向 Web 和移动应用程序发送消息:

admin.messaging().sendToTopic("topic",payload);

但是您的命令行正在使用 Google Cloud Pubsub,这是一个完全不同的产品:

gcloud pubsub subscriptions pull --auto-ack MySub

您不能使用 FCM 将消息发送到 pubsub 主题。同样,它们是完全不同的。而且您不能使用 gcloud 查看 FCM 消息发生了什么。您需要在您的网络或移动应用程序中执行此操作。

如果您想向 pubsub 主题发送消息,您应该使用 Google Cloud SDK,而不是 Firebase Admin SDK。