add background notif sound setting

This commit is contained in:
7w1 committed 2026-06-16 19:38:13 -05:00
1 parent 85d69185eb
commit cba206019a
5 files changed
+39 -11

No files matched your search

@@ -0,0 +1,5 @@
---
default: patch
---
Added a setting to play notification sounds when tab is in background, enabled by default.
@@ -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={<Switch value={isNotificationSounds} onChange={setIsNotificationSounds} />}
/>
<SettingTile
title="Background Tab Sound"
focusId="background-notification-sound"
description="Play the notification sound when this browser tab is in the background."
after={
<Switch
value={backgroundNotificationSounds}
onChange={setBackgroundNotificationSounds}
disabled={!isNotificationSounds}
/>
}
/>
</SequenceCard>
<SequenceCard
className={SequenceCardStyle}
@@ -157,6 +157,7 @@ const settingsLinkFocusIdsBySection: Record<SettingsSectionId, readonly string[]
'favicon-dot-mentions-only',
'highlight-mentions',
'in-app-notification-sound',
'background-notification-sound',
'in-app-notifications',
'mention-room',
'mention-user-id',
+15 -11
View File
@@ -178,6 +178,7 @@ function InviteNotifications() {
const [showSystemNotifications] = useSetting(settingsAtom, 'useSystemNotifications');
const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications');
const [notificationSound] = useSetting(settingsAtom, 'isNotificationSounds');
const [backgroundNotificationSounds] = useSetting(settingsAtom, 'backgroundNotificationSounds');
const notify = useCallback(
(count: number) => {
@@ -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,
+2
View File
@@ -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,