transistorsoft / react-native-background-fetch

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

react-native-background-fetch not working [ios] and Simulate Background Fetch is disabled in xcode #278

Closed rsiuli001 closed 3 years ago

rsiuli001 commented 3 years ago

Your Environment

try {

  BackgroundFetch.configure(

    {
      minimumFetchInterval: 240,
      requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY
    },

    async taskId => {

      let currentDate = new Date();

      let lastSync =
        currentDate.toLocaleDateString() +
        '@' + currentDate.toTimeString().slice(0, 8);

      storeDataLocally('bgTask', { lastSync: lastSync });

      Sentry.captureMessage(
        `[BackgroundFetch] executed successfully: ${lastSync}`
      );
      BackgroundFetch.finish(taskId);
    }
  );
} catch (err) {
  Sentry.captureException(err);
}

Expected Behavior

It should write the current timestamp of its execution to local storage and log it in Sentry in every 4 hours interval. And if it works successfully, Api call will be added in the execution block.

Actual Behavior

It logged only six times in sentry for for around 10 users in last 11 days.

Steps to Reproduce

I'm calling the background fetch in useEffect() in a context api.

useEffect(() => { init(); });

Context

I am trying to call an API in background and store it in realm database (or local storage in JSON format) and the last sync timestamp is also stored in local storage to show it to user. Right now i'm only testing with timestamp. If it works well i will add the api call.

Debug logs

christocracy commented 3 years ago

You need to actually run your app in XCode by clicking the ( > ) button to Simulate Background Fetch

rsiuli001 commented 3 years ago

I have connected my iPad and ran the app from xcode multiple times, but still no luck.

christocracy commented 3 years ago

There's no possible way that Debug->Simulate Background Fetch can be disabled in XCode with the app launched from XCode.

It should write the current timestamp of its execution to local storage and log it in Sentry in every 4 hours interval

No, you will never get exactly 4 hour intervals. The frequency of events is purely up to Apple's secret machine-learning algorithm based upon the user's behaviour with your app.

rsiuli001 commented 3 years ago

So, for fetch events to work, the app needs to be minimized not terminated?

christocracy commented 3 years ago

the app needs to be minimized not terminated?

That is correct. Once you terminate the app, there are no more fetch events and certainly no ability to Simulate Background Fetch.

mikehardy commented 3 years ago

There is a lot of documentation about "how to wake up an iOS app" or "how to start an iOS app that's terminated etc" on the internet. It's really something you have to go to school on, so you can learn the real possibilities of the platform and it's limitations.

As far as I know, if your app is not loved / used by the users, fetch is a dead-end as iOS won't run your fetch any more. After that you have either motion detection / significant locations / GPS (used by the companion geolocation package here, but Apple will reject you unless you really need use it to improve the user's life!), or cloud messaging, Sending cloud messages in a way that they will actually wake up the app comes with it's own set of possibilities and limitations which (as far as I can tell) about 1% of the developer population actually understands.

It's just not easy, and maybe not even possible, and that's the iOS platform.

christocracy commented 3 years ago

@mikehardy is right. I try to remember to launch my /example app daily (available in this repo) However, this weekend I neglected it (06-13, 06-14): no daily "love", no events.

rsiuli001 commented 3 years ago

Thank you very much @mikehardy @christocracy I will definitely test the app reducing the timer to 15 mins and minimizing the app. And will share the details.

thekrol01 commented 3 years ago

I have read the comments but I am a little confused how to test the ' plain' background fetch with the new 3.x API. I test the older version on a physical device with Xcode which works fine. if i am correct you can only test the background fetch (3.X API) on a physical device. But you can test custom tasks in the simulator as explained in the debugging section of the docs?

christocracy commented 3 years ago

But you can test custom tasks in the simulator as explained in the debugging section of the docs?

No. You cannot test any of the new iOS BGTaskScheduler API in the simulator currently. Hopefully Apple makes this work at some point.

thekrol01 commented 3 years ago

Ok, thanks!

seki-ne commented 3 years ago

hi, i have the same issue in the native app with swift. is it a bug in iOS 13? or how can i fix it?

christocracy commented 3 years ago

Is “what” a bug?

seki-ne commented 3 years ago

@christocracy The method performFetchWithCompletionHandler is not triggered the day when I do not open the app

func application(_: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
}
christocracy commented 3 years ago

That's correct. As noted above

If the user doesn't open the app daily, no more fetch events.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and I will leave this open.

stale[bot] commented 3 years ago

Closing this issue after a prolonged period of inactivity. Fell free to reopen this issue, if this still affecting you.

hmunoz commented 3 years ago

I have the same problem.
The functionality of generating events by time intervals does not work for me. Which is strange to me, because in motion it works correctly.

In order to close the topic, is it or is it not possible to generate reports every X time?