Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import type { ClientConfig } from '../../../hooks/useClientConfig';

const debugLog = createDebugLogger('PushNotifications');

export function isPushSupported(): boolean {
return 'serviceWorker' in navigator && 'PushManager' in window;
}

type PushSubscriptionState = [
PushSubscriptionJSON | null,
(subscription: PushSubscription | null) => void,
Expand Down Expand Up @@ -32,12 +36,12 @@ export async function enablePushNotifications(
clientConfig: ClientConfig,
pushSubscriptionAtom: PushSubscriptionState
): Promise<void> {
if (!('serviceWorker' in navigator) || !('PushManager' in window)) {
if (!isPushSupported()) {
debugLog.error(
'notification',
'Push messaging not supported - missing serviceWorker or PushManager'
);
throw new Error('Push messaging is not supported in this browser.');
return;
}
debugLog.info('notification', 'Enabling push notifications');
const [pushSubAtom, setPushSubscription] = pushSubscriptionAtom;
Expand Down Expand Up @@ -178,6 +182,7 @@ export async function togglePusher(
keepEnabledWhenVisible = false
): Promise<void> {
if (usePushNotifications) {
if (!isPushSupported()) return;
if (visible && !keepEnabledWhenVisible) {
await disablePushNotifications(mx, clientConfig, pushSubscriptionAtom);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
requestBrowserNotificationPermission,
enablePushNotifications,
disablePushNotifications,
isPushSupported,
} from './PushNotifications';
import { DeregisterAllPushersSetting } from './DeregisterPushNotifications';

Expand Down Expand Up @@ -106,9 +107,11 @@
const pushSubAtom = useAtom(pushSubscriptionAtom);

const browserPermission = usePermissionState('notifications', getNotificationState());

if (!isPushSupported()) return null;
useEffect(() => {
setIsLoading(false);
}, []);

Check failure on line 114 in src/app/features/settings/notifications/SystemNotification.tsx

View workflow job for this annotation

GitHub Actions / Lint

eslint-plugin-react-hooks(rules-of-hooks)

React Hook "useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render.
const handleRequestPermissionAndEnable = async () => {
setIsLoading(true);
try {
Expand Down
2 changes: 1 addition & 1 deletion src/app/state/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export const defaultSettings: Settings = {
// Push notifications (SW/Sygnal): default on for mobile, opt-in on desktop.
// In-app pill banner: default on for mobile (primary foreground alert), opt-in on desktop.
// System (OS) notifications: desktop-only; hidden and disabled on mobile.
usePushNotifications: mobileOrTablet(),
usePushNotifications: mobileOrTablet() && 'serviceWorker' in navigator && 'PushManager' in window,
useInAppNotifications: mobileOrTablet(),
useSystemNotifications: !mobileOrTablet(),
isNotificationSounds: true,
Expand Down
Loading