diff --git a/src/serviceWorker/pushNotification.ts b/src/serviceWorker/pushNotification.ts index d0ffde01b..fbade4411 100644 --- a/src/serviceWorker/pushNotification.ts +++ b/src/serviceWorker/pushNotification.ts @@ -208,6 +208,11 @@ export function handleClientMessage(e: ExtendableMessageEvent) { e.waitUntil(showNotification(notification)); shownNotifications.add(notification.messageId); } + if (e.data.type === 'notificationHandled') { + const notification: NotificationData = e.data.payload; + // mark this notification as shown if it was handled locally + shownNotifications.add(notification.messageId); + } } self.onsync = () => { diff --git a/src/util/notifications.ts b/src/util/notifications.ts index 7daa8f3e9..bd1e36ada 100644 --- a/src/util/notifications.ts +++ b/src/util/notifications.ts @@ -321,7 +321,7 @@ type NotificationData = { icon?: string; }; -const shownNotifications = new Set(); +const handledNotifications = new Set(); let pendingNotifications: Record = {}; async function showNotifications(groupLimit: number = 2) { @@ -362,8 +362,8 @@ const flushNotifications = debounce(showNotifications, 1000, false); async function handleNotification(data: NotificationData, groupLimit?: number) { // Dont show already triggered notification - if (shownNotifications.has(data.messageId)) { - shownNotifications.delete(data.messageId); + if (handledNotifications.has(data.messageId)) { + handledNotifications.delete(data.messageId); return; } @@ -373,6 +373,18 @@ async function handleNotification(data: NotificationData, groupLimit?: number) { } pendingNotifications[groupId].push(data); await flushNotifications(groupLimit); + + if (checkIfPushSupported()) { + if (navigator.serviceWorker.controller) { + // notify service worker that notification was handled locally + navigator.serviceWorker.controller.postMessage({ + type: 'notificationHandled', + payload: data, + }); + } + } + + handledNotifications.add(data.messageId); } function showNotification(data: NotificationData) {