Skip to content

Commit

Permalink
adding SW to ICN
Browse files Browse the repository at this point in the history
  • Loading branch information
captainbrosset committed Nov 4, 2024
1 parent 7660c92 commit c9ce1ba
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion incoming-call-notifications/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
};
}

navigator.serviceWorker.register("service-worker.js", { scope: '/repro_pages/notifications/' })
navigator.serviceWorker.register("sw.js", { scope: './' })
.then((result) => {
registration = result;
log("register() succeeded");
Expand Down
86 changes: 86 additions & 0 deletions incoming-call-notifications/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"use strict";

skipWaiting();

onactivate = () => {
return clients.claim();
};

onmessage = (messageEvent) => {
registration
.showNotification(`ServiceWorkerGlobalScope Title ${messageEvent.data}`, {
body: `ServiceWorkerGlobalScope Body ${messageEvent.data}`,
icon: "../resources/happy.jpg",
})
.then(() => {
messageEvent.source.postMessage(
"ServiceWorkerGlobalScope showNotification() succeeded."
);
})
.catch((error) => {
messageEvent.source.postMessage(
`ServiceWorkerGlobalScope showNotification() failed: ${error}.`
);
});
};

onnotificationclick = (event) => {
const notification = event.notification;

clients.matchAll().then((resultList) => {
resultList.forEach((client) => {
client.postMessage(
`ServiceWorkerGlobalScope 'click' event for: ${
notification.title
} , timestamp: ${new Date(
notification.timestamp
)}, requireInteraction: ${notification.requireInteraction}, silent: ${
notification.silent
}`
);
notification.close();
});
});

if (event.action === "open_window") {
event.waitUntil(
new Promise((resolve) => {
setTimeout(() => {
clients.openWindow("on-click.html");
resolve();
}, 0);
})
);
} else {
// Focus existing client.
event.waitUntil(
clients.matchAll().then((resultList) => {
if (resultList.length > 0) {
return resultList[0].focus();
}
})
);
}
};

onnotificationclose = (event) => {
clients.matchAll().then((resultList) => {
resultList.forEach((client) => {
const notification = event.notification;
client.postMessage(
`ServiceWorkerGlobalScope 'close' event for: ${
notification.title
} , timestamp: ${new Date(
notification.timestamp
)}, requireInteraction: ${notification.requireInteraction}, silent: ${
notification.silent
}`
);
});
});
};

onfetch = (fetchEvent) => {
console.log(fetchEvent.request.url);
fetchEvent.respondWith(fetch(fetchEvent.request));
};

0 comments on commit c9ce1ba

Please sign in to comment.