frontend/public/sw/push.js
Nathan Lamy b9773de805
All checks were successful
Deploy to Netlify / Deploy to Netlify (push) Successful in 2m10s
feat: add open action
2025-08-21 11:36:37 +02:00

36 lines
910 B
JavaScript

self.addEventListener("push", onPush);
async function onPush(event) {
if (event.data) {
const data = event.data.json();
const { title, ...rest } = data;
// Send the push data to the application
const clients = await self.clients.matchAll();
clients.forEach((client) => client.postMessage(data));
await event.waitUntil(
self.registration.showNotification(title, {
...rest,
})
);
}
}
const BASE_URL = "https://khollise.fr";
self.addEventListener("notificationclick", function (event) {
const clickedNotification = event.notification;
clickedNotification.close();
if (event.action == "open" && event.data.id) {
const promiseChain = clients.openWindow(
BASE_URL + "/colles/" + event.data.id
);
event.waitUntil(promiseChain);
return;
}
const promiseChain = clients.openWindow(BASE_URL);
event.waitUntil(promiseChain);
});