diff --git a/frigate/comms/webpush.py b/frigate/comms/webpush.py index 084f91059..f26d4e6a9 100644 --- a/frigate/comms/webpush.py +++ b/frigate/comms/webpush.py @@ -165,6 +165,7 @@ class WebPushClient(Communicator): # type: ignore[misc] "direct_url": direct_url, "image": image, "id": reviewId, + "type": "alert", } ), ) diff --git a/web/public/notifications-worker.js b/web/public/notifications-worker.js index 5e0e2a0f8..ab8a6ae44 100644 --- a/web/public/notifications-worker.js +++ b/web/public/notifications-worker.js @@ -5,6 +5,20 @@ self.addEventListener("push", function (event) { if (event.data) { // @ts-expect-error we know this exists const data = event.data.json(); + + let actions = []; + + switch (data.type ?? "unknown") { + case "alert": + actions = [ + { + action: "markReviewed", + title: "Mark as Reviewed", + }, + ]; + break; + } + // @ts-expect-error we know this exists self.registration.showNotification(data.title, { body: data.message, @@ -13,6 +27,7 @@ self.addEventListener("push", function (event) { badge: "/images/maskable-badge.png", tag: data.id, data: { id: data.id, link: data.direct_url }, + actions, }); } else { // pass @@ -26,14 +41,26 @@ self.addEventListener("notificationclick", (event) => { // @ts-expect-error we know this exists event.notification.close(); - // @ts-expect-error we know this exists - if (event.notification.data) { - const url = event.notification.data.link; - // eslint-disable-next-line no-undef - if (clients.openWindow) { - // eslint-disable-next-line no-undef - return clients.openWindow(url); - } + switch (event.action ?? "default") { + case "markReviewed": + if (event.notification.data) { + fetch("/api/reviews/viewed", { + method: "POST", + headers: { "Content-Type": "application/json", "X-CSRF-TOKEN": 1 }, + body: JSON.stringify({ ids: [event.notification.data.id] }), + }); + } + break; + default: + // @ts-expect-error we know this exists + if (event.notification.data) { + const url = event.notification.data.link; + // eslint-disable-next-line no-undef + if (clients.openWindow) { + // eslint-disable-next-line no-undef + return clients.openWindow(url); + } + } } } });