This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
/* global self */
|
||||
self.addEventListener("push", (event) => {
|
||||
let message = {
|
||||
title: "HotFridge",
|
||||
body: "You have food expiring soon.",
|
||||
url: "/app/list",
|
||||
};
|
||||
try {
|
||||
message = { ...message, ...JSON.parse(event.data?.text() ?? "{}") };
|
||||
} catch {
|
||||
// Keep the generic notification when a payload cannot be decoded.
|
||||
}
|
||||
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(message.title, {
|
||||
body: message.body,
|
||||
tag: "near-expiry",
|
||||
renotify: true,
|
||||
data: { url: message.url },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("notificationclick", (event) => {
|
||||
event.notification.close();
|
||||
const url = event.notification.data?.url ?? "/app/list";
|
||||
event.waitUntil(
|
||||
self.clients.matchAll({ type: "window", includeUncontrolled: true }).then((clients) => {
|
||||
const existing = clients.find((client) => new URL(client.url).pathname === url);
|
||||
return existing ? existing.focus() : self.clients.openWindow(url);
|
||||
}),
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user