如何使用带有实时数据库/分析触发器的Firebase Cloud功能的调度程序? [英] How to use scheduler for Firebase Cloud Functions with Realtime Database/Analytics triggers?

本文介绍了如何使用带有实时数据库/分析触发器的Firebase Cloud功能的调度程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase Cloud Function,以发送触发的推送通知. 现在,只要用户在我的应用程序中触发"IAP"事件,我的函数就会发送一次推送.

I'm working on a Firebase Cloud Function, to send triggered push notifications. Right now my function sends a push as soon as an user triggers the "IAP" event in my app.

'use strict';

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

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

exports.sendIAPAnalytics = functions.analytics.event('IAP').onLog((event) => {

    const user = event.user;
    const uid = user.userId; // The user ID set via the setUserId API.   

    sendPushToUser();

    return true;
});


function sendPushToUser(uid) {
  // Fetching all the user's device tokens.

     var ref = admin.database().ref(`/users/${uid}/tokens`);

     return ref.once("value", function(snapshot){
         const payload = {
              notification: {
                  title: 'Hello',
                  body: 'Open the push'
              }
         };

        console.log("sendPushToUser ready");

         admin.messaging().sendToDevice(snapshot.val(), payload)

    }, function (errorObject) {
        console.log("The read failed: " + errorObject.code);
    });
}

此功能有效,发送和接收推送.

This functions works, push are sent and received.

我阅读了一些有关安排Firebase Cloud Functions的新闻:

I read some news about scheduling for Firebase Cloud Functions:

  • https://medium.com/@pascalluther/scheduling-firebase-cloud-functions-with-cloud-scheduler-b5ec22ace683
  • https://firebase.googleblog.com/2019/04/schedule-cloud-functions-firebase-cron.html

我了解,它仅适用于HTTP触发器或PUB/SUB触发器. 因此,到目前为止,通过写入实时数据库或触发分析事件来延迟触发功能始终是不可能的.

I understood, it's only for HTTP triggers ou PUB/SUB triggers. So for now it's always impossible to trigger functions with delays, by writing in realtime database or when analytics events are triggered.

我是对的吗?还是有把戏?

Am I right? or is there a trick?

我对此一无所知.

官方文档 https://firebase.google.com/docs/functions/schedule-functions

我的语法是错误的,但是我需要这样的东西:

My syntax is wrong but I need something like this:

function sendPushToUser(uid) {

     var ref = admin.database().ref(`/users/${uid}/tokens`);

     return ref.once("value", function(snapshot){
         const payload = {
              notification: {
                  title: 'Hello',
                  body: 'Open the push'
              }
         };


    functions.pubsub.schedule('at now + 10 mins').onRun((context) => {
       admin.messaging().sendToDevice(snapshot.val(), payload)

    })

    }, function (errorObject) {
        console.log("The read failed: " + errorObject.code);
    });
}

推荐答案

没有内置的方法可以延迟重新触发Cloud Functions.如果需要此类功能,则必须自己构建,例如,安排要定期运行的功能,然后查看需要触发哪些任务.在这里查看我的答案:延迟Google Cloud Function

There is no built-in way to retrigger Cloud Functions with a delay. If you want such functionality you will have to build that yourself, for example by scheduling a function to run periodically and then see what tasks need to be triggered. See my answer here: Delay Google Cloud Function

如Doug所评论,您可以使用 Cloud Tasks 安排各个调用.您将动态创建任务,然后让它调用HTTP函数.

As Doug commented, you can use Cloud Tasks to schedule individual invocations. You'd dynamically create the task, and then have it call a HTTP function.

这篇关于如何使用带有实时数据库/分析触发器的Firebase Cloud功能的调度程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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