From 775a17e5d030a1441bc8c5344d6801ec03d3d23e Mon Sep 17 00:00:00 2001 From: Evie Gauthier Date: Tue, 3 Mar 2026 10:36:38 -0500 Subject: [PATCH] Add toggle-able message previews to notifictions --- .../notifications/SystemNotification.tsx | 38 +++++++++++++++++ .../pages/client/BackgroundNotifications.tsx | 30 +++++++++++-- src/app/pages/client/ClientNonUIFeatures.tsx | 36 ++++++++++++++-- src/app/state/settings.ts | 4 ++ src/app/utils/notificationStyle.ts | 42 ++++++++++++++++++- src/sw.ts | 14 +++++++ src/sw/pushNotification.ts | 19 ++++++++- 7 files changed, 172 insertions(+), 11 deletions(-) diff --git a/src/app/features/settings/notifications/SystemNotification.tsx b/src/app/features/settings/notifications/SystemNotification.tsx index 67b40c2a..04e31db7 100644 --- a/src/app/features/settings/notifications/SystemNotification.tsx +++ b/src/app/features/settings/notifications/SystemNotification.tsx @@ -169,6 +169,14 @@ export function SystemNotification() { settingsAtom, 'isNotificationSounds' ); + const [showMessageContent, setShowMessageContent] = useSetting( + settingsAtom, + 'showMessageContentInNotifications' + ); + const [showEncryptedMessageContent, setShowEncryptedMessageContent] = useSetting( + settingsAtom, + 'showMessageContentInEncryptedNotifications' + ); return ( @@ -193,6 +201,36 @@ export function SystemNotification() { after={} /> + + } + /> + + + + } + /> + { const payload = buildRoomMessageNotification({ roomName, roomAvatar, username, - previewText: 'new message', + previewText, silent: true, eventId, }); @@ -267,6 +278,7 @@ function MessageNotifications() { } if (showNotifications && notificationPermission('granted')) { + const isEncryptedRoom = !!getStateEvent(room, StateEvent.RoomEncryption); const avatarMxc = room.getAvatarFallbackMember()?.getMxcAvatarUrl() ?? room.getMxcAvatarUrl(); notify({ @@ -280,6 +292,13 @@ function MessageNotifications() { sender, roomId: room.roomId, eventId, + previewText: resolveNotificationPreviewText({ + content: mEvent.getContent(), + eventType: mEvent.getType(), + isEncryptedRoom, + showMessageContent, + showEncryptedMessageContent, + }), }); } @@ -296,6 +315,8 @@ function MessageNotifications() { notificationSound, notificationSelected, showNotifications, + showMessageContent, + showEncryptedMessageContent, usePushNotifications, forcePushOnMobile, playSound, @@ -386,6 +407,11 @@ function HandleNotificationClick() { function SyncNotificationSettingsWithServiceWorker() { const [notificationSound] = useSetting(settingsAtom, 'isNotificationSounds'); const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications'); + const [showMessageContent] = useSetting(settingsAtom, 'showMessageContentInNotifications'); + const [showEncryptedMessageContent] = useSetting( + settingsAtom, + 'showMessageContentInEncryptedNotifications' + ); useEffect(() => { if (!('serviceWorker' in navigator)) return; @@ -394,13 +420,15 @@ function SyncNotificationSettingsWithServiceWorker() { type: 'setNotificationSettings' as const, notificationSoundEnabled: notificationSound, preferPushOnMobile, + showMessageContent, + showEncryptedMessageContent, }; navigator.serviceWorker.controller?.postMessage(payload); navigator.serviceWorker.ready.then((registration) => { registration.active?.postMessage(payload); }); - }, [notificationSound, usePushNotifications]); + }, [notificationSound, usePushNotifications, showMessageContent, showEncryptedMessageContent]); return null; } diff --git a/src/app/state/settings.ts b/src/app/state/settings.ts index fe104bc5..a68f8dfc 100644 --- a/src/app/state/settings.ts +++ b/src/app/state/settings.ts @@ -46,6 +46,8 @@ export interface Settings { usePushNotifications: boolean; useInAppNotifications: boolean; isNotificationSounds: boolean; + showMessageContentInNotifications: boolean; + showMessageContentInEncryptedNotifications: boolean; hour24Clock: boolean; dateFormatString: string; @@ -97,6 +99,8 @@ const defaultSettings: Settings = { usePushNotifications: false, useInAppNotifications: true, isNotificationSounds: true, + showMessageContentInNotifications: false, + showMessageContentInEncryptedNotifications: false, hour24Clock: false, dateFormatString: 'D MMM YYYY', diff --git a/src/app/utils/notificationStyle.ts b/src/app/utils/notificationStyle.ts index a3e5a193..0bfd85ad 100644 --- a/src/app/utils/notificationStyle.ts +++ b/src/app/utils/notificationStyle.ts @@ -1,5 +1,7 @@ export const DEFAULT_NOTIFICATION_ICON = '/public/res/apple/apple-touch-icon-180x180.png'; export const DEFAULT_NOTIFICATION_BADGE = '/public/res/apple/apple-touch-icon-72x72.png'; +export const DEFAULT_MESSAGE_PREVIEW = 'new message'; +export const ENCRYPTED_MESSAGE_PREVIEW = 'Encrypted message'; type RoomMessageNotificationInput = { roomName?: string; @@ -16,12 +18,50 @@ type NotificationPayload = { options: NotificationOptions; }; +type NotificationPreviewInput = { + content?: unknown; + eventType?: string; + isEncryptedRoom?: boolean; + showMessageContent: boolean; + showEncryptedMessageContent: boolean; +}; + const getString = (value: unknown, fallback: string): string => { if (typeof value !== 'string') return fallback; const normalized = value.trim(); return normalized.length > 0 ? normalized : fallback; }; +const getBodyFromContent = (content: unknown): string | undefined => { + if (!content || typeof content !== 'object') return undefined; + const { body } = content as Record; + if (typeof body !== 'string') return undefined; + const normalized = body.trim(); + return normalized.length > 0 ? normalized : undefined; +}; + +export const resolveNotificationPreviewText = ({ + content, + eventType, + isEncryptedRoom, + showMessageContent, + showEncryptedMessageContent, +}: NotificationPreviewInput): string => { + const encryptedContext = isEncryptedRoom || eventType === 'm.room.encrypted'; + + if (!showMessageContent) { + return encryptedContext ? ENCRYPTED_MESSAGE_PREVIEW : DEFAULT_MESSAGE_PREVIEW; + } + if (encryptedContext && !showEncryptedMessageContent) { + return ENCRYPTED_MESSAGE_PREVIEW; + } + + const body = getBodyFromContent(content); + if (body) return body; + + return encryptedContext ? ENCRYPTED_MESSAGE_PREVIEW : DEFAULT_MESSAGE_PREVIEW; +}; + export const buildRoomMessageNotification = ({ roomName, username, @@ -33,7 +73,7 @@ export const buildRoomMessageNotification = ({ }: RoomMessageNotificationInput): NotificationPayload => { const sender = getString(username, 'Someone'); const room = getString(roomName, 'Unknown'); - const message = getString(previewText, 'new message'); + const message = getString(previewText, DEFAULT_MESSAGE_PREVIEW); const avatar = getString(roomAvatar, DEFAULT_NOTIFICATION_ICON); return { diff --git a/src/sw.ts b/src/sw.ts index 5fd27a35..51c5124a 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -9,8 +9,12 @@ declare const self: ServiceWorkerGlobalScope; let notificationSoundEnabled = true; let preferPushOnMobile = false; +let showMessageContent = false; +let showEncryptedMessageContent = false; const { handlePushNotificationPushData } = createPushNotifications(self, () => ({ notificationSoundEnabled, + showMessageContent, + showEncryptedMessageContent, })); type SessionInfo = { @@ -124,6 +128,16 @@ self.addEventListener('message', (event: ExtendableMessageEvent) => { if (typeof (data as { preferPushOnMobile?: unknown }).preferPushOnMobile === 'boolean') { preferPushOnMobile = (data as { preferPushOnMobile: boolean }).preferPushOnMobile; } + if (typeof (data as { showMessageContent?: unknown }).showMessageContent === 'boolean') { + showMessageContent = (data as { showMessageContent: boolean }).showMessageContent; + } + if ( + typeof (data as { showEncryptedMessageContent?: unknown }).showEncryptedMessageContent === + 'boolean' + ) { + showEncryptedMessageContent = (data as { showEncryptedMessageContent: boolean }) + .showEncryptedMessageContent; + } } }); diff --git a/src/sw/pushNotification.ts b/src/sw/pushNotification.ts index 19f95226..1459df54 100644 --- a/src/sw/pushNotification.ts +++ b/src/sw/pushNotification.ts @@ -3,10 +3,13 @@ import { buildRoomMessageNotification, DEFAULT_NOTIFICATION_ICON, DEFAULT_NOTIFICATION_BADGE, + resolveNotificationPreviewText, } from '../app/utils/notificationStyle'; type NotificationSettings = { notificationSoundEnabled: boolean; + showMessageContent: boolean; + showEncryptedMessageContent: boolean; }; export const createPushNotifications = ( @@ -49,7 +52,13 @@ export const createPushNotifications = ( roomName: pushData?.room_name, username: pushData?.sender_display_name, roomAvatar: pushData?.room_avatar_url, - previewText: 'new message', + previewText: resolveNotificationPreviewText({ + content: pushData?.content, + eventType: pushData?.type, + isEncryptedRoom: false, + showMessageContent: getNotificationSettings().showMessageContent, + showEncryptedMessageContent: getNotificationSettings().showEncryptedMessageContent, + }), silent: resolveSilent(pushData?.silent), eventId: pushData?.event_id, data, @@ -77,7 +86,13 @@ export const createPushNotifications = ( roomName: pushData?.room_name, username: pushData?.sender_display_name, roomAvatar: pushData?.room_avatar_url, - previewText: 'Encrypted message', + previewText: resolveNotificationPreviewText({ + content: pushData?.content, + eventType: pushData?.type, + isEncryptedRoom: true, + showMessageContent: getNotificationSettings().showMessageContent, + showEncryptedMessageContent: getNotificationSettings().showEncryptedMessageContent, + }), silent: resolveSilent(pushData?.silent), eventId: pushData?.event_id, data,