1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-28 22:17:15 +01:00

Implement http actions in service worker

These are only supported in Chrome-based browsers via the service worker
and not for regular desktop notifications.
This commit is contained in:
nimbleghost 2023-06-08 20:12:41 +02:00
parent 75a4b5bd88
commit 4ce6fdcc5a

View file

@ -48,6 +48,13 @@ self.addEventListener("push", (event) => {
const image = message.attachment?.name.match(/\.(png|jpe?g|gif|webp)$/i) ? message.attachment.url : undefined; const image = message.attachment?.name.match(/\.(png|jpe?g|gif|webp)$/i) ? message.attachment.url : undefined;
const actions = message.actions
?.filter(({ action }) => action === "view" || action === "http")
.map(({ label }) => ({
action: label,
title: label,
}));
await Promise.all([ await Promise.all([
(async () => { (async () => {
await db.notifications.add({ await db.notifications.add({
@ -71,6 +78,7 @@ self.addEventListener("push", (event) => {
image, image,
data, data,
timestamp: message.time * 1_000, timestamp: message.time * 1_000,
actions,
}), }),
]); ]);
} else { } else {
@ -88,8 +96,6 @@ self.addEventListener("push", (event) => {
self.addEventListener("notificationclick", (event) => { self.addEventListener("notificationclick", (event) => {
console.log("[ServiceWorker] NotificationClick"); console.log("[ServiceWorker] NotificationClick");
event.notification.close();
event.waitUntil( event.waitUntil(
(async () => { (async () => {
const clients = await self.clients.matchAll({ type: "window" }); const clients = await self.clients.matchAll({ type: "window" });
@ -97,20 +103,52 @@ self.addEventListener("notificationclick", (event) => {
const rootUrl = new URL(self.location.origin); const rootUrl = new URL(self.location.origin);
const rootClient = clients.find((client) => client.url === rootUrl.toString()); const rootClient = clients.find((client) => client.url === rootUrl.toString());
if (event.notification.data.event !== "message") { if (event.notification.data?.event !== "message") {
if (rootClient) { if (rootClient) {
rootClient.focus(); rootClient.focus();
} else { } else {
self.clients.openWindow(rootUrl); self.clients.openWindow(rootUrl);
} }
event.notification.close();
} else { } else {
const { message } = event.notification.data; const { message } = event.notification.data;
if (message.click) { if (event.action) {
self.clients.openWindow(message.click); const action = event.notification.data.message.actions.find(({ label }) => event.action === label);
return;
if (action.action === "view") {
self.clients.openWindow(action.url);
if (action.clear) {
event.notification.close();
}
} else if (action.action === "http") {
try {
const response = await fetch(action.url, {
method: action.method ?? "POST",
headers: action.headers ?? {},
body: action.body,
});
if (!response.ok) {
throw new Error(`HTTP ${response.status} ${response.statusText}`);
} }
if (action.clear) {
event.notification.close();
}
} catch (e) {
console.error("[ServiceWorker] Error performing http action", e);
self.registration.showNotification(`Unsuccessful action ${action.label} (${action.action})`, {
body: e.message,
});
}
}
} else if (message.click) {
self.clients.openWindow(message.click);
event.notification.close();
} else {
const topicUrl = new URL(message.topic, self.location.origin); const topicUrl = new URL(message.topic, self.location.origin);
const topicClient = clients.find((client) => client.url === topicUrl.toString()); const topicClient = clients.find((client) => client.url === topicUrl.toString());
@ -121,6 +159,9 @@ self.addEventListener("notificationclick", (event) => {
} else { } else {
self.clients.openWindow(topicUrl); self.clients.openWindow(topicUrl);
} }
event.notification.close();
}
} }
})() })()
); );