Skip to content

Commit 04a51ad

Browse files
committed
Fix push notifications on iOS
1 parent 7de8dc2 commit 04a51ad

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

frontend/src/common-components/EnableNotifications.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { Alert, AlertDescription, AlertIcon, Box, Button, HStack, useDisclosure, VStack } from '@chakra-ui/react'
22
import { useConfigContext } from '../api/contexts/config/ConfigContext.tsx'
3+
import { areNotificationsSupported } from '../util/configs/firebase.config.ts'
34

45
export const EnableNotifications = () => {
56
const { isOpen, onClose } = useDisclosure({ defaultIsOpen: true })
67
const config = useConfigContext()
8+
if (!areNotificationsSupported()) return
79
if (Notification.permission !== 'default') return null // we cannot ask again
810

911
const component = config.components.pushnotification

frontend/src/common-components/PushNotificationHandler.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FC, PropsWithChildren, useEffect } from 'react'
2-
import { disableNotifications, getCloudMessaging, initNotifications } from '../util/configs/firebase.config.ts'
2+
import { areNotificationsSupported, disableNotifications, getCloudMessaging, initNotifications } from '../util/configs/firebase.config.ts'
33
import { useConfigContext } from '../api/contexts/config/ConfigContext.tsx'
44
import { Alert, AlertDescription, AlertTitle, Box, Button, CloseButton, useToast } from '@chakra-ui/react'
55
import { useAuthContext } from '../api/contexts/auth/useAuthContext.ts'
@@ -12,9 +12,10 @@ export const PushNotificationHandler: FC<PropsWithChildren> = ({ children }) =>
1212
const toast = useToast()
1313

1414
useEffect(() => {
15+
if (!areNotificationsSupported()) return
1516
if (!authContext.isLoggedIn) return
1617
const shouldShowNotifications = config.components.pushnotification?.notificationsEnabled
17-
const permissionGranted = Notification?.permission === 'granted'
18+
const permissionGranted = Notification.permission === 'granted'
1819
if (shouldShowNotifications && permissionGranted) {
1920
const messaging = getCloudMessaging()
2021
if (messaging === null) return

frontend/src/common-components/footer/Footer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const Footer = () => {
1616
const sponsors = useMemo(() => parseSponsors(component?.sponsorLogoUrls, component?.sponsorAlts, component?.sponsorWebsiteUrls), [config])
1717
const partners = useMemo(() => parseSponsors(component?.partnerLogoUrls, component?.partnerAlts, component?.partnerWebsiteUrls), [config])
1818
if (!component) return null
19-
console.log(component)
19+
2020
const partnersVisible = component?.bmeEnabled || component?.vikEnabled || component?.schonherzEnabled || component?.schdesignEnabled
2121
const topBarVisible = (component?.sponsorsEnabled || partnersVisible) && !component.minimalisticFooter
2222
const transparentNavbar = useColorModeValue(config.components.style.lightFooterTransparent, config.components.style.darkFooterTransparent)

frontend/src/util/configs/firebase.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export function getCloudMessaging(): Messaging | null {
2929
}
3030
}
3131

32+
export function areNotificationsSupported() {
33+
return 'Notification' in window && 'serviceWorker' in navigator && 'PushManager' in window
34+
}
35+
3236
export async function initNotifications(messaging: Messaging, onNotification: (payload: MessagePayload) => void): Promise<void> {
3337
onMessage(messaging, onNotification)
3438
const token = await getMessagingToken(messaging)
@@ -40,6 +44,7 @@ export async function initNotifications(messaging: Messaging, onNotification: (p
4044
export const disableNotifications = () => unregisterServiceWorker()
4145

4246
export async function unsubscribeFromNotifications() {
47+
if (!areNotificationsSupported()) return
4348
if (Notification.permission !== 'granted') return // We cannot obtain a messaging token if the permission is not granted
4449

4550
try {

0 commit comments

Comments
 (0)