Files
PVM/public_html/firebase-messaging-sw.js
ClaudioR c9d91630a4 [Notifiche]
- Firebase, fix importScripts
2024-02-16 10:30:23 +01:00

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);
})
}
})
);
}
});