Firebase 每天在Firestore中执行功能

Firebase 每天在Firestore中执行功能,firebase,google-cloud-firestore,google-cloud-functions,Firebase,Google Cloud Firestore,Google Cloud Functions,我有这个功能 exports.webhook = functions.https.onRequest((_req: any, res: { send: (arg0: string) => void; }) => { // It sends notification to a user }); 我希望这个功能每天下午3点执行。此时如何安排函数每天运行 编辑1 我有一个发送通知的函数,函数名是sendNotifications,如何从URL调用这个函数并传递有效负载变量titl

我有这个功能

exports.webhook = functions.https.onRequest((_req: any, res: { send: (arg0: string) => void; }) => {
    // It sends notification to a user
});
我希望这个功能每天下午3点执行。此时如何安排函数每天运行

编辑1 我有一个发送通知的函数,函数名是sendNotifications,如何从URL调用这个函数并传递有效负载变量title和body

示例工作代码:

exports.sendNotifications = functions.firestore
      const payload = {
        notification: {
         title: no_of_followers2,
         body: desc + '  Notification body',
         icon: 'https://img.icons8.com/material/4ac144/256/user-male.png',
         click_action: `https://google.com`,
        }
      };
  ... // some code
const subscriber = doc.data().token;
return admin.messaging().sendToDevice(subscriber, payload);
编辑2 我的职能:

exports.sendNoti_cal_log = functions.https.onRequest((_req: any, res: { send: (arg0: string) => void; }) => {
      const payload = {
        notification: {
         title: 'Notification Title',
         body: 'Notification body',
         icon: 'https://img.icons8.com/material/4ac144/256/user-male.png',
         click_action: `https://google.com`,
        }
      };
      const subscriber = "evGBnI_klVQYSBIPMqJbx8:APA91bEV5xOEbPwF4vBJ7mHrOskCTpTRJx0cQrZ_uxa-QH8HLomXdSYixwRIvcA2AuBRh4B_2DDaY8hvj-TsFJG_Hb6LJt9sgbPrWkI-eo0Xtx2ZKttbIuja4NqajofmjgnubraIOb4_";
      return admin.messaging().sendToDevice(subscriber, payload)
    });
exports.sendNoti_cal_log = functions.https.onRequest((req: any, res: { status: (arg0: number) => { (): any; new(): any; send: { (arg0: { status: string; }): void; new(): any; }; }; }) => {
  const param1 = req.params.param1;    

  const payload = {
        notification: {
         title: 'Notification Title'+param1,
现在,当我从URL调用此函数时,它可以工作,但我没有得到任何响应,请参见屏幕截图:

我需要做的最后一件事是,如何从参数传递标题并在函数中接收它

编辑3 我的工作Https功能

exports.webhook = functions.https.onRequest((_req: any, res: { send: (arg0: string) => void; }) => {
    // It sends notification to a user
});
编辑4个传递参数 我在函数中传递了如下参数:

exports.sendNoti_cal_log = functions.https.onRequest((_req: any, res: { send: (arg0: string) => void; }) => {
      const payload = {
        notification: {
         title: 'Notification Title',
         body: 'Notification body',
         icon: 'https://img.icons8.com/material/4ac144/256/user-male.png',
         click_action: `https://google.com`,
        }
      };
      const subscriber = "evGBnI_klVQYSBIPMqJbx8:APA91bEV5xOEbPwF4vBJ7mHrOskCTpTRJx0cQrZ_uxa-QH8HLomXdSYixwRIvcA2AuBRh4B_2DDaY8hvj-TsFJG_Hb6LJt9sgbPrWkI-eo0Xtx2ZKttbIuja4NqajofmjgnubraIOb4_";
      return admin.messaging().sendToDevice(subscriber, payload)
    });
exports.sendNoti_cal_log = functions.https.onRequest((req: any, res: { status: (arg0: number) => { (): any; new(): any; send: { (arg0: { status: string; }): void; new(): any; }; }; }) => {
  const param1 = req.params.param1;    

  const payload = {
        notification: {
         title: 'Notification Title'+param1,
但当我从URL传递它时,它在通知中表示未定义:

我是这样通过的-

https://us-central1-fir-crud-5b378.cloudfunctions.net/sendNoti_cal_log?param1=Hello
在第二次更新后更新: 正如您将在中看到的:

始终使用send、redirect或end结束HTTP函数。 否则,您的函数可能会继续运行并被强制执行 由系统终止。另见

所以你需要做一些类似的事情:

return admin.messaging().sendToDevice(subscriber, payload)
then(messagingResponse => {
     res.status(200).send({status: "OK"});
})
对于您的问题:如何从参数传递标题并在函数中接收它

为了将变量传递给HTTP云函数,我在下面的第一次更新中解释了这一点

根据您的评论和更新进行更新: 您的云功能的URL如下所示:

https://us-central1-<project-id>.cloudfunctions.net/webhook
初步答复: 您的云功能是一个需要通过外部世界向云功能平台发出HTTP请求来触发的功能

因此,您需要从调用者/消费者端对其进行调度,例如,像这样的CRON服务将向相应的云函数URL发出HTTP请求,或者您的一台服务器,等等

如果您希望云功能平台在每天下午3点执行特定的云功能,您应该使用以下方法:

exports.webhook = functions.pubsub.schedule('0 15 * * *')
  .timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
  .onRun((context) => {
  // It sends notification to a user
  // return ...;
});
请注意,计划函数与HTTPS不同,但它实现的业务逻辑在您的情况下可能是相同的,即发送通知的代码


我已经对我的问题做了编辑::1,我可以做一个cron作业,我如何从url调用我的函数并传递变量,谢谢,我正在处理这个问题,一旦我排序,我会回来HII,我已经更新了问题并添加了编辑::2,请指导我哪里错了我已经更新了我的编辑2,它正在工作,但有一些问题,请参阅Edit::2最后我可以看到函数正在执行并返回响应。我更新到编辑::3,请指导我,我实际使用的是什么>TS或JS的功能,有点困惑。最后一件事,我要测试的是参数,一旦我实现了,就回来