This commit is contained in:
Alexander Zinchuk 2021-08-16 21:22:06 +03:00
parent b7953f545b
commit b485a247dc
2 changed files with 20 additions and 3 deletions

View File

@ -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 = () => {

View File

@ -321,7 +321,7 @@ type NotificationData = {
icon?: string;
};
const shownNotifications = new Set();
const handledNotifications = new Set();
let pendingNotifications: Record<number, NotificationData[]> = {};
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) {