From cba206019aeb824d5214ecadb2ba663228680958 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Tue, 16 Jun 2026 19:38:13 -0500 Subject: [PATCH] add background notif sound setting --- .../fix-background-notification-sound.md | 5 ++++ .../notifications/SystemNotification.tsx | 16 ++++++++++++ src/app/features/settings/settingsLink.ts | 1 + src/app/pages/client/ClientNonUIFeatures.tsx | 26 +++++++++++-------- src/app/state/settings.ts | 2 ++ 5 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 .changeset/fix-background-notification-sound.md diff --git a/.changeset/fix-background-notification-sound.md b/.changeset/fix-background-notification-sound.md new file mode 100644 index 000000000..be7d1566c --- /dev/null +++ b/.changeset/fix-background-notification-sound.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +Added a setting to play notification sounds when tab is in background, enabled by default. diff --git a/src/app/features/settings/notifications/SystemNotification.tsx b/src/app/features/settings/notifications/SystemNotification.tsx index fafe0a793..876b96fca 100644 --- a/src/app/features/settings/notifications/SystemNotification.tsx +++ b/src/app/features/settings/notifications/SystemNotification.tsx @@ -175,6 +175,10 @@ export function SystemNotification() { settingsAtom, 'isNotificationSounds' ); + const [backgroundNotificationSounds, setBackgroundNotificationSounds] = useSetting( + settingsAtom, + 'backgroundNotificationSounds' + ); const [showMessageContent, setShowMessageContent] = useSetting( settingsAtom, 'showMessageContentInNotifications' @@ -280,6 +284,18 @@ export function SystemNotification() { description="Play a sound inside the app when a new message arrives." after={} /> + + } + /> { @@ -198,7 +199,7 @@ function InviteNotifications() { const playSound = useCallback(() => { const audioElement = audioRef.current; - audioElement?.play(); + audioElement?.play()?.catch(() => {}); clearMediaSessionQuickly(); }, []); @@ -216,8 +217,8 @@ function InviteNotifications() { // window.Notification may be unavailable in sandboxed environments. } } - // Audio API requires a visible document; skip when hidden. - if (document.visibilityState === 'visible' && notificationSound) { + const tabVisible = document.visibilityState === 'visible'; + if (notificationSound && (tabVisible || backgroundNotificationSounds)) { playSound(); } }, [ @@ -227,6 +228,7 @@ function InviteNotifications() { showSystemNotifications, usePushNotifications, notificationSound, + backgroundNotificationSounds, notify, playSound, ]); @@ -253,6 +255,7 @@ function MessageNotifications() { const [showSystemNotifications] = useSetting(settingsAtom, 'useSystemNotifications'); const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications'); const [notificationSound] = useSetting(settingsAtom, 'isNotificationSounds'); + const [backgroundNotificationSounds] = useSetting(settingsAtom, 'backgroundNotificationSounds'); const [showMessageContent] = useSetting(settingsAtom, 'showMessageContentInNotifications'); const [showEncryptedMessageContent] = useSetting( settingsAtom, @@ -272,7 +275,7 @@ function MessageNotifications() { const playSound = useCallback(() => { const audioElement = audioRef.current; - audioElement?.play(); + audioElement?.play()?.catch(() => {}); clearMediaSessionQuickly(); }, []); @@ -453,8 +456,13 @@ function MessageNotifications() { } } - // Everything below requires the page to be visible (in-app UI + audio). - if (document.visibilityState !== 'visible') return; + const tabVisible = document.visibilityState === 'visible'; + if (notificationSound && isLoud && (tabVisible || backgroundNotificationSounds)) { + playSound(); + } + + // In-app banner requires a visible tab. + if (!tabVisible) return; // Page is visible — show the themed in-app notification banner. // For non-DM rooms, only show banner for highlighted messages (mentions/keywords). @@ -530,11 +538,6 @@ function MessageNotifications() { }, }); } - - // In-app audio: play when notification sounds are enabled AND this notification is loud. - if (notificationSound && isLoud) { - playSound(); - } }; mx.on(RoomEvent.Timeline, handleTimelineEvent); return () => { @@ -543,6 +546,7 @@ function MessageNotifications() { }, [ mx, notificationSound, + backgroundNotificationSounds, notificationSelected, showNotifications, showSystemNotifications, diff --git a/src/app/state/settings.ts b/src/app/state/settings.ts index d5e0dba83..9b5a56241 100644 --- a/src/app/state/settings.ts +++ b/src/app/state/settings.ts @@ -129,6 +129,7 @@ export interface Settings { useInAppNotifications: boolean; useSystemNotifications: boolean; isNotificationSounds: boolean; + backgroundNotificationSounds: boolean; showMessageContentInNotifications: boolean; showMessageContentInEncryptedNotifications: boolean; clearNotificationsOnRead: boolean; @@ -283,6 +284,7 @@ export const defaultSettings: Settings = { useInAppNotifications: mobileOrTablet(), useSystemNotifications: !mobileOrTablet(), isNotificationSounds: true, + backgroundNotificationSounds: true, showMessageContentInNotifications: false, showMessageContentInEncryptedNotifications: false, clearNotificationsOnRead: false,