improve test push notifications;

renames; more alerts (merge)
This commit is contained in:
Dobromir Popov
2024-05-23 02:51:15 +03:00
parent 8b3f13d2ee
commit cfc78abff9
10 changed files with 50 additions and 25 deletions

View File

@ -56,15 +56,17 @@ const Notification = async (req, res) => {
if (index !== -1) {
subscriptions[index] = subscription; // Update existing subscription
console.log('Subscription for publisher', id, 'updated.')
} else {
subscriptions.push(subscription); // Add new subscription
console.log('Subscription for publisher', id, 'saved.')
}
await prisma.publisher.update({
where: { id },
data: { pushSubscription: subscriptions }
});
console.log('Subscription for publisher', id, 'updated:', subscription)
console.log('Subscription update successful', subscription.keys.auth, ". Total subscriptions:", subscriptions.length)
res.send({ subs: subscriptions.length })
res.statusCode = 200
res.end()
@ -111,6 +113,7 @@ const Notification = async (req, res) => {
return
}
else if (id) {
console.log('Sending push notification to publisher ', id)
await sendPush(id, title, message.actions)
res.statusCode = 200
res.end()
@ -148,22 +151,25 @@ export const sendPush = async (id, title, message, actions) => {
const publisher = await prisma.publisher.findUnique({
where: { id }
})
if (!publisher.pushSubscription) {
console.log('No push subscription found for publisher', id)
return
}
await webPush
.sendNotification(
publisher.pushSubscription,
JSON.stringify({ title, message, actions })
)
.then(response => {
console.log('Push notification sent to publisher', id)
})
.catch(err => {
console.error('Error sending push notification to publisher', id, ':', err)
})
if (Array.isArray(publisher.pushSubscription) && publisher.pushSubscription.length) {
for (const subscription of publisher.pushSubscription) {
await webPush
.sendNotification(
subscription,
JSON.stringify({ title, message, actions })
)
.then(response => {
console.log('Push notification sent to publisher', id)
})
.catch(err => {
console.error('Error sending push notification to publisher', id, ':', err)
})
}
} else {
console.log('No valid subscriptions found for publisher', id)
}
}
//export breoadcastNotification for use in other files
export const broadcastPush = async (title, message, actions) => {