【问题标题】:Firebase Functions code deployed but not visible in consoleFirebase Functions 代码已部署但在控制台中不可见
【发布时间】:2020-09-29 15:14:09
【问题描述】:

我在使用 Firebase 函数时遇到问题。一旦我在终端中执行firebase deploy,我就会收到以下消息

=== Deploying to 'xxxxxxxx'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint D:\Flutter Projects\xxxxxxxx\xxxxxxxx\functions
> eslint .

+  functions: Finished running predeploy script.
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
+  functions: required API cloudfunctions.googleapis.com is enabled
i  functions: preparing functions directory for uploading...

+  Deploy complete!

Project Console: https://console.firebase.google.com/project/xxxxxxxx/overview

但是当我查看 Firebase Functions 控制台时,它显示“等待您的第一个实现”。

这是我在函数文件夹中的index.js 文件:

const admin = require("firebase-admin");

var serviceAccount = require("../functions/xxxxxxxx-firebase-adminsdk.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://xxxxxxxx.firebaseio.com"
});

var payload = {
    notification: {
      title: "Test title",
      body: "Test notification",
      image: ""
    },
  };

var options = {
    priority: "high",
    to: '/topics/all'
}

function sendPushNotification(payload){
    admin.messaging().sendToTopic("all", payload)
    .then((response) => {
        console.log("Successfully sent message: ", response);
        return null;
    })
    .catch((error) => {
        console.log("Error sending message: ", error);
    });
}

setInterval(() => {
  sendPushNotification(payload);
}, 3600000);

module.exports.sendPushNotification = sendPushNotification;

运行node index.js 时,代码按预期工作。我不确定我是否以正确的方式导出了函数,所以 Firebase Functions 可能不知道要部署哪个函数。目标是 Firebase Functions 每小时向所有设备发送一个通知(仅用于测试目的)。我怎样才能做到这一点?

注意:我检查了以下内容(根据this 线程):

  1. 实际上保存了我正在尝试部署的功能的文件。
  2. 文件位于正确的目录中。对于 JavaScript 项目,默认为 functions/index.js
  3. 我正在从正确的目录部署正确的项目。

【问题讨论】:

    标签: javascript node.js firebase


    【解决方案1】:

    您必须重构代码以使用scheduled function

    const functions = require('firebase-functions');
    const admin = require("firebase-admin");
    
    const serviceAccount = require("../functions/xxxxxxxx-firebase-adminsdk.json");
    
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: "https://xxxxxxxx.firebaseio.com",
    });
    
    const payload = {
      notification: {
        title: "Test title",
        body: "Test notification",
        image: "",
      },
    };
    
    const options = {
        priority: "high",
        to: '/topics/all'
    }
    
    exports.sendPushNotification = functions.pubsub.schedule('every 1 hour')
      .onRun((context) => {
        admin.messaging().sendToTopic("all", payload)
          .then((response) => {
            console.log("Successfully sent message: ", response);
            return null;
          })
          .catch((error) => {
            console.log("Error sending message: ", error);
            return null;
          });
      });
    

    【讨论】:

      猜你喜欢
      • 2019-07-04
      • 2019-03-03
      • 2017-10-10
      • 2018-08-07
      • 2022-09-28
      • 1970-01-01
      相关资源
      最近更新 更多