From 634452120b831c042b143b29e773b0d3bbcc7490 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Mon, 28 Jun 2021 16:35:37 +0300 Subject: [PATCH] Notifications: Do not show stale notifications (#1212) --- src/serviceWorker/pushNotification.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/serviceWorker/pushNotification.ts b/src/serviceWorker/pushNotification.ts index 3bc984841..f3a8489b5 100644 --- a/src/serviceWorker/pushNotification.ts +++ b/src/serviceWorker/pushNotification.ts @@ -31,6 +31,8 @@ type NotificationData = { body: string; }; +let lastSyncAt = new Date().valueOf(); + const clickBuffer: Record = {}; const shownNotifications = new Set(); let pendingNotifications: Record = {}; @@ -111,9 +113,11 @@ async function showNotifications(groupLimit: number = 1) { .map(async (groupId) => { const group = pendingNotifications[Number(groupId)]; if (group.length > groupLimit) { + const lastMessage = group[group.length - 1]; return showNotification({ title: APP_NAME, - body: `You have ${count} notifications from ${group[0].title}`, + body: `You have ${count} notifications from ${lastMessage.title}`, + messageId: lastMessage.messageId, chatId: Number(groupId), }); } @@ -151,6 +155,11 @@ export function handlePush(e: PushEvent) { console.log('[SW] Push received with data', e.data.json()); } } + + // Do not show notifications right after sync (when browser is opened) + // To avoid stale notifications + if (new Date().valueOf() - lastSyncAt < 3000) return; + const data = getPushData(e); // Do not show muted notifications @@ -229,3 +238,7 @@ export function handleClientMessage(e: ExtendableMessageEvent) { shownNotifications.add(notification.messageId); } } + +self.onsync = () => { + lastSyncAt = new Date().valueOf(); +};