我正在使用cron安排推送通知。我正在从用户那里收集推送通知数据和预定时间,以便在预定时间发送推送通知。
下面是我如何创建一个cron作业来调度推送通知:
const CronJob = require("cron").CronJob;
...
...
const schedulePushNotification = async (req, res) => {
const job = new CronJob(
new Date(req.body.scheduledTime),
async () => {
// my code to send push notification
// code to store push notification data
// Here I write a code to store push notification data to
// show on the dashboard like a list of notification
},
null,
true,
"America/Los_Angeles"
);
job.start();
return res.status(200).json({
success: true,
message: "Notification has been scheduled successfully !",
data: null,
});
};现在的要求是,如果用户想要取消预定的推送通知,他们可以通过单击cancel来完成。
那么,我该如何销毁正在运行的cron呢?
发布于 2021-02-27 16:34:14
您可以通过在函数中添加一个if语句来实现这一点
您可以检查用户是否激活了通知,但首先必须将用户是否激活通知存储在数据库中,然后再发送通知
const CronJob = require("cron").CronJob;
...
...
const schedulePushNotification = async (req, res) => {
const job = new CronJob(
new Date(req.body.scheduledTime),
async () => {
let userNotificationDetails = getUserNotificationDetails();
if(userNotificationDetails.isSendNotification){
job.start();
} else {
// don't send notification
}
// my code to send push notification
// code to store push notification data
// Here I write a code to store push notification data to
// show on the dashboard like a list of notification
},
null,
true,
"America/Los_Angeles"
);
return res.status(200).json({
success: true,
message: "Notification has been scheduled successfully !",
data: null,
});
};。
cron编辑(另一种解决方案:在启动之前检查是否isNotification ):
const CronJob = require("cron").CronJob;
...
...
const schedulePushNotification = async (req, res) => {
const job = new CronJob(
new Date(req.body.scheduledTime),
async () => {
// my code to send push notification
// code to store push notification data
// Here I write a code to store push notification data to
// show on the dashboard like a list of notification
},
null,
true,
"America/Los_Angeles"
);
let userNotificationDetails = getUserNotificationDetails();
if(userNotificationDetails.isSendNotification){
job.start();
} else {
// don't send notification
}
return res.status(200).json({
success: true,
message: "Notification has been scheduled successfully !",
data: null,
});
};发布于 2021-02-27 17:54:23
您可以通过在函数内添加一条if语句来完成此操作
您可以检查用户是否激活了通知,但首先必须在数据库中存储用户是否激活了通知,然后发送通知
https://stackoverflow.com/questions/66396937
复制相似问题