Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix blank screen after notification request #918

Merged
merged 2 commits into from
Sep 1, 2023
Merged
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
18 changes: 11 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { CozyProvider, useClient } from 'cozy-client'
import { NativeIntentProvider } from 'cozy-intent'

import { RootNavigator } from '/AppRouter'
import { useSecureBackgroundSplashScreen } from '/hooks/useSplashScreen'
import * as RootNavigation from '/libs/RootNavigation'
import NetStatusBoundary from '/libs/services/NetStatusBoundary'
import { IconChangedModal } from '/libs/icon/IconChangedModal'
Expand All @@ -36,6 +35,8 @@ import { useNetService } from '/libs/services/NetService'
import { withSentry } from '/libs/monitoring/Sentry'
import { ThemeProvider } from '/app/theme/ThemeProvider'
import { useInitI18n } from '/locales/useInitI18n'
import { SecureBackgroundSplashScreenWrapper } from '/app/theme/SecureBackgroundSplashScreenWrapper'
import { PermissionsChecker } from '/app/domain/nativePermissions/components/PermissionsChecker'

// Polyfill needed for cozy-client connection
if (!global.btoa) {
Expand All @@ -57,7 +58,6 @@ const App = ({ setClient }) => {
const { initialRoute, isLoading } = useAppBootstrap(client)

useGlobalAppState()
useSecureBackgroundSplashScreen()
useCookieResyncOnResume()
useNotifications()
useCozyEnvironmentOverride()
Expand Down Expand Up @@ -156,11 +156,15 @@ const Wrapper = () => {
<HttpServerProvider>
<HomeStateProvider>
<SplashScreenProvider>
<NetStatusBoundary>
<ThemeProvider>
<WrappedApp />
</ThemeProvider>
</NetStatusBoundary>
<SecureBackgroundSplashScreenWrapper>
<NetStatusBoundary>
<ThemeProvider>
<PermissionsChecker>
<WrappedApp />
</PermissionsChecker>
</ThemeProvider>
</NetStatusBoundary>
</SecureBackgroundSplashScreenWrapper>
</SplashScreenProvider>
</HomeStateProvider>
</HttpServerProvider>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useState, useEffect } from 'react'

import { requestNotifications } from '/app/domain/nativePermissions'

interface PermissionsCheckerProps {
children: JSX.Element
}

export const PermissionsChecker = ({
children
}: PermissionsCheckerProps): JSX.Element | null => {
const [notificationPermissionAnswered, setNotificationPermissionAnswered] =
useState(false)

useEffect(() => {
const askNotificationPermission = async (): Promise<void> => {
await requestNotifications()
setNotificationPermissionAnswered(true)
}

void askNotificationPermission()
})

if (notificationPermissionAnswered) {
return children
}

return null
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
check,
request,
checkNotifications as RNPCheckNotifications,
requestNotifications as RNPRequestNotifications,
PermissionStatus,
RESULTS
Expand All @@ -24,6 +25,12 @@ export const requestNativePermission = async (
return formatResult(result)
}

export const checkNotifications = async (): Promise<NativePermissionStatus> => {
const result = await RNPCheckNotifications()

return formatResult(result.status)
}

export const requestNotifications =
async (): Promise<NativePermissionStatus> => {
const result = await RNPRequestNotifications(['alert'])
Expand Down
13 changes: 13 additions & 0 deletions src/app/theme/SecureBackgroundSplashScreenWrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useSecureBackgroundSplashScreen } from '/hooks/useSplashScreen'

interface SecureBackgroundSplashScreenWrapperProps {
children: JSX.Element
}

export const SecureBackgroundSplashScreenWrapper = ({
children
}: SecureBackgroundSplashScreenWrapperProps): JSX.Element => {
useSecureBackgroundSplashScreen()

return children
}
4 changes: 2 additions & 2 deletions src/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
handleInitialNotification,
handleNotificationOpening
} from '/libs/notifications/notifications'
import { requestNotifications } from '/app/domain/nativePermissions'
import { checkNotifications } from '/app/domain/nativePermissions'

export const useNotifications = (): void => {
const client = useClient()
Expand All @@ -20,7 +20,7 @@ export const useNotifications = (): void => {
const initializeNotifications = async (): Promise<void> => {
if (!client) return

const permission = await requestNotifications()
const permission = await checkNotifications()

if (permission.granted) {
setAreNotificationsEnabled(true)
Expand Down
Loading