diff --git a/app/_layout.tsx b/app/_layout.tsx index 59a75dfd..bf308c78 100644 --- a/app/_layout.tsx +++ b/app/_layout.tsx @@ -294,6 +294,32 @@ function Layout() { checkAndRequestPermissions(); }, []); + const checkStatusAsync = async () => { + if (Platform.isTV) return; + + await BackgroundFetch.getStatusAsync(); + return await TaskManager.isTaskRegisteredAsync( + BACKGROUND_FETCH_TASK_RECENTLY_ADDED + ); + }; + + useEffect(() => { + (async () => { + const isRegistered = await checkStatusAsync(); + + if (settings.recentlyAddedNotifications === false && isRegistered) { + console.log("unregisterBackgroundFetchAsyncRecentlyAdded"); + unregisterBackgroundFetchAsyncRecentlyAdded(); + } else if ( + settings.recentlyAddedNotifications === true && + !isRegistered + ) { + console.log("registerBackgroundFetchAsyncRecentlyAdded"); + registerBackgroundFetchAsyncRecentlyAdded(); + } + })(); + }, [settings.recentlyAddedNotifications]); + useEffect(() => { // If the user has auto rotate enabled, unlock the orientation if (settings.autoRotate === true) { @@ -304,12 +330,6 @@ function Layout() { ScreenOrientation.OrientationLock.PORTRAIT_UP ); } - - if (settings.recentlyAddedNotifications === true) { - registerBackgroundFetchAsyncRecentlyAdded(); - } else { - unregisterBackgroundFetchAsyncRecentlyAdded(); - } }, [settings]); useEffect(() => {