transistorsoft / react-native-background-fetch

Periodic callbacks in the background for both IOS and Android
MIT License
1.43k stars 191 forks source link

'minimumFetchInterval' does not work properly #483

Closed koreahn closed 2 weeks ago

koreahn commented 3 months ago

Your Environment

Expected Behavior

Actual Behavior

I have set up all the configurations according to the setup guides.(Android / iOS both)

  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)]
  3. It's not working on iOS.

Steps to Reproduce

1. 2. 4. 5.

Context

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);


2. 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);
}

};



## Debug logs
<!-- 
include iOS / Android logs
- iOS: XCode logs, 
- Android: $ adb logcat -->
christocracy commented 3 months ago

varies inconsistently.

Yes, that's to be expected. The OS is interested in preserving device battery. It decides when it's ready to fire an event.

koreahn commented 3 months ago

varies inconsistently.

Yes, that's to be expected. The OS is interested in preserving device battery. It decides when it's ready to fire an event.

Thank you for your reply. Can you tell me why it doesn't work on iOS? I have set up all the configurations according to the setup guides.

christocracy commented 3 months ago

See readme to learn how to simulate events in iOS. If simulated events work, that's all you need to do to know that it works. After that, you must wait several days before regular events beginning.

github-actions[bot] commented 4 weeks ago

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

github-actions[bot] commented 2 weeks ago

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