Javascript 白天每15分钟发送一次通知
我希望在向Firebase添加数据后1小时内发送通知,并希望在添加数据后1天取消通知。因为我不懂JavaScript,而且我还不熟悉软件世界,所以我不能完全理解它的算法,我写了一些类似的东西。addedNewCard方法有效,但我无法调整时间Javascript 白天每15分钟发送一次通知,javascript,firebase,google-cloud-functions,firebase-cloud-messaging,Javascript,Firebase,Google Cloud Functions,Firebase Cloud Messaging,我希望在向Firebase添加数据后1小时内发送通知,并希望在添加数据后1天取消通知。因为我不懂JavaScript,而且我还不熟悉软件世界,所以我不能完全理解它的算法,我写了一些类似的东西。addedNewCard方法有效,但我无法调整时间 const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config(
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().functions);
var newData;
exports.addedNewCard =
functions.firestore.document('Users/{userID}/Cards/{cardID}').onCreate(async
(snapshot, context) => {
const cardID = context.params.cardID;
if (snapshot.empty) {
console.log('No Devices');
return;
}
newData = snapshot.data();
const cardAddedDate = newData.cardAddedDate;
const deviceIdTokens = await admin
.firestore()
.collection('DeviceToken')
.get();
var tokens = [];
for (var token of deviceIdTokens.docs) {
tokens.push(token.data().deviceToken);
}
var payload = {
notification: {
title: 'Tekrar vakti',
body: 'Tekrar etmen gereken kelimeler var!!!',
sound: 'default',
},
data: {
click_action: 'FLUTTER_NOTIFICATIoN_CLICK',
sendCardID: cardID,
}
};
const options = {
priority: "high",
};
try {
await admin.messaging().sendToDevice(tokens, payload, options);
console.log('Notification sent successfully');
} catch (err) {
console.log(err);
}
})
exports.timeSettings = functions.pubsub.schedule('every 1 mins').onRun(async
(context) => {
console.log(context.timestamp);
let now = new Date();
const finishWorking = now.setDate(now.getDate + 1);
const finishWorkingStamp = admin.firestore.Timestamp.fromDate(finishWorking);
db.collection('Users/{userID}/Cards').where('cardAddedDate', '<',
finishWorkingStamp).get().then((snap) => {
if (snap.exist) {
snap.forEach(() => {
return addedNewCard();
}).catch((e) => {
return console.log(e);
});
}
});
})
const functions=require('firebase-functions');
const admin=require('firebase-admin');
admin.initializeApp(functions.config().functions);
var新数据;
exports.addedNewCard=
functions.firestore.document('Users/{userID}/Cards/{cardd}').onCreate(异步
(快照、上下文)=>{
const cardd=context.params.cardd;
if(snapshot.empty){
console.log(“无设备”);
返回;
}
newData=snapshot.data();
const cardaddate=newData.cardaddate;
const deviceIdTokens=等待管理员
.firestore()
.集合(“DeviceToken”)
.get();
var标记=[];
for(deviceIdTokens.docs的var标记){
tokens.push(token.data().deviceToken);
}
var有效载荷={
通知:{
标题:“Tekrar vakti”,
正文:“Tekrar etmen gereken kelimeler var!!!”,
声音:'默认',
},
数据:{
单击操作:“颤振通知”单击,
森卡迪德:卡迪德,
}
};
常量选项={
优先级:“高”,
};
试一试{
等待admin.messaging().sendToDevice(令牌、负载、选项);
log(“通知已成功发送”);
}捕捉(错误){
控制台日志(err);
}
})
exports.timeSettings=functions.pubsub.schedule(“每1分钟”).onRun(异步
(上下文)=>{
log(context.timestamp);
让我们现在=新日期();
const finishWorking=now.setDate(now.getDate+1);
const finishWorkingStamp=admin.firestore.Timestamp.fromDate(finishWorking);
db.collection('Users/{userID}/Cards')。其中('cardAddedDate','我不会在客户端执行计划通知,而是通过服务器端配置和发送计划。尝试为客户端创建线程以处理通知。您必须创建firebase云函数,在该函数中,您需要升级firebase帐户订阅并使用发布订阅
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.scheduledFunctionCrontab = functions.pubsub.schedule('*/15 * * * *').timeZone('Asia/Kolkata').onRun(async (context) => {
console.log('This will be run every 15 minutes');
return null});
感谢您的评论,我建议您使用。使用云任务,您可以在将来延迟执行
当用户发送数据时,您可以提前计划24小时通知(每15分钟通知一次,持续1天->创建96个任务,下一个任务的延迟比上一个任务多15分钟)
你有代码示例。看看这部分,改变延迟
if (inSeconds) {
// The time when the task is scheduled to be attempted.
task.scheduleTime = {
seconds: inSeconds + Date.now() / 1000,
};
}
你所说的“白天”是什么意思?它与夜晚相反吗?如果是的话,你希望调度程序的时间框架是什么?例如,用户添加了一个数据,并且希望在完成1天时发送24小时的通知,希望取消发送通知