mirror of
https://github.com/jkingsman/Remote-Terminal-for-MeshCore.git
synced 2026-08-08 01:33:01 +02:00
Add web push
This commit is contained in:
+52
-5
@@ -1,12 +1,59 @@
|
||||
// Minimal service worker required for PWA installability.
|
||||
// No caching — this app is network-dependent. All fetches pass through.
|
||||
/* Service worker for PWA installability and Web Push notifications. */
|
||||
|
||||
self.addEventListener("install", function () {
|
||||
self.addEventListener("install", () => {
|
||||
self.skipWaiting();
|
||||
});
|
||||
|
||||
self.addEventListener("activate", function (event) {
|
||||
self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(self.clients.claim());
|
||||
});
|
||||
|
||||
self.addEventListener("fetch", function () {});
|
||||
// No-op fetch handler — required for PWA installability criteria.
|
||||
// We don't cache anything; the app always fetches from the network.
|
||||
self.addEventListener("fetch", () => {});
|
||||
|
||||
self.addEventListener("push", (event) => {
|
||||
let data = {};
|
||||
try {
|
||||
data = event.data ? event.data.json() : {};
|
||||
} catch {
|
||||
data = { title: "New message", body: event.data?.text() || "" };
|
||||
}
|
||||
|
||||
const title = data.title || "New message";
|
||||
const options = {
|
||||
body: data.body || "",
|
||||
icon: "./favicon-256x256.png",
|
||||
badge: "./favicon-96x96.png",
|
||||
tag: data.tag || "meshcore-push",
|
||||
data: { url_hash: data.url_hash || "" },
|
||||
};
|
||||
|
||||
event.waitUntil(self.registration.showNotification(title, options));
|
||||
});
|
||||
|
||||
self.addEventListener("notificationclick", (event) => {
|
||||
event.notification.close();
|
||||
const urlHash = event.notification.data?.url_hash || "";
|
||||
|
||||
event.waitUntil(
|
||||
clients
|
||||
.matchAll({ type: "window", includeUncontrolled: true })
|
||||
.then((windowClients) => {
|
||||
// Focus an existing tab if one is open
|
||||
for (const client of windowClients) {
|
||||
if (client.url.includes(self.location.origin)) {
|
||||
client.focus();
|
||||
if (urlHash) {
|
||||
client.navigate(self.location.origin + "/" + urlHash);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Otherwise open a new tab
|
||||
return clients.openWindow(
|
||||
self.location.origin + "/" + (urlHash || "")
|
||||
);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user