From f66545bbd8c7bcfce868591b9d6e1bec9d60ee71 Mon Sep 17 00:00:00 2001 From: Kris Hu Date: Thu, 28 Aug 2025 20:05:50 +0800 Subject: [PATCH] add a fallback push notification so that a notification will always be generated from a push event, avoiding being killed for privacy policies from browser and os --- src/sw.ts | 4 +++- src/sw/pushNotification.ts | 26 +++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/sw.ts b/src/sw.ts index 2b6209e8..2dfb7e2c 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -1,7 +1,7 @@ /// import { precacheAndRoute, cleanupOutdatedCaches } from 'workbox-precaching'; -import { usePushNotifications } from './sw/pushNotification'; import { EventType } from "matrix-js-sdk/lib/@types/event"; +import { usePushNotifications } from './sw/pushNotification'; export type {}; declare const self: ServiceWorkerGlobalScope; @@ -204,6 +204,8 @@ self.addEventListener('notificationclick', (event: NotificationEvent) => { return Promise.resolve(); }) ); + + return Promise.resolve(); }); if (self.__WB_MANIFEST) { diff --git a/src/sw/pushNotification.ts b/src/sw/pushNotification.ts index 8e5073bc..140f3177 100644 --- a/src/sw/pushNotification.ts +++ b/src/sw/pushNotification.ts @@ -1,3 +1,4 @@ +/* eslint-disable camelcase */ import { EventType } from "matrix-js-sdk/lib/@types/event"; const DEFAULT_NOTIFICATION_ICON = '/public/res/apple/apple-touch-icon-180x180.png'; @@ -79,28 +80,35 @@ export const usePushNotifications = (self: ServiceWorkerGlobalScope) => { ) }; + const fallbackNotification = async () => { + await self.registration.showNotification("You have a new notification", { + icon: DEFAULT_NOTIFICATION_ICON, + badge: DEFAULT_NOTIFICATION_BADGE, + tag: "Cinny", + }); + }; + const handlePushNotificationPushData = async (pushData: any) => { const eventType = pushData?.type as (EventType | undefined); if (!eventType) { - console.log("no event type"); - return; + console.warn("no event type"); } switch (eventType) { case EventType.RoomMessage: - await handleRoomMessageNotification(pushData); - return; + return handleRoomMessageNotification(pushData); case EventType.RoomMessageEncrypted: - await handleEncryptedMessageNotification(pushData); - return; + return handleEncryptedMessageNotification(pushData); case EventType.RoomMember: - if (!(pushData?.content?.membership == "invite")) return; - await handleInvitationNotification(pushData); - return; + if (!(pushData?.content?.membership === "invite")) break; + return handleInvitationNotification(pushData); + default: // no voip support in app anyway return; } + + return fallbackNotification(); }; return { handlePushNotificationPushData };