37 lines
946 B
JavaScript

self.addEventListener("push", (event) => {
let data = {};
try {
data = event.data ? event.data.json() : {};
} catch {
data = {};
}
const title = data.title || "Aarthalabs";
const options = {
body: data.body || "You have a new Aarthalabs notification.",
icon: "/favicon.ico",
badge: "/favicon.ico",
data: {
url: data.url || "/notifications",
},
};
event.waitUntil(self.registration.showNotification(title, options));
});
self.addEventListener("notificationclick", (event) => {
event.notification.close();
const url = event.notification.data?.url || "/notifications";
event.waitUntil(
self.clients.matchAll({ type: "window", includeUncontrolled: true }).then((clients) => {
for (const client of clients) {
if ("focus" in client && client.url.includes(url)) {
return client.focus();
}
}
return self.clients.openWindow(url);
}),
);
});