Push-notifications is fun
This commit is contained in:
@@ -27,7 +27,11 @@ webpush.setVapidDetails(
|
||||
);
|
||||
|
||||
const sendNotification = (subscription, dataToSend = "") => {
|
||||
webpush.sendNotification(subscription, dataToSend);
|
||||
try {
|
||||
webpush.sendNotification(subscription, dataToSend);
|
||||
} catch (e) {
|
||||
console.log("error", e);
|
||||
}
|
||||
};
|
||||
|
||||
router.use((req, res, next) => {
|
||||
@@ -55,6 +59,19 @@ const saveToDatabase = async subscription => {
|
||||
}
|
||||
};
|
||||
|
||||
router.route("/send-notification").post(async (req, res) => {
|
||||
if (!req.isAuthenticated()) {
|
||||
res.send(false);
|
||||
return;
|
||||
}
|
||||
const message = req.body.message;
|
||||
let subs = await Subscription.find();
|
||||
for (let i = 0; i < subs.length; i++) {
|
||||
let subscription = subs[i]; //get subscription from your databse here.
|
||||
sendNotification(subscription, message);
|
||||
}
|
||||
});
|
||||
|
||||
schedule.scheduleJob(
|
||||
`0 50 ${lotteryConfig.hours - 1} * * ${lotteryConfig.date}`,
|
||||
async () => {
|
||||
|
||||
Reference in New Issue
Block a user