Aded not-show notif when window visible

This commit is contained in:
retoor 2025-07-04 05:40:19 +02:00
parent a7d7cb5627
commit 85edc95652

View File

@ -1,3 +1,11 @@
function isClientOpen(url) {
return self.clients.matchAll().then((clients) => {
return clients.some((client) => {
return client.url === url && "focus" in client;
});
});
}
self.addEventListener("install", (event) => {
console.log("Service worker installing...");
event.waitUntil(
@ -20,6 +28,11 @@ self.addEventListener("push", (event) => {
const data = event.data?.json() ?? {};
console.log("Received a push message", event, data);
if(isClientOpen(data.url || data.link || `/web.html`)){
console.log("Client already open, not showing notification.");
return;
}
const title = data.title || "Something Has Happened";
const message =
data.message || "Here's something you might want to check out.";
@ -41,6 +54,8 @@ self.addEventListener("push", (event) => {
event.waitUntil(reg);
});
self.addEventListener("notificationclick", (event) => {
console.log("Notification click Received.", event);
event.notification.close();