Add toggle-able message previews to notifictions
This commit is contained in:
1 parent
541654b5b2
commit
775a17e5d0
7 files changed
+172
-11
No files matched your search
@@ -169,6 +169,14 @@ export function SystemNotification() {
|
||||
settingsAtom,
|
||||
'isNotificationSounds'
|
||||
);
|
||||
const [showMessageContent, setShowMessageContent] = useSetting(
|
||||
settingsAtom,
|
||||
'showMessageContentInNotifications'
|
||||
);
|
||||
const [showEncryptedMessageContent, setShowEncryptedMessageContent] = useSetting(
|
||||
settingsAtom,
|
||||
'showMessageContentInEncryptedNotifications'
|
||||
);
|
||||
|
||||
return (
|
||||
<Box direction="Column" gap="100">
|
||||
@@ -193,6 +201,36 @@ export function SystemNotification() {
|
||||
after={<Switch value={showInAppNotifs} onChange={setShowInAppNotifs} />}
|
||||
/>
|
||||
</SequenceCard>
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<SettingTile
|
||||
title="Show Message Content"
|
||||
description="Include message text in notification bodies."
|
||||
after={<Switch value={showMessageContent} onChange={setShowMessageContent} />}
|
||||
/>
|
||||
</SequenceCard>
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<SettingTile
|
||||
title="Show Encrypted Message Content"
|
||||
description="Allow message text from encrypted rooms in notification bodies. May not work on some platforms due to technical limitations."
|
||||
after={
|
||||
<Switch
|
||||
value={showEncryptedMessageContent}
|
||||
onChange={setShowEncryptedMessageContent}
|
||||
disabled={!showMessageContent}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</SequenceCard>
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
|
||||
@@ -19,13 +19,21 @@ import {
|
||||
import { useSetting } from '$state/hooks/settings';
|
||||
import { settingsAtom } from '$state/settings';
|
||||
import { getMxIdLocalPart, mxcUrlToHttp } from '$utils/matrix';
|
||||
import { getMemberDisplayName, getNotificationType, isNotificationEvent } from '$utils/room';
|
||||
import { NotificationType } from '$types/matrix/room';
|
||||
import {
|
||||
getMemberDisplayName,
|
||||
getNotificationType,
|
||||
getStateEvent,
|
||||
isNotificationEvent,
|
||||
} from '$utils/room';
|
||||
import { NotificationType, StateEvent } from '$types/matrix/room';
|
||||
import { createLogger } from '$utils/debug';
|
||||
import LogoSVG from '$public/res/svg/cinny.svg';
|
||||
import { nicknamesAtom } from '$state/nicknames';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import { buildRoomMessageNotification } from '$utils/notificationStyle';
|
||||
import {
|
||||
buildRoomMessageNotification,
|
||||
resolveNotificationPreviewText,
|
||||
} from '$utils/notificationStyle';
|
||||
import { mobileOrTablet } from '$utils/user-agent';
|
||||
import { startClient, stopClient } from '$client/initMatrix';
|
||||
import { useClientConfig } from '$hooks/useClientConfig';
|
||||
@@ -79,6 +87,11 @@ export function BackgroundNotifications() {
|
||||
const [showNotifications] = useSetting(settingsAtom, 'useInAppNotifications');
|
||||
const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications');
|
||||
const [notificationSound] = useSetting(settingsAtom, 'isNotificationSounds');
|
||||
const [showMessageContent] = useSetting(settingsAtom, 'showMessageContentInNotifications');
|
||||
const [showEncryptedMessageContent] = useSetting(
|
||||
settingsAtom,
|
||||
'showMessageContentInEncryptedNotifications'
|
||||
);
|
||||
const forcePushOnMobile = usePushNotifications && mobileOrTablet();
|
||||
const activeMx = useMatrixClient();
|
||||
const nicknames = useAtomValue(nicknamesAtom);
|
||||
@@ -194,6 +207,7 @@ export function BackgroundNotifications() {
|
||||
: LogoSVG;
|
||||
|
||||
const isHighlight = pushActions.tweaks?.highlight === true;
|
||||
const isEncryptedRoom = !!getStateEvent(room, StateEvent.RoomEncryption);
|
||||
|
||||
notifiedEventsRef.current.add(eventId);
|
||||
// Cap the set so it doesn't grow unbounded
|
||||
@@ -206,7 +220,13 @@ export function BackgroundNotifications() {
|
||||
roomName: room.name ?? 'Unknown',
|
||||
roomAvatar,
|
||||
username: senderName,
|
||||
previewText: 'new message',
|
||||
previewText: resolveNotificationPreviewText({
|
||||
content: mEvent.getContent(),
|
||||
eventType: mEvent.getType(),
|
||||
isEncryptedRoom,
|
||||
showMessageContent,
|
||||
showEncryptedMessageContent,
|
||||
}),
|
||||
silent: !notificationSound || !isHighlight,
|
||||
eventId,
|
||||
data: {
|
||||
@@ -248,6 +268,8 @@ export function BackgroundNotifications() {
|
||||
forcePushOnMobile,
|
||||
showNotifications,
|
||||
notificationSound,
|
||||
showMessageContent,
|
||||
showEncryptedMessageContent,
|
||||
activeMx,
|
||||
activeSessionId,
|
||||
setActiveSessionId,
|
||||
|
||||
@@ -18,17 +18,21 @@ import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import {
|
||||
getMemberDisplayName,
|
||||
getNotificationType,
|
||||
getStateEvent,
|
||||
getUnreadInfo,
|
||||
isNotificationEvent,
|
||||
} from '$utils/room';
|
||||
import { NotificationType, UnreadInfo } from '$types/matrix/room';
|
||||
import { NotificationType, StateEvent, UnreadInfo } from '$types/matrix/room';
|
||||
import { getMxIdLocalPart, mxcUrlToHttp } from '$utils/matrix';
|
||||
import { useSelectedRoom } from '$hooks/router/useSelectedRoom';
|
||||
import { useInboxNotificationsSelected } from '$hooks/router/useInbox';
|
||||
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
|
||||
import { registrationAtom } from '$state/serviceWorkerRegistration';
|
||||
import { activeSessionIdAtom, pendingNotificationAtom } from '$state/sessions';
|
||||
import { buildRoomMessageNotification } from '$utils/notificationStyle';
|
||||
import {
|
||||
buildRoomMessageNotification,
|
||||
resolveNotificationPreviewText,
|
||||
} from '$utils/notificationStyle';
|
||||
import { mobileOrTablet } from '$utils/user-agent';
|
||||
import { getInboxInvitesPath, getInboxNotificationsPath } from '../pathUtils';
|
||||
import { BackgroundNotifications } from './BackgroundNotifications';
|
||||
@@ -176,6 +180,11 @@ function MessageNotifications() {
|
||||
const [showNotifications] = useSetting(settingsAtom, 'useInAppNotifications');
|
||||
const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications');
|
||||
const [notificationSound] = useSetting(settingsAtom, 'isNotificationSounds');
|
||||
const [showMessageContent] = useSetting(settingsAtom, 'showMessageContentInNotifications');
|
||||
const [showEncryptedMessageContent] = useSetting(
|
||||
settingsAtom,
|
||||
'showMessageContentInEncryptedNotifications'
|
||||
);
|
||||
const forcePushOnMobile = usePushNotifications && mobileOrTablet();
|
||||
const nicknames = useAtomValue(nicknamesAtom);
|
||||
const nicknamesRef = useRef(nicknames);
|
||||
@@ -192,18 +201,20 @@ function MessageNotifications() {
|
||||
username,
|
||||
roomId,
|
||||
eventId,
|
||||
previewText,
|
||||
}: {
|
||||
roomName: string;
|
||||
roomAvatar?: string;
|
||||
username: string;
|
||||
roomId: string;
|
||||
eventId: string;
|
||||
previewText: string;
|
||||
}) => {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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<string, unknown>;
|
||||
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 {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user