我正在处理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函数调度的新闻:
我理解,它只适用于HTTP触发器ou 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);
});
}发布于 2019-05-02 16:12:36
没有内置的方式来重新触发云函数的延迟。如果您想要这样的功能,您必须自己构建它,例如,安排一个函数定期运行,然后查看需要触发哪些任务。见我在这里的答案:延迟Google云功能
正如道格所评论的,您可以使用云任务来安排单独的调用。您可以动态地创建任务,然后让它调用一个HTTP函数。
https://stackoverflow.com/questions/55956317
复制相似问题