Make closing notifications work

This commit is contained in:
binwiederhier
2026-01-14 21:17:21 -05:00
parent 96638b516c
commit ff2b8167b4
3 changed files with 31 additions and 3 deletions

View File

@@ -81,6 +81,13 @@ const handlePushMessageDelete = async (data) => {
await db.notifications.where({ subscriptionId, sequenceId }).delete(); await db.notifications.where({ subscriptionId, sequenceId }).delete();
} }
// Close browser notification with matching tag
const tag = message.sequence_id || message.id;
if (tag) {
const notifications = await self.registration.getNotifications({ tag });
notifications.forEach((notification) => notification.close());
}
// Update subscription last message id (for ?since=... queries) // Update subscription last message id (for ?since=... queries)
await db.subscriptions.update(subscriptionId, { await db.subscriptions.update(subscriptionId, {
last: message.id, last: message.id,
@@ -101,6 +108,13 @@ const handlePushMessageClear = async (data) => {
await db.notifications.where({ subscriptionId, sequenceId }).modify({ new: 0 }); await db.notifications.where({ subscriptionId, sequenceId }).modify({ new: 0 });
} }
// Close browser notification with matching tag
const tag = message.sequence_id || message.id;
if (tag) {
const notifications = await self.registration.getNotifications({ tag });
notifications.forEach((notification) => notification.close());
}
// Update subscription last message id (for ?since=... queries) // Update subscription last message id (for ?since=... queries)
await db.subscriptions.update(subscriptionId, { await db.subscriptions.update(subscriptionId, {
last: message.id, last: message.id,
@@ -108,7 +122,6 @@ const handlePushMessageClear = async (data) => {
// Update badge count // Update badge count
const badgeCount = await db.notifications.where({ new: 1 }).count(); const badgeCount = await db.notifications.where({ new: 1 }).count();
console.log("[ServiceWorker] Setting new app badge count", { badgeCount });
self.navigator.setAppBadge?.(badgeCount); self.navigator.setAppBadge?.(badgeCount);
}; };

View File

@@ -31,6 +31,21 @@ class Notifier {
); );
} }
async cancel(notification) {
if (!this.supported()) {
return;
}
try {
const tag = notification.sequence_id || notification.id;
console.log(`[Notifier] Cancelling notification with ${tag}`);
const registration = await this.serviceWorkerRegistration();
const notifications = await registration.getNotifications({ tag });
notifications.forEach((notification) => notification.close());
} catch (e) {
console.log(`[Notifier] Error cancelling notification`, e);
}
}
async playSound() { async playSound() {
// Play sound // Play sound
const sound = await prefs.sound(); const sound = await prefs.sound();

View File

@@ -55,11 +55,11 @@ export const useConnectionListeners = (account, subscriptions, users, webPushTop
// and FirebaseService::handleMessage(). // and FirebaseService::handleMessage().
if (notification.event === EVENT_MESSAGE_DELETE && notification.sequence_id) { if (notification.event === EVENT_MESSAGE_DELETE && notification.sequence_id) {
// Handle delete: remove notification from database
await subscriptionManager.deleteNotificationBySequenceId(subscriptionId, notification.sequence_id); await subscriptionManager.deleteNotificationBySequenceId(subscriptionId, notification.sequence_id);
await notifier.cancel(notification);
} else if (notification.event === EVENT_MESSAGE_CLEAR && notification.sequence_id) { } else if (notification.event === EVENT_MESSAGE_CLEAR && notification.sequence_id) {
// Handle read: mark notification as read
await subscriptionManager.markNotificationReadBySequenceId(subscriptionId, notification.sequence_id); await subscriptionManager.markNotificationReadBySequenceId(subscriptionId, notification.sequence_id);
await notifier.cancel(notification);
} else { } else {
// Regular message: delete existing and add new // Regular message: delete existing and add new
const sequenceId = notification.sequence_id || notification.id; const sequenceId = notification.sequence_id || notification.id;