53 lines
2.1 KiB
JavaScript
53 lines
2.1 KiB
JavaScript
importScripts("assets/firebase/firebase-app.js");
|
|
importScripts("assets/firebase/firebase-messaging.js");
|
|
importScripts("dist/gest-lib/notifiche/js/main.js");
|
|
|
|
self.addEventListener("notificationclick", e => {
|
|
e.notification.close();
|
|
|
|
let href = self.location.href.split("/");
|
|
href.pop();
|
|
href = href.join("/");
|
|
|
|
// Get all the Window clients
|
|
e.waitUntil(
|
|
self.clients
|
|
.matchAll({type: "window", includeUncontrolled: true})
|
|
.then(windowClients => {
|
|
// If a Window tab matching the targeted URL already exists, focus that;
|
|
const hadWindowToFocus = windowClients.some(windowClient => windowClient.url === href + "/" ? (windowClient.focus(), true) : false);
|
|
// Otherwise, open a new tab to the applicable URL and focus it.
|
|
if (!hadWindowToFocus) self.clients.openWindow(href + "/").then(windowClient => windowClient ? windowClient.focus() : null);
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener("push", async function (e) {
|
|
const payload = e.data.json();
|
|
|
|
if (payload.data && payload.data.title) {
|
|
e.waitUntil(
|
|
self.clients
|
|
.matchAll({type: "window", includeUncontrolled: true})
|
|
.then(windowClients => {
|
|
let visibleClients = windowClients.some(client => client.visibilityState === "visible" &&
|
|
// Ignore chrome-extension clients as that matches the background pages of extensions, which
|
|
// are always considered visible for some reason.
|
|
!client.url.startsWith("chrome-extension://"));
|
|
|
|
if (!visibleClients) {
|
|
e.waitUntil(
|
|
self.registration.showNotification(
|
|
payload.data.title,
|
|
payload.data
|
|
)
|
|
);
|
|
} else {
|
|
windowClients.forEach(function (client) {
|
|
client.postMessage(payload.data);
|
|
})
|
|
}
|
|
})
|
|
);
|
|
}
|
|
}); |