在我正在构建的应用程序中,当创建用户时,我将时间戳保存在
users/{userid}/timestamp
我试图实现的是在用户创建后24、48、72小时发送用户FCM通知。
exports.hourlyTick = functions.https.onRequest((req, res) => {
let now = Date.now();
let onedaycutoff = now - 24 * 60 * 60 * 1000;
let twodaycutoff = now - 48 * 60 * 60 * 1000;
let threedaycutoff = now - 72 * 60 * 60 * 1000;
let onedaycutoffpromise = admin.database().ref(`Users`).orderByChild('timestamp').endAt(onedaycutoff).once('value');
let twodaycutoffpromise = admin.database().ref(`Users`).orderByChild('timestamp').endAt(twodaycutoff).once('value');
let threedaycutoffpromise = admin.database().ref(`Users`).orderByChild('timestamp').endAt(threedaycutoff).once('value');
return Promise.all([onedaycutoffpromise, twodaycutoffpromise, threedaycutoffpromise]).then(r => {
const onedaycutofftokens = r[0];
const twodaycutofftokens = r[1];
const threedaycutofftokens = r[2];
const onedayTokens = [];
const twodayTokens = [];
const threedayTokens = [];
onedaycutofftokens.forEach(p => {
if (p.child("registrationToken").val()) {
onedayTokens.push(p.child("registrationToken").val());
}
});
twodaycutofftokens.forEach(p => {
if (p.child("registrationToken").val()) {
twodayTokens.push(p.child("registrationToken").val());
}
});
threedaycutofftokens.forEach(p => {
if (p.child("registrationToken").val()) {
threedayTokens.push(p.child("registrationToken").val());
}
});
console.log(onedayTokens);
console.log(twodayTokens);
console.log(threedayTokens);
return Promise.all([SendNotification(4, onedayTokens), SendNotification(5, twodayTokens), SendNotification(6, threedayTokens)]).then((result) => {
res.send('Notifications sent');
return null;
});
})
});
日志为所有人打印相同的数组。