2024-07-20 00:41:17 +03:00
|
|
|
// Notifications Worker
|
|
|
|
|
|
|
|
|
|
self.addEventListener("push", function (event) {
|
2024-07-20 22:19:34 +03:00
|
|
|
// @ts-expect-error we know this exists
|
2024-07-20 00:41:17 +03:00
|
|
|
if (event.data) {
|
2024-07-20 22:19:34 +03:00
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
|
const data = event.data.json();
|
|
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
|
self.registration.showNotification(data.title, {
|
|
|
|
|
body: data.message,
|
|
|
|
|
icon: data.image,
|
|
|
|
|
image: data.image,
|
|
|
|
|
tag: data.id,
|
|
|
|
|
data: { id: data.id, link: data.direct_url },
|
|
|
|
|
actions: [
|
|
|
|
|
{
|
|
|
|
|
action: `view-${data.id}`,
|
|
|
|
|
title: "View",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
2024-07-20 00:41:17 +03:00
|
|
|
} else {
|
2024-07-20 22:19:34 +03:00
|
|
|
// pass
|
|
|
|
|
// This push event has no data
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
self.addEventListener("notificationclick", (event) => {
|
|
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
|
if (event.notification) {
|
|
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
|
event.notification.close();
|
|
|
|
|
|
|
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
|
if (event.notification.data) {
|
|
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
|
const url = event.notification.data.link;
|
|
|
|
|
|
|
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
|
clients.matchAll({ type: "window" }).then((windowClients) => {
|
|
|
|
|
// Check if there is already a window/tab open with the target URL
|
|
|
|
|
for (let i = 0; i < windowClients.length; i++) {
|
|
|
|
|
const client = windowClients[i];
|
|
|
|
|
// If so, just focus it.
|
|
|
|
|
if (client.url === url && "focus" in client) {
|
|
|
|
|
return client.focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// If not, then open the target URL in a new window/tab.
|
|
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
|
if (clients.openWindow) {
|
|
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
|
return clients.openWindow(url);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-07-20 00:41:17 +03:00
|
|
|
}
|
|
|
|
|
});
|