Use attachment URL for image & add timestamp

pull/751/head
nimbleghost 2023-06-07 21:40:35 +02:00
parent f3db0e083e
commit a8db08c7d4
2 changed files with 13 additions and 2 deletions

View File

@ -46,6 +46,8 @@ self.addEventListener("push", (event) => {
const db = await getDbAsync(); const db = await getDbAsync();
const image = message.attachment?.name.match(/\.(png|jpe?g|gif|webp)$/i) ? message.attachment.url : undefined;
await Promise.all([ await Promise.all([
(async () => { (async () => {
await db.notifications.add({ await db.notifications.add({
@ -61,11 +63,14 @@ self.addEventListener("push", (event) => {
db.subscriptions.update(subscriptionId, { db.subscriptions.update(subscriptionId, {
last: message.id, last: message.id,
}), }),
// Please update the desktop notification in Notifier.js to match any changes
self.registration.showNotification(formatTitleWithDefault(message, message.topic), { self.registration.showNotification(formatTitleWithDefault(message, message.topic), {
tag: subscriptionId, tag: subscriptionId,
body: formatMessage(message), body: formatMessage(message),
icon: "/static/images/ntfy.png", icon: image ?? "/static/images/ntfy.png",
image,
data, data,
timestamp: message.time * 1_000,
}), }),
]); ]);
} else { } else {
@ -81,6 +86,8 @@ self.addEventListener("push", (event) => {
}); });
self.addEventListener("notificationclick", (event) => { self.addEventListener("notificationclick", (event) => {
console.log("[ServiceWorker] NotificationClick");
event.notification.close(); event.notification.close();
event.waitUntil( event.waitUntil(

View File

@ -17,13 +17,17 @@ class Notifier {
const displayName = topicDisplayName(subscription); const displayName = topicDisplayName(subscription);
const message = formatMessage(notification); const message = formatMessage(notification);
const title = formatTitleWithDefault(notification, displayName); const title = formatTitleWithDefault(notification, displayName);
const image = notification.attachment?.name.match(/\.(png|jpe?g|gif|webp)$/i) ? notification.attachment.url : undefined;
// Show notification // Show notification
console.log(`[Notifier, ${shortUrl}] Displaying notification ${notification.id}: ${message}`); console.log(`[Notifier, ${shortUrl}] Displaying notification ${notification.id}: ${message}`);
// Please update sw.js if formatting changes
const n = new Notification(title, { const n = new Notification(title, {
body: message, body: message,
tag: subscription.id, tag: subscription.id,
icon: logo, icon: image ?? logo,
image,
timestamp: message.time * 1_000,
}); });
if (notification.click) { if (notification.click) {
n.onclick = () => openUrl(notification.click); n.onclick = () => openUrl(notification.click);