react-native-background-fetch, 'minimumFetchInterval' does not work properly

111 views Asked by At

I am developing an app with react-native and react-native-background-fetch. I have 3 problems with this. I have set up all the configurations according to the setup guides.(Android / iOS both)

First of all, belows are my code.

index.js

import BackgroundFetch from 'react-native-background-fetch';

let MyHeadlessTask = async event => {
  // Get task id from event {}:
  let taskId = event.taskId;
  let isTimeout = event.timeout; // <-- true when your background-time has expired.
  if (isTimeout) {
    // This task has exceeded its allowed running-time.
    // You must stop what you're doing immediately finish(taskId)
    console.log('[BackgroundFetch] Headless TIMEOUT:', taskId);
    BackgroundFetch.finish(taskId);
    return;
  }
  console.log('[BackgroundFetch HeadlessTask] start: ', taskId);

  // Perform an example HTTP request.
  // Important:  await asychronous tasks when using HeadlessJS.
  const {saveLocation} = require('./App');
  const location = await saveLocation('auto');
  console.log('[BackgroundFetch HeadlessTask] location: ', location);

  // Required:  Signal to native code that your task is complete.
  // If you don't do this, your app could be terminated and/or assigned
  // battery-blame for consuming too much time in background.
  BackgroundFetch.finish(taskId);
};

// Register your BackgroundFetch HeadlessTask
BackgroundFetch.registerHeadlessTask(MyHeadlessTask);

App.js

useEffect(() => {
    if (!dbUser) return;
    initBackgroundFetch();
  }, [dbUser]);

  const initBackgroundFetch = async () => {
    try {
      const onEvent = async taskId => {
        console.log('BackgroundFetch event triggered. Task ID:', taskId);
        console.log(new Date());

        await saveLocation('auto');
        BackgroundFetch.finish(taskId);
      };

      const onTimeout = async taskId => {
        console.warn(
          'BackgroundFetch TIMEOUT event triggered. Task ID:',
          taskId,
        );
        BackgroundFetch.finish(taskId);
      };

      let status = await BackgroundFetch.configure(
        {
          minimumFetchInterval: 30, // Minimum fetch interval in minutes
          stopOnTerminate: false,
          startOnBoot: true,
          enableHeadless: true,
        },
        onEvent,
        onTimeout,
      );

      console.log('BackgroundFetch configured successfully.');
    } catch (err) {
      console.error(err);
    }
  };
  1. The 'minimumFetchInterval' is not working as expected. Regardless of whether it is set to 1 or 30, the automatic execution interval varies inconsistently.

  2. The app automatically runs when it is closed (not in the background), although the execution interval is not as per the configured settings. However, an error occurs stating that the executing function is not defined.

 ERROR  [TypeError: saveLocation is not a function (it is undefined)]
  1. It's not working on iOS.

If anyone has solutions, I'd greatly appreciate detailed explanations. Thanks.

1

There are 1 answers

0
gshoanganh On

my solution:

let status = await BackgroundFetch.configure({  
  minimumFetchInterval: 15
}, async (taskId) => {  
  console.log("[BackgroundFetch] taskId:", taskId);
  BackgroundFetch.finish(taskId);
}, async (taskId) => {  // <-- NEW:  task-timeout callback.
  // This task has exceeded its allowed running-time.
  // You must stop what you're doing immediately finish(taskId)
  //
  console.log("[BackgroundFetch] TIMEOUT taskId", taskId);
  BackgroundFetch.finish(taskId);
});