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

[Getting this warning] "No task registered for key BackgroundFetch" #503

Closed
kopposh opened this issue May 28, 2024 · 5 comments
Closed

[Getting this warning] "No task registered for key BackgroundFetch" #503

kopposh opened this issue May 28, 2024 · 5 comments
Labels

Comments

@kopposh
Copy link

kopposh commented May 28, 2024

I have registered a headless task in my index.js to run when app is terminated on android along with BackgroundFetch.configure(code shown below) and receiving the below warning message.

WARN No task registered for key BackgroundFetch

useEffect(()=>{
    BackgroundFetch.registerHeadlessTask(BGHeadlessTask);
},[])

const BGHeadlessTask = async (event: { taskId: any; timeout: any }) => {
    let taskId = event.taskId;
    let isTimeout = event.timeout;
    if (isTimeout) {
      BackgroundFetch.finish(taskId);
      return;
    }
    try {
      const location = await BackgroundGeolocation.getCurrentPosition({
         timeout: 30,
          maximumAge: 1000,
          desiredAccuracy: 1,
          samples: 1,
          persist: false,
      });
      if (location && location?.coords) {
        await insertLocation({
          latitude: `${location?.coords?.latitude}`,
          longitude: `${location?.coords?.longitude}`,
          source: LOCATION_BG_TRACKING,
        });
      }
      BackgroundFetch.finish(taskId);
    } catch (err) {
      BackgroundFetch.finish(taskId);
    }
  }
const status = await BackgroundFetch.configure(
{
  minimumFetchInterval: 20,
 enableHeadless: true,
 stopOnTerminate: false,
},
async (taskId) => {
try {
   const location = await BackgroundGeolocation.getCurrentPosition({
         timeout: 30,
          maximumAge: 1000,
          desiredAccuracy: 1,
          samples: 1,
          persist: false,
      });
        insertLocation({
        latitude: ${location?.coords?.latitude},
        longitude: ${location?.coords?.longitude},
        });
}
} catch (err) {
console.log(
"BackgroundGeolocation.initBackgroundFetch error", err
);
}
    BackgroundFetch.finish(taskId);
},
async (taskId) => {
    BackgroundFetch.finish(taskId);
}
);

Your Environment

Plugin version: "react-native-background-geolocation": "^4.14.6",
Platform: iOS and Android
OS version: Android >=9, iOS >= 15
Device manufacturer / model: Tested on iPhone 15 pro, Mi A1
React Native version (react-native -v): Expo 50.0.18

Expected Behavior

Expecting the app to save the current location to local storage even when app is terminated.

Actual Behavior

Giving "No task registered for key BackgroundFetch" warning when app is terminated

Steps to Reproduce

Context

Debug logs

screenshot when app is terminated for a while and opened again after receiving the warning:
Screenshot 2024-05-28 at 1 27 52 PM

@christocracy
Copy link
Member

christocracy commented May 28, 2024

Why are you registering a headlessTask within a useEffect block? There is nowhere that instructs that.

useEffect(()=>{
BackgroundFetch.registerHeadlessTask(BGHeadlessTask);
},[])

the docs for Config.enableHeadless instruct that a headlessTask is to be added directly to the index.js.

a warning showing TaskTimeout means that a task has timed out, whereBackgroundFetch.finish(taskId) is not being executed in time.

@kopposh
Copy link
Author

kopposh commented May 28, 2024

Thank you for quick response. I will make the necessary changes to remove out of useEffect. and Does "No task registered for key BackgroundFetch" means task timeout?

@christocracy
Copy link
Member

There is no possible way that a headlessTask can operate correctly when registered within a useEffect block. All other queries about any warnings received are moot.

Copy link

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Jun 28, 2024
Copy link

This issue was closed because it has been inactive for 14 days since being marked as stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants