Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
subscriptions.forEach((subscription) => {
webPush.sendNotification(subscription, payload, options)
.then(() => {})
.catch((err) => {
console.log(`Error when trying to deliver message for ${subscription.endpoint}`, err);
// This is probably an old subscription, remove it
deleteSubscription(null, { endpoint: subscription.endpoint }, { user });
});
});
};
webpush.setVapidDetails(
SERVER_SUBJECT,
pushKeys.pushVapidKeys.publicKey,
pushKeys.pushVapidKeys.privateKey,
);
// This is the same output of calling JSON.stringify on a PushSubscription
const pushSubscription = {
endpoint: endpoint,
keys: {
auth: keys.auth,
p256dh: keys.p256dh,
},
};
webpush.sendNotification(pushSubscription, payload)
.then((webPushResult) => {
return resolve(webPushResult);
}).catch((webPushError) => {
console.log(`push failed to ${endpoint}\n ${webPushError}`);
return reject(webPushError);
});
});
}
setTimeout(function() {
webPush.sendNotification(subscription, payload, options)
.then(function() {
res.sendStatus(201);
})
.catch(function(error) {
console.log(error);
res.sendStatus(500);
});
}, req.body.delay * 1000);
});
.then(sub => {
const payload = JSON.stringify({
notification: {
'title': 'Push notifications with Service Workers'
}
})
const Sub ={
endpoint: sub.endpoint,
keys: sub.keys
}
webPush.sendNotification(Sub,payload)
.then(result => console.log(result))
.catch(err => console.log(err))
})
function processNextNotification(user: IUser, cb: CB): void {
if (!user.state.weeklyContactList.length) {
console.log(`No more contacts to process this week, go ${user.name}!`);
cb(undefined);
return;
}
for (const bucket of user.state.dailyBuckets) {
if (bucket.timestamp <= Date.now() && Date.now() < bucket.timestamp + BUCKET_DURATION) {
if (bucket.available) {
const nextContact = user.state.weeklyContactList[0];
if (nextContact && user.state.subscription) {
console.log(`Time for ${user.name} to reach out to ${nextContact.name}`);
sendNotification(user.state.subscription, JSON.stringify(nextContact))
.then(() => cb(undefined))
.catch(cb);
return;
}
}
}
}
cb(undefined);
}
snapshot.forEach(doc => {
webpush.sendNotification(doc.data().subscription);
});
res.status(200).send({ success: true, hour: currentHour });
export const sendPush = (subscription, title, body, link) => {
if (!subscription) return
webpush.sendNotification(subscription, JSON.stringify({ title, body, link }))
}
updateSubscriptionSettings(user, settings) {
for (let uuid in subscriptions) {
if (subscriptions[uuid].user === user) {
subscriptions[uuid].settings = settings
webPush.sendNotification(subscriptions[uuid], JSON.stringify({settings, type: 'updateSettings'}), { TTL: 60 * 60 })
}
}
}
}