transistorsoft / react-native-background-fetch

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

The task stops running after 30 minutes #473

Closed ifgabriel closed 5 months ago

ifgabriel commented 6 months ago

Your Environment:

Code

  const initBackground = async () => {
    const onEvent = async (taskId: string) => {
      if (taskId === 'UPDATE_COORDINATES') {
        console.log('[BackgroundFetch] task: ', taskId);
      }
    };

    const onTimeout = async (taskId: string) => {
      console.warn('[BackgroundFetch] TIMEOUT task: ', taskId);
      BackgroundFetch.finish(taskId);
    };

    const status = await BackgroundFetch.configure(
      {
        minimumFetchInterval: 0,
        forceAlarmManager: true,
        requiredNetworkType: 0,
        stopOnTerminate: false,
      },
      onEvent,
      onTimeout
    );

    console.log('[BackgroundFetch] configure status: ', status);
  };

  BackgroundFetch.scheduleTask({
    taskId: 'UPDATE_COORDINATES',
    forceAlarmManager: true,
    enableHeadless: true,
    delay: 1,
    periodic: true,
  });

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

Expected Behavior

It should run without interruptions

Actual Behavior

After 30 minutes the task stops running

Steps to Reproduce

  1. npx react-native run-android
  2. access the screen that starts the background fetch
  3. exit the application

Context

I'm trying to update device coordinates even with the app running in the background

Debug logs

[Wed Nov 01 2023 10:22:26.640] LOG [BackgroundFetch] configure status: 2 [Wed Nov 01 2023 10:23:24.228] LOG [BackgroundFetch] task: UPDATE_COORDINATES [Wed Nov 01 2023 10:24:24.228] LOG [BackgroundFetch] task: UPDATE_COORDINATES [Wed Nov 01 2023 10:25:24.227] LOG [BackgroundFetch] task: UPDATE_COORDINATES ............

[Wed Nov 01 2023 10:50:24.228] LOG [BackgroundFetch] task: UPDATE_COORDINATES [Wed Nov 01 2023 10:51:24.230] LOG [BackgroundFetch] task: UPDATE_COORDINATES

christocracy commented 6 months ago

exit the application

What code are you expecting to execute after terminate?

ifgabriel commented 6 months ago

I create a task and after leaving the application, it stays in the background, it must execute a task every 1 minute (the minimum time) and this should be carried out in a loop until I finish a task, right?

christocracy commented 6 months ago

Paste the specific code that you expect to execute after app terminate.

ifgabriel commented 6 months ago

My code is just the above, what do you mean by code after finishing? I create an activity with periodic true, shouldn't it continue until I finish it?

christocracy commented 6 months ago

After terminating an Android app, none of your code above is going to run..

Read the API docs enableHeadless

christocracy commented 6 months ago

Also read the Debugging section.

ifgabriel commented 6 months ago

The application is not terminated, just placed in the background

christocracy commented 6 months ago

It's up to the OS to fire events. See https://dontkillmyapp.com

If a task stops running, it's the OS doing that to preserve the device battery and there's nothing the plug-in can do to stop that.

ifgabriel commented 6 months ago

I don't know if you can help me. But I need my user (restricted use) to send me their location every 20 seconds, I capture the location with Google's Gelocalition and send it to an API in my backend. However, I am facing the problem of running background tasks on Android. And I'm wondering how this location service works so well in the Uber, Ifood, WhatsApp apps. Can you tell me if they use another approach?

christocracy commented 6 months ago

You will never succeed attempting to get the location every 20s, particularly with the most recent Android 14.

The best you'll get is periodic requests about every 15 minutes.

Your best hope is to try using my react-native-background-geolocation (non-free): https://www.transistorsoft.com/shop/products/react-native-background-geolocation

This library tracks location when the device is determined to be moving.