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
This commit is contained in:
1 parent
75816cf991
commit
54d2ddf1f5
4 files changed
+31
-18
No files matched your search
@@ -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!,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
import { atom } from "jotai";
|
||||
|
||||
export const registrationAtom = atom(async () => navigator.serviceWorker.ready);
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user