feat: pwa notifications
This commit is contained in:
@@ -16,6 +16,45 @@ self.addEventListener("activate", (event) => {
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("push", (event) => {
|
||||
let payload = {};
|
||||
try {
|
||||
payload = event.data ? event.data.json() : {};
|
||||
} catch (e) {
|
||||
payload = { title: "Osiris", body: event.data ? event.data.text() : "" };
|
||||
}
|
||||
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(payload.title || "Osiris", {
|
||||
body: payload.body || "",
|
||||
icon: "/static/icons/icon-192.png",
|
||||
badge: "/static/icons/icon-192.png",
|
||||
// Same tag replaces the previous notification instead of stacking.
|
||||
tag: payload.tag || "osiris",
|
||||
renotify: true,
|
||||
requireInteraction: true, // A dose reminder should wait, not fade away.
|
||||
data: { url: payload.url || "/admin/today/" },
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("notificationclick", (event) => {
|
||||
event.notification.close();
|
||||
const url = (event.notification.data && event.notification.data.url) || "/admin/today/";
|
||||
|
||||
// Focus the app if it is already open rather than opening a second window.
|
||||
event.waitUntil(
|
||||
self.clients
|
||||
.matchAll({ type: "window", includeUncontrolled: true })
|
||||
.then((windows) => {
|
||||
for (const client of windows) {
|
||||
if (client.url.includes(url) && "focus" in client) return client.focus();
|
||||
}
|
||||
return self.clients.openWindow(url);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("fetch", (event) => {
|
||||
const { request } = event;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user