diff --git a/src/app/features/settings/notifications/NotificationTransportRuntimeFeature.tsx b/src/app/features/settings/notifications/NotificationTransportRuntimeFeature.tsx index 2d6e88be0..358a41083 100644 --- a/src/app/features/settings/notifications/NotificationTransportRuntimeFeature.tsx +++ b/src/app/features/settings/notifications/NotificationTransportRuntimeFeature.tsx @@ -2,8 +2,10 @@ import { useEffect, useMemo, useRef } from 'react'; import { isTauri } from '@tauri-apps/api/core'; import { type as osType } from '@tauri-apps/plugin-os'; import { useMatrixClient } from '$hooks/useMatrixClient'; +import { useClientConfig } from '$hooks/useClientConfig'; import { useSetting } from '$state/hooks/settings'; import { settingsAtom } from '$state/settings'; +import { enableUnifiedPush, listenForUnifiedPushEndpointChanges } from './UnifiedPushNotifications'; import { NotificationTransportRuntime, type NotificationTransportRuntimeContext, @@ -24,9 +26,11 @@ function currentPlatform(): NotificationTransportPlatform { export function NotificationTransportRuntimeFeature() { const mx = useMatrixClient(); + const clientConfig = useClientConfig(); const [backgroundPushEnabled] = useSetting(settingsAtom, 'backgroundPushEnabled'); const [backgroundPushProvider] = useSetting(settingsAtom, 'backgroundPushProvider'); const [pushTransportMode] = useSetting(settingsAtom, 'pushTransportMode'); + const [pushTransportOverride] = useSetting(settingsAtom, 'pushTransportOverride'); const [useInAppNotifications] = useSetting(settingsAtom, 'useInAppNotifications'); const [isNotificationSounds] = useSetting(settingsAtom, 'isNotificationSounds'); const [showMessageContent] = useSetting(settingsAtom, 'showMessageContentInNotifications'); @@ -69,6 +73,51 @@ export function NotificationTransportRuntimeFeature() { void runtimeRef.current?.sync(provider, () => contextRef.current); }, [provider]); + const upConfigRef = useRef<{ unifiedPushAppID?: string; unifiedPushGatewayUrl?: string }>({}); + upConfigRef.current = { + unifiedPushAppID: + pushTransportOverride?.unifiedPushAppID ?? + clientConfig.pushNotificationDetails?.unifiedPushAppID, + unifiedPushGatewayUrl: + pushTransportOverride?.unifiedPushGatewayUrl ?? + clientConfig.pushNotificationDetails?.unifiedPushGatewayUrl, + }; + + // Keep the pusher current: establish it when UnifiedPush becomes the active + // transport (so it survives a fresh install/session, not just a manual toggle) + // and refresh it whenever the distributor rotates the endpoint. Deduped by + // endpoint so re-registration can't loop. + const lastEndpointRef = useRef(null); + useEffect(() => { + if (provider !== 'unifiedpush' || !mx) return undefined; + + const establish = () => { + enableUnifiedPush(mx, upConfigRef.current) + .then((result) => { + lastEndpointRef.current = result.endpoint; + }) + .catch(() => undefined); + }; + + establish(); + + let cancelled = false; + let listener: { unregister: () => Promise | void } | undefined; + void listenForUnifiedPushEndpointChanges((endpoint) => { + if (endpoint === lastEndpointRef.current) return; + lastEndpointRef.current = endpoint; + establish(); + }).then((handle) => { + if (cancelled) void handle.unregister(); + else listener = handle; + }); + + return () => { + cancelled = true; + void listener?.unregister(); + }; + }, [provider, mx]); + useEffect( () => () => { void runtimeRef.current?.dispose();