diff --git a/src/app/features/settings/notifications/PushNotifications.tsx b/src/app/features/settings/notifications/PushNotifications.tsx index a67667ca1..5d4221028 100644 --- a/src/app/features/settings/notifications/PushNotifications.tsx +++ b/src/app/features/settings/notifications/PushNotifications.tsx @@ -144,10 +144,11 @@ export async function togglePusher( clientConfig: ClientConfig, visible: boolean, usePushNotifications: boolean, - pushSubscriptionAtom: Atom + pushSubscriptionAtom: Atom, + keepEnabledWhenVisible = false ): Promise { if (usePushNotifications) { - if (visible) { + if (visible && !keepEnabledWhenVisible) { await disablePushNotifications(mx, clientConfig, pushSubscriptionAtom); } else { await enablePushNotifications(mx, clientConfig, pushSubscriptionAtom); diff --git a/src/app/hooks/useAppVisibility.ts b/src/app/hooks/useAppVisibility.ts index f53974c1a..6e2ff2bf6 100644 --- a/src/app/hooks/useAppVisibility.ts +++ b/src/app/hooks/useAppVisibility.ts @@ -7,11 +7,13 @@ import { useClientConfig } from './useClientConfig'; import { useSetting } from '../state/hooks/settings'; import { settingsAtom } from '../state/settings'; import { pushSubscriptionAtom } from '../state/pushSubscription'; +import { mobileOrTablet } from '../utils/user-agent'; export function useAppVisibility(mx: MatrixClient | undefined) { const clientConfig = useClientConfig(); const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications'); const pushSubAtom = useAtom(pushSubscriptionAtom); + const isMobile = mobileOrTablet(); useEffect(() => { const handleVisibilityChange = () => { @@ -33,7 +35,7 @@ export function useAppVisibility(mx: MatrixClient | undefined) { if (!mx) return; const handleVisibilityForNotifications = (isVisible: boolean) => { - togglePusher(mx, clientConfig, isVisible, usePushNotifications, pushSubAtom); + togglePusher(mx, clientConfig, isVisible, usePushNotifications, pushSubAtom, isMobile); }; appEvents.onVisibilityChange = handleVisibilityForNotifications; @@ -41,5 +43,5 @@ export function useAppVisibility(mx: MatrixClient | undefined) { return () => { appEvents.onVisibilityChange = null; }; - }, [mx, clientConfig, usePushNotifications, pushSubAtom]); + }, [mx, clientConfig, usePushNotifications, pushSubAtom, isMobile]); } diff --git a/src/app/pages/client/BackgroundNotifications.tsx b/src/app/pages/client/BackgroundNotifications.tsx index 37aae3fba..2fe646ebe 100644 --- a/src/app/pages/client/BackgroundNotifications.tsx +++ b/src/app/pages/client/BackgroundNotifications.tsx @@ -27,6 +27,7 @@ import LogoSVG from '$public/res/svg/cinny.svg'; import { nicknamesAtom } from '$state/nicknames'; import { useMatrixClient } from '$hooks/useMatrixClient'; import { buildRoomMessageNotification } from '$appUtils/notificationStyle'; +import { mobileOrTablet } from '$appUtils/user-agent'; const log = createLogger('BackgroundNotifications'); @@ -67,7 +68,9 @@ export function BackgroundNotifications() { const activeSessionId = useAtomValue(activeSessionIdAtom); const setActiveSessionId = useSetAtom(activeSessionIdAtom); const [showNotifications] = useSetting(settingsAtom, 'useInAppNotifications'); + const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications'); const [notificationSound] = useSetting(settingsAtom, 'isNotificationSounds'); + const forcePushOnMobile = usePushNotifications && mobileOrTablet(); const activeMx = useMatrixClient(); const nicknames = useAtomValue(nicknamesAtom); const nicknamesRef = useRef(nicknames); @@ -96,6 +99,7 @@ export function BackgroundNotifications() { } useEffect(() => { + if (forcePushOnMobile) return undefined; if (!showNotifications) return undefined; const { current } = clientsRef; @@ -231,6 +235,7 @@ export function BackgroundNotifications() { }; }, [ inactiveSessions, + forcePushOnMobile, showNotifications, notificationSound, activeMx, diff --git a/src/app/pages/client/ClientNonUIFeatures.tsx b/src/app/pages/client/ClientNonUIFeatures.tsx index d796c7eee..5e9bc1cd4 100644 --- a/src/app/pages/client/ClientNonUIFeatures.tsx +++ b/src/app/pages/client/ClientNonUIFeatures.tsx @@ -32,6 +32,7 @@ import { registrationAtom } from '$state/serviceWorkerRegistration'; import { BackgroundNotifications } from './BackgroundNotifications'; import { activeSessionIdAtom, pendingNotificationAtom } from '$state/sessions'; import { buildRoomMessageNotification } from '$appUtils/notificationStyle'; +import { mobileOrTablet } from '$appUtils/user-agent'; function SystemEmojiFeature() { const [twitterEmoji] = useSetting(settingsAtom, 'twitterEmoji'); @@ -109,6 +110,7 @@ function InviteNotifications() { const [showNotifications] = useSetting(settingsAtom, 'useInAppNotifications'); const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications'); const [notificationSound] = useSetting(settingsAtom, 'isNotificationSounds'); + const forcePushOnMobile = usePushNotifications && mobileOrTablet(); const notify = useCallback( (count: number) => { @@ -133,6 +135,7 @@ function InviteNotifications() { }, []); useEffect(() => { + if (forcePushOnMobile) return; if (usePushNotifications && document.visibilityState !== "visible") return; if (invites.length > perviousInviteLen && mx.getSyncState() === 'SYNCING') { if (showNotifications && notificationPermission('granted')) { @@ -149,6 +152,7 @@ function InviteNotifications() { perviousInviteLen, showNotifications, usePushNotifications, + forcePushOnMobile, notificationSound, notify, playSound @@ -171,6 +175,7 @@ function MessageNotifications() { const [showNotifications] = useSetting(settingsAtom, 'useInAppNotifications'); const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications'); const [notificationSound] = useSetting(settingsAtom, 'isNotificationSounds'); + const forcePushOnMobile = usePushNotifications && mobileOrTablet(); const nicknames = useAtomValue(nicknamesAtom); const nicknamesRef = useRef(nicknames); nicknamesRef.current = nicknames; @@ -230,6 +235,7 @@ function MessageNotifications() { removed, data ) => { + if (forcePushOnMobile) return; if (mx.getSyncState() !== 'SYNCING') return; if (usePushNotifications && document.visibilityState !== "visible") return; if (document.hasFocus() && (selectedRoomId === room?.roomId || notificationSelected)) return; @@ -290,6 +296,7 @@ function MessageNotifications() { notificationSelected, showNotifications, usePushNotifications, + forcePushOnMobile, playSound, notify, selectedRoomId, @@ -380,19 +387,22 @@ function HandleNotificationClick() { function SyncNotificationSettingsWithServiceWorker() { const [notificationSound] = useSetting(settingsAtom, 'isNotificationSounds'); + const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications'); useEffect(() => { if (!('serviceWorker' in navigator)) return; + const preferPushOnMobile = usePushNotifications && mobileOrTablet(); const payload = { type: 'setNotificationSettings' as const, notificationSoundEnabled: notificationSound, + preferPushOnMobile, }; navigator.serviceWorker.controller?.postMessage(payload); void navigator.serviceWorker.ready.then((registration) => { registration.active?.postMessage(payload); }); - }, [notificationSound]); + }, [notificationSound, usePushNotifications]); return null; } diff --git a/src/sw.ts b/src/sw.ts index 84d358bee..aafba366f 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -7,6 +7,7 @@ export type {}; declare const self: ServiceWorkerGlobalScope; let notificationSoundEnabled = true; +let preferPushOnMobile = false; const { handlePushNotificationPushData } = usePushNotifications(self, () => ({ notificationSoundEnabled, })); @@ -167,6 +168,9 @@ self.addEventListener('message', (event: ExtendableMessageEvent) => { if (typeof (data as { notificationSoundEnabled?: unknown }).notificationSoundEnabled === 'boolean') { notificationSoundEnabled = (data as { notificationSoundEnabled: boolean }).notificationSoundEnabled; } + if (typeof (data as { preferPushOnMobile?: unknown }).preferPushOnMobile === 'boolean') { + preferPushOnMobile = (data as { preferPushOnMobile: boolean }).preferPushOnMobile; + } } }); @@ -271,7 +275,7 @@ const onPushNotification = async (event: PushEvent) => { const clients = await self.clients.matchAll({ type: 'window', includeUncontrolled: true }); const hasVisibleClient = clients.some((client) => client.visibilityState === 'visible'); - if (hasVisibleClient) { + if (hasVisibleClient && !preferPushOnMobile) { return; }