mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 09:01:17 -08:00
Force push notifications on mobile, as in-app notifications are unreliable
This commit is contained in:
1 parent
58e92c22f9
commit
759acbb191
5 files changed
+28
-6
No files matched your search
@@ -144,10 +144,11 @@ export async function togglePusher(
|
||||
clientConfig: ClientConfig,
|
||||
visible: boolean,
|
||||
usePushNotifications: boolean,
|
||||
pushSubscriptionAtom: Atom<PushSubscriptionJSON | null, [PushSubscription | null], void>
|
||||
pushSubscriptionAtom: Atom<PushSubscriptionJSON | null, [PushSubscription | null], void>,
|
||||
keepEnabledWhenVisible = false
|
||||
): Promise<void> {
|
||||
if (usePushNotifications) {
|
||||
if (visible) {
|
||||
if (visible && !keepEnabledWhenVisible) {
|
||||
await disablePushNotifications(mx, clientConfig, pushSubscriptionAtom);
|
||||
} else {
|
||||
await enablePushNotifications(mx, clientConfig, pushSubscriptionAtom);
|
||||
|
||||
@@ -7,11 +7,13 @@ import { useClientConfig } from './useClientConfig';
|
||||
import { useSetting } from '../state/hooks/settings';
|
||||
import { settingsAtom } from '../state/settings';
|
||||
import { pushSubscriptionAtom } from '../state/pushSubscription';
|
||||
import { mobileOrTablet } from '../utils/user-agent';
|
||||
|
||||
export function useAppVisibility(mx: MatrixClient | undefined) {
|
||||
const clientConfig = useClientConfig();
|
||||
const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications');
|
||||
const pushSubAtom = useAtom(pushSubscriptionAtom);
|
||||
const isMobile = mobileOrTablet();
|
||||
|
||||
useEffect(() => {
|
||||
const handleVisibilityChange = () => {
|
||||
@@ -33,7 +35,7 @@ export function useAppVisibility(mx: MatrixClient | undefined) {
|
||||
if (!mx) return;
|
||||
|
||||
const handleVisibilityForNotifications = (isVisible: boolean) => {
|
||||
togglePusher(mx, clientConfig, isVisible, usePushNotifications, pushSubAtom);
|
||||
togglePusher(mx, clientConfig, isVisible, usePushNotifications, pushSubAtom, isMobile);
|
||||
};
|
||||
|
||||
appEvents.onVisibilityChange = handleVisibilityForNotifications;
|
||||
@@ -41,5 +43,5 @@ export function useAppVisibility(mx: MatrixClient | undefined) {
|
||||
return () => {
|
||||
appEvents.onVisibilityChange = null;
|
||||
};
|
||||
}, [mx, clientConfig, usePushNotifications, pushSubAtom]);
|
||||
}, [mx, clientConfig, usePushNotifications, pushSubAtom, isMobile]);
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import LogoSVG from '$public/res/svg/cinny.svg';
|
||||
import { nicknamesAtom } from '$state/nicknames';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import { buildRoomMessageNotification } from '$appUtils/notificationStyle';
|
||||
import { mobileOrTablet } from '$appUtils/user-agent';
|
||||
|
||||
const log = createLogger('BackgroundNotifications');
|
||||
|
||||
@@ -67,7 +68,9 @@ export function BackgroundNotifications() {
|
||||
const activeSessionId = useAtomValue(activeSessionIdAtom);
|
||||
const setActiveSessionId = useSetAtom(activeSessionIdAtom);
|
||||
const [showNotifications] = useSetting(settingsAtom, 'useInAppNotifications');
|
||||
const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications');
|
||||
const [notificationSound] = useSetting(settingsAtom, 'isNotificationSounds');
|
||||
const forcePushOnMobile = usePushNotifications && mobileOrTablet();
|
||||
const activeMx = useMatrixClient();
|
||||
const nicknames = useAtomValue(nicknamesAtom);
|
||||
const nicknamesRef = useRef(nicknames);
|
||||
@@ -96,6 +99,7 @@ export function BackgroundNotifications() {
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (forcePushOnMobile) return undefined;
|
||||
if (!showNotifications) return undefined;
|
||||
|
||||
const { current } = clientsRef;
|
||||
@@ -231,6 +235,7 @@ export function BackgroundNotifications() {
|
||||
};
|
||||
}, [
|
||||
inactiveSessions,
|
||||
forcePushOnMobile,
|
||||
showNotifications,
|
||||
notificationSound,
|
||||
activeMx,
|
||||
|
||||
@@ -32,6 +32,7 @@ import { registrationAtom } from '$state/serviceWorkerRegistration';
|
||||
import { BackgroundNotifications } from './BackgroundNotifications';
|
||||
import { activeSessionIdAtom, pendingNotificationAtom } from '$state/sessions';
|
||||
import { buildRoomMessageNotification } from '$appUtils/notificationStyle';
|
||||
import { mobileOrTablet } from '$appUtils/user-agent';
|
||||
|
||||
function SystemEmojiFeature() {
|
||||
const [twitterEmoji] = useSetting(settingsAtom, 'twitterEmoji');
|
||||
@@ -109,6 +110,7 @@ function InviteNotifications() {
|
||||
const [showNotifications] = useSetting(settingsAtom, 'useInAppNotifications');
|
||||
const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications');
|
||||
const [notificationSound] = useSetting(settingsAtom, 'isNotificationSounds');
|
||||
const forcePushOnMobile = usePushNotifications && mobileOrTablet();
|
||||
|
||||
const notify = useCallback(
|
||||
(count: number) => {
|
||||
@@ -133,6 +135,7 @@ function InviteNotifications() {
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (forcePushOnMobile) return;
|
||||
if (usePushNotifications && document.visibilityState !== "visible") return;
|
||||
if (invites.length > perviousInviteLen && mx.getSyncState() === 'SYNCING') {
|
||||
if (showNotifications && notificationPermission('granted')) {
|
||||
@@ -149,6 +152,7 @@ function InviteNotifications() {
|
||||
perviousInviteLen,
|
||||
showNotifications,
|
||||
usePushNotifications,
|
||||
forcePushOnMobile,
|
||||
notificationSound,
|
||||
notify,
|
||||
playSound
|
||||
@@ -171,6 +175,7 @@ function MessageNotifications() {
|
||||
const [showNotifications] = useSetting(settingsAtom, 'useInAppNotifications');
|
||||
const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications');
|
||||
const [notificationSound] = useSetting(settingsAtom, 'isNotificationSounds');
|
||||
const forcePushOnMobile = usePushNotifications && mobileOrTablet();
|
||||
const nicknames = useAtomValue(nicknamesAtom);
|
||||
const nicknamesRef = useRef(nicknames);
|
||||
nicknamesRef.current = nicknames;
|
||||
@@ -230,6 +235,7 @@ function MessageNotifications() {
|
||||
removed,
|
||||
data
|
||||
) => {
|
||||
if (forcePushOnMobile) return;
|
||||
if (mx.getSyncState() !== 'SYNCING') return;
|
||||
if (usePushNotifications && document.visibilityState !== "visible") return;
|
||||
if (document.hasFocus() && (selectedRoomId === room?.roomId || notificationSelected)) return;
|
||||
@@ -290,6 +296,7 @@ function MessageNotifications() {
|
||||
notificationSelected,
|
||||
showNotifications,
|
||||
usePushNotifications,
|
||||
forcePushOnMobile,
|
||||
playSound,
|
||||
notify,
|
||||
selectedRoomId,
|
||||
@@ -380,19 +387,22 @@ function HandleNotificationClick() {
|
||||
|
||||
function SyncNotificationSettingsWithServiceWorker() {
|
||||
const [notificationSound] = useSetting(settingsAtom, 'isNotificationSounds');
|
||||
const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications');
|
||||
|
||||
useEffect(() => {
|
||||
if (!('serviceWorker' in navigator)) return;
|
||||
const preferPushOnMobile = usePushNotifications && mobileOrTablet();
|
||||
const payload = {
|
||||
type: 'setNotificationSettings' as const,
|
||||
notificationSoundEnabled: notificationSound,
|
||||
preferPushOnMobile,
|
||||
};
|
||||
|
||||
navigator.serviceWorker.controller?.postMessage(payload);
|
||||
void navigator.serviceWorker.ready.then((registration) => {
|
||||
registration.active?.postMessage(payload);
|
||||
});
|
||||
}, [notificationSound]);
|
||||
}, [notificationSound, usePushNotifications]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ export type {};
|
||||
declare const self: ServiceWorkerGlobalScope;
|
||||
|
||||
let notificationSoundEnabled = true;
|
||||
let preferPushOnMobile = false;
|
||||
const { handlePushNotificationPushData } = usePushNotifications(self, () => ({
|
||||
notificationSoundEnabled,
|
||||
}));
|
||||
@@ -167,6 +168,9 @@ self.addEventListener('message', (event: ExtendableMessageEvent) => {
|
||||
if (typeof (data as { notificationSoundEnabled?: unknown }).notificationSoundEnabled === 'boolean') {
|
||||
notificationSoundEnabled = (data as { notificationSoundEnabled: boolean }).notificationSoundEnabled;
|
||||
}
|
||||
if (typeof (data as { preferPushOnMobile?: unknown }).preferPushOnMobile === 'boolean') {
|
||||
preferPushOnMobile = (data as { preferPushOnMobile: boolean }).preferPushOnMobile;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -271,7 +275,7 @@ const onPushNotification = async (event: PushEvent) => {
|
||||
|
||||
const clients = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
|
||||
const hasVisibleClient = clients.some((client) => client.visibilityState === 'visible');
|
||||
if (hasVisibleClient) {
|
||||
if (hasVisibleClient && !preferPushOnMobile) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user