如何使用实时 Database/Analytics 触发器的 Firebase Cloud Functions 调度程序?
How to use scheduler for Firebase Cloud Functions with Realtime Database/Analytics triggers?
我正在研究 Firebase 云功能,以发送触发的推送通知。
现在,只要用户在我的应用程序中触发 "IAP" 事件,我的功能就会发送推送。
'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);
});
}
此功能有效,发送和接收推送。
我阅读了一些有关 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 触发器。
所以现在总是不可能通过在实时数据库中写入或在触发分析事件时延迟触发函数。
我说得对吗?或者有什么技巧?
我对此一无所知。
编辑:官方文档
https://firebase.google.com/docs/functions/schedule-functions
我的语法有误,但我需要这样的东西:
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。如果你想要这样的功能,你将不得不自己构建它,例如通过定期安排一个函数到 运行 然后查看需要触发哪些任务。在这里查看我的回答:Delay Google Cloud Function
正如 Doug 评论的那样,您可以使用 Cloud Tasks 来安排单独的调用。您将动态创建任务,然后让它调用 HTTP 函数。
我正在研究 Firebase 云功能,以发送触发的推送通知。 现在,只要用户在我的应用程序中触发 "IAP" 事件,我的功能就会发送推送。
'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);
});
}
此功能有效,发送和接收推送。
我阅读了一些有关 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 触发器。 所以现在总是不可能通过在实时数据库中写入或在触发分析事件时延迟触发函数。
我说得对吗?或者有什么技巧?
我对此一无所知。
编辑:官方文档 https://firebase.google.com/docs/functions/schedule-functions
我的语法有误,但我需要这样的东西:
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。如果你想要这样的功能,你将不得不自己构建它,例如通过定期安排一个函数到 运行 然后查看需要触发哪些任务。在这里查看我的回答:Delay Google Cloud Function
正如 Doug 评论的那样,您可以使用 Cloud Tasks 来安排单独的调用。您将动态创建任务,然后让它调用 HTTP 函数。