当存储在Firestore数据库中的日期是今天的日期时,如何触发功能? [英] How to trigger function when date stored in firestore database is todays date?

查看:132
本文介绍了当存储在Firestore数据库中的日期是今天的日期时,如何触发功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,当今天的日期与数据库中存储的日期匹配时,我需要在其中发送推送通知,以便发送推送通知. 如何实现这一目标?

I am creating an app where I need to send push notification when today's date matches with the date stored in database in order to send push notification. How to achieve this?

推荐答案

在不知道您的数据模型的情况下,很难给出准确的答案,但是为了简化起见,让我们想象一下,您在每个文档中存储了一个名为notifDate的字段.格式为DDMMYYY,并且这些文档存储在名为notificationTriggers的集合中.

Without knowing your data model it is difficult to give a precise answer, but let's imagine, to simplify, that you store in each document a field named notifDate with format DDMMYYY and that those documents are store in a Collection named notificationTriggers.

您可以编写如下的HTTPS Cloud Function:

You could write an HTTPS Cloud Function as follows:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

const cors = require('cors')({ origin: true });
const moment = require('moment');

admin.initializeApp();

exports.sendDailyNotifications = functions.https.onRequest((request, response) => {

    cors(request, response, () => {

       const now = moment();
       const dateFormatted = now.format('DDMMYYYY');

       admin.firestore()
       .collection("notificationTriggers").where("notifDate", "==", dateFormatted)
       .get()
       .then(function(querySnapshot) {

           const promises = []; 

           querySnapshot.forEach(doc => {

               const tokenId = doc.data().tokenId;  //Assumption: the tokenId is in the doc
               const notificationContent = {
                 notification: {
                    title: "...",
                    body: "...",  //maybe use some data from the doc, e.g  doc.data().notificationContent
                    icon: "default",
                    sound : "default"
                 }
              };

              promises
              .push(admin.messaging().sendToDevice(tokenId, notificationContent));      

          });
          return Promise.all(promises);
       })
       .then(results => {
            response.send(data)
       })
       .catch(error => {
          console.log(error)
          response.status(500).send(error)
       });

    });

});

然后,您每天都会通过在线CRON作业服务(例如 https://cron- job.org/en/.

You would then call this Cloud Function every day with an online CRON job service like https://cron-job.org/en/.

有关如何在Cloud Functions中发送通知的更多示例,请查看这些SO答案 node.js firebase部署错误

For more examples on how to send notifications in Cloud Functions, have a look at those SO answers Sending push notification using cloud function when a new node is added in firebase realtime database?, node.js firebase deploy error or Firebase: Cloud Firestore trigger not working for FCM.

如果您不熟悉在Cloud Functions中使用Promises,我建议您观看Firebase视频系列中有关"JavaScript Promises"的3个视频: https://firebase.google.com/docs/functions/video-series/

If you are not familiar with the use of Promises in Cloud Functions I would suggest you watch the 3 videos about "JavaScript Promises" from the Firebase video series: https://firebase.google.com/docs/functions/video-series/

由于要并行执行几个异步任务(sendToDevice()方法),因此您会在上面的代码中注意到Promise.all()的使用.这在上面提到的第三个视频中有详细介绍.

You will note the use of Promise.all() in the above code, since you are executing several asynchronous tasks (sendToDevice() method) in parallel. This is detailed in the third video mentioned above.

这篇关于当存储在Firestore数据库中的日期是今天的日期时,如何触发功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆