Files
mkstack/public/sw.js
T
2025-10-20 13:25:54 -07:00

34 lines
766 B
JavaScript

// public/sw.js
self.addEventListener("push", (event) => {
if (!event.data) return;
try {
const data = event.data.json();
const title = data.title || "New Notification";
const options = {
body: data.body,
icon: data.icon || "/icon-192.png"
// badge: data.badge || "/badge-72x72.png",
// data: data.data || {},
};
event.waitUntil(
self.registration.showNotification(title, options)
);
} catch (err) {
console.error("Push event error:", err);
}
});
self.addEventListener("notificationclick", (event) => {
event.notification.close();
// If you send a `url` in payload.data, open it
const url = event.notification.data?.url;
if (url) {
event.waitUntil(clients.openWindow(url));
}
});