From b8ca43abbf004c14a024fbfce0428d833869c7f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Rynning-T=C3=B8nnesen?= Date: Fri, 14 Feb 2020 15:14:17 +0100 Subject: [PATCH] Push-notifications is fun --- api/subscriptions.js | 19 ++++++++++++++++++- src/components/RegisterPage.vue | 28 +++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/api/subscriptions.js b/api/subscriptions.js index ed6ea56..ed0f164 100644 --- a/api/subscriptions.js +++ b/api/subscriptions.js @@ -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 () => { diff --git a/src/components/RegisterPage.vue b/src/components/RegisterPage.vue index de91a07..b9c5f96 100644 --- a/src/components/RegisterPage.vue +++ b/src/components/RegisterPage.vue @@ -1,6 +1,15 @@