Suhail Misbah
Suhail Misbah

Reputation: 11

background fetch not working with accelerometer in react native expo

im using below code to fetch the accelerometer values when my app is running in the background i have added the below code ouside of the app component

TaskManager.defineTask('firstTask', async ({ data, error }) => {
  if (error) {
    // Error occurred - check `error.message` for more details.
    return; 
  }
  if (data) {
    //below 3 lines of code is the accelerometer is for the accelerometer tracking
    Accelerometer.addListener(accelerometerData => {
      console.log(accelerometerData);
      Accelerometer.setUpdateInterval(10000);
     })
    console.log('locations')
  }
});

the below code is inside of the app component

   useEffect(() => {
    RegisterBackgroundTask();
   }, []);

  const RegisterBackgroundTask = async () => {
    try {
      await BackgroundFetch.registerTaskAsync('firstTask', {
        minimumInterval: 5, // seconds,
      })

      console.log("Task registered")
    } catch (err) {
      console.log("Task Register failed:", err)
    }
  }

when i console log something the background app works meaning when i close my app the console.log will display on the console but when i add the acceleromenter nothing happens

Any idea on what i should do

Upvotes: 2

Views: 430

Answers (0)

Related Questions