From 54d2ddf1f5e1429a41ca23fd620c009269d6e9b0 Mon Sep 17 00:00:00 2001 From: Kris Hu Date: Mon, 28 Jul 2025 15:43:56 +0800 Subject: [PATCH] keep push notification alive currenttly all browser requrire a `push` produces a notification, if not, push subscription may be revoked. see: https://github.com/firebase/firebase-js-sdk/issues/8010#issuecomment-2119275829 in this case, sygnal, the push gateway will send unread count, and create a `push` event, when messages are read on other clients. Therefore we have no choice but to stop sygnal from pushing data that won't be presented as notifications see: https://github.com/element-hq/sygnal/blob/da48ba7457409391c1d12140ce2ea3d49d377ff3/sygnal/webpushpushkin.py#L142-L145 --- .../notifications/PushNotifications.tsx | 1 + src/app/pages/client/ClientNonUIFeatures.tsx | 11 +++++- src/app/state/serviceWorkerRegistration.ts | 3 ++ src/sw.ts | 34 +++++++++---------- 4 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 src/app/state/serviceWorkerRegistration.ts diff --git a/src/app/features/settings/notifications/PushNotifications.tsx b/src/app/features/settings/notifications/PushNotifications.tsx index 8a872a44..a67667ca 100644 --- a/src/app/features/settings/notifications/PushNotifications.tsx +++ b/src/app/features/settings/notifications/PushNotifications.tsx @@ -41,6 +41,7 @@ export async function enablePushNotifications( data: { url: clientConfig.pushNotificationDetails?.pushNotifyUrl, // format: 'event_id_only' as const, + events_only: true, endpoint: pushSubAtom.endpoint, p256dh: pushSubAtom.keys!.p256dh!, auth: pushSubAtom.keys!.auth!, diff --git a/src/app/pages/client/ClientNonUIFeatures.tsx b/src/app/pages/client/ClientNonUIFeatures.tsx index f9261df6..9acb90b3 100644 --- a/src/app/pages/client/ClientNonUIFeatures.tsx +++ b/src/app/pages/client/ClientNonUIFeatures.tsx @@ -27,6 +27,7 @@ import { useSelectedRoom } from '../../hooks/router/useSelectedRoom'; import { useInboxNotificationsSelected } from '../../hooks/router/useInbox'; import { useMediaAuthentication } from '../../hooks/useMediaAuthentication'; import { useRoomNavigate } from '../../hooks/useRoomNavigate'; +import { registrationAtom } from '../../state/serviceWorkerRegistration'; function SystemEmojiFeature() { const [twitterEmoji] = useSetting(settingsAtom, 'twitterEmoji'); @@ -54,6 +55,8 @@ function PageZoomFeature() { function FaviconUpdater() { const roomToUnread = useAtomValue(roomToUnreadAtom); + const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications'); + const registration = useAtomValue(registrationAtom); useEffect(() => { let notification = false; @@ -81,7 +84,13 @@ function FaviconUpdater() { } catch (e) { // Likely Firefox/Gecko-based and doesn't support badging API } - }, [roomToUnread]); + if (usePushNotifications && total === 0) { + registration.getNotifications() + .then((pushNotifications) => pushNotifications + .forEach((pushNotification) => pushNotification.close())); + navigator.clearAppBadge(); + } + }, [roomToUnread, usePushNotifications, registration]); return null; } diff --git a/src/app/state/serviceWorkerRegistration.ts b/src/app/state/serviceWorkerRegistration.ts new file mode 100644 index 00000000..6e9f94bd --- /dev/null +++ b/src/app/state/serviceWorkerRegistration.ts @@ -0,0 +1,3 @@ +import { atom } from "jotai"; + +export const registrationAtom = atom(async () => navigator.serviceWorker.ready); diff --git a/src/sw.ts b/src/sw.ts index e9716e3e..2b6209e8 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -140,23 +140,23 @@ const onPushNotification = async (event: PushEvent) => { const pushData = event.data.json(); console.log(pushData); - try { - if (typeof pushData?.unread === 'number') { - self.navigator.setAppBadge(pushData.unread); - - if (pushData.unread == 0) { - self.registration.getNotifications() - .then((notifications) => notifications - .forEach((notification) => notification.close())); - await navigator.clearAppBadge(); - return; - } - } else { - await navigator.clearAppBadge(); - } - } catch (_) { - // Likely Firefox/Gecko-based and doesn't support badging API - } + // try { + // if (typeof pushData?.unread === 'number') { + // self.navigator.setAppBadge(pushData.unread); + // + // if (pushData.unread == 0) { + // self.registration.getNotifications() + // .then((notifications) => notifications + // .forEach((notification) => notification.close())); + // await navigator.clearAppBadge(); + // return; + // } + // } else { + // await navigator.clearAppBadge(); + // } + // } catch (_) { + // // Likely Firefox/Gecko-based and doesn't support badging API + // } await handlePushNotificationPushData(pushData); };