expo: BackgroundFetch not running
Environment
Expo CLI 2.11.1 environment info:
System:
OS: macOS 10.14.3
Shell: 5.3 - /bin/zsh
Binaries:
Node: 10.6.0 - /usr/local/bin/node
Yarn: 1.12.3 - /usr/local/bin/yarn
npm: 6.8.0 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
IDEs:
Xcode: 10.1/10B61 - /usr/bin/xcodebuild
npmGlobalPackages:
expo-cli: 2.11.1
app’s target - iOS, Standalone
Steps to Reproduce
- Allow background fetch in app.json
"ios": {
"infoPlist": {
"UIBackgroundModes": [
"location",
"fetch"
]
- define a task in App.js
defineHealthTask = () => {
TaskManager.defineTask(taskName, async () => {
try {
await UserRemoteLogService.logWithTaskType("STEPS_TASK_RUNNING");
return BackgroundFetch.Result.NewData;
} catch (error) {
return BackgroundFetch.Result.Failed;
}
});
};
- register task at some point of the apps running
await UserRemoteLogService.logWithTaskType("STEPS_TASK_REGISTRATION_REQUESTED");
const pedometerAvailable = await Pedometer.isAvailableAsync();
const backGroundFetchStatus = await BackgroundFetch.getStatusAsync();
if (pedometerAvailable && BackgroundFetch.Status.Available === backGroundFetchStatus) {
await BackgroundFetch.registerTaskAsync(taskName, {
userBusinessId: user.businessId
});
UserRemoteLogService.logWithTaskType("STEPS_TASK_REGISTERED");
}
- set minimum interval for task in seconds
BackgroundFetch.setMinimumIntervalAsync(60);
Expected Behavior
App should periodically call my server to log that the background task is running
Actual Behavior
Never see the background task run, although i do see the logs that show the background task was registered. The logging service i use has been used to show when location background task has run so this logging service should log if the task runs
My main question is has anyone seen background fetch run? if so have i just configured it wrong?
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 97 (27 by maintainers)
Commits related to this issue
- Fix crash in HeadlessAppLoader after the activity is killed devSupportManager can be null. This is evident both from my experience and from similar code in: https://github.com/expo/expo/blob/0ec9e78... — committed to tasn/expo by tasn 4 years ago
- [task-manager] Fix crash in HeadlessAppLoader after the activity is killed (#6879) devSupportManager can be null. This is evident both from my experience and from similar code in: https://github.com... — committed to expo/expo by tasn 4 years ago
- [task-manager] Fix crash in HeadlessAppLoader after the activity is killed (#6879) devSupportManager can be null. This is evident both from my experience and from similar code in: https://github.com... — committed to expo/expo by tasn 4 years ago
- Prevent invalid dates from throwing an error on Android for expo-contacts (#6694) * Prevent invalid dates from throwing an error on Android fix #4757 * parse as many dates as possible * Adde... — committed to expo/expo by EvanBacon 4 years ago
- Fixed the code according to https://github.com/expo/expo/issues/3582 — committed to maaaashin324/wash-your-hands by maaaashin324 4 years ago
- Upgraded Expo SDK to version 37 (#30) * Upgraded Expo SDK 37 * Downgraded @react-native-community/masked-view * Fixed the code according to https://github.com/expo/expo/issues/3582 * Splitte... — committed to maaaashin324/wash-your-hands by maaaashin324 4 years ago
This is really frustrating after I tried all the code above and checked expo docs for over 10 times at least still couldn’t make it work on my device nor the simulator.
If expo team could provide a snack or an example of
TaskManagerorBackgroundFetch, it will save a ton of time for me.There is example for virtually every module we provide available within our repository. Here’s one for BackgroundFetch. There might seem obsolete sometimes (no hooks and so on) but we test it regularly so they should, most of the time, show each module works and should be used.
We are having it on our map. Exact timeline is difficult to predict, I am afraid. It won’t, however, fix most pressing issue for you, which is executing tasks even after app is being swiped from recents. This is, and probably will be, system limitation.
As of now, sine BackgroundFetch seems to be working with system limitations, let me close this issue for now. For maintaining order, please report feature request if you wish to extend background capabilities for expo.
Hello, I have the same problem, the global task was defined but it is not executed in the interval, is it possible that someone can share functional code ?, since the expo documentation does not define anything.
It took me a long while to understand what was my problem. The expo documentation is a bit misleading. According to Apple you MUST call
BackgroundFetch.setMinimumIntervalAsyncin order for the job to be scheduled.So, you have to
TaskManager.defineTask.BackgroundFetch. If not, register.BackgroundFetch.setMinimumIntervalAsync.Be aware that an emulator does not run your task, that happens only on real devices.
The following worked for me:
Is there any update on this?
I seem to have gotten it to work now, but the backgroundfetch only runs once or day (if at all) even though it’s set to run every half an hour.
Asking people to always allow location tracking doesn’t seem to be a viable option to me, since my app has nothing to do with a users location at all.
Is this really how it’s supposed to work? If that’s really the case, it’s seems to me to be a pretty useless API?!
@mczernek 👍 Upvote for more reliable background tasks via BGTaskScheduler
BackgroundFetch doesn’t work at all for me on the simulator, in expo go, or in the compiled app. The task is defined in the list of tasks, it’s properly started, and the background fetch never fires - not one time. It fails completely silently with no explanation as to why.
I’ve abandoned expo as a framework because of the lack of support for this feature and because of how poorly they’ve handled this matter. I strongly recommend that others do the same, before they get burned as well. This framework is extremely unreliable and the support for it is nonexistent.
@tsapeta will investigate this after he wraps up his current project
+1 on needed this to be more reliable. Having the same issues many have described above after checking many sources of documentation and many forums. Have been developing an app for months that is basically done besides needing this feature to work, ASAP.
re: 2. - there is no need to check if background fetch task is already registered - if you try to register it again, then it just overwrites the previously registered task, which is fine 👌 Except that, these steps look good.
I’ll try to check it out soon, but it seems weird to me as there is no connection between location and background fetch. They are completely independent features. My assumption is that, if you turn on background location then your app is actually always active, so the background fetch can occur in the time you set by
setMinimumIntervalAsync. However, if the app is not active, then it strongly depends on the operating system and how you use the application. Apple has its own algorithm which defines how often the background fetch should trigger, based on your own usage of the app. If you use it a lot, then it will fetch as often as possible, but if you use it like at 4pm every day, the background fetch should trigger just before, so your data is updated when you launch it. So imho you shouldn’t worry about the task not being done on time - Apple knows better when to call it 😅Would help if you could give us a snack or toy repo demonstrating the issue if changing the call context doesn’t resolve it
Was a solution ever found for this problem? Or is the only way to get BackgroundFetch to work on iOS to use the hacky Location.startLocationUpdatesAsync() work around? The problem with this fix is that it runs the task in the foreground as well as the background, which causes new issues.
@juancardlm This does work… thank you for your reply! But I am trying to get background fetch work without using expo Location. My app does not need Location so do not want to ask permission for that from the user.
For me it’s working like this: I used the @kpheasey and @mskg comments
./src/services/tasks.ts
./App.tsx
Ah ok that makes sense… the problem is with the last 2 cases, i.e. background fetch while the app is not running. (it works when the app is backgrounded) The bug seems to be related to the way the app gets loaded when it’s triggered by the task service which causes the app to crash and the task to get removed (see #6290 for more details)
BackgroundFetch not working with me. I use expo client test it. Is it only working in standalone app?
If your app requires background location, the best solution is to eject so you can use native modules. @mauron85/react-native-background-geolocation or headless JS
@srikanthkh I haven’t tried it yet as it doesn’t provide for my need to work even on app termination
I can not get background fetch to work on iOS(when app is in background or not does not matter). I tried using the code from the comments but nothing seem to work. I was able to get the android version working with the same code though. Tried using expo 39 and 40 with all latest packages.
Was anyone successful in getting background fetch to work without Location in iOS?? Please share code. Thanks for your help in advance.
How are you retrieving the data from the location updates? In the docs it said to use TaskManager and define a task but that is not working for me.
To make background tasks work, I used the foregroundService option for the startLocationUpdatesAsync method as it says in the docs:
Use this option to put the location service into a foreground state, which will make location updates in the background as frequent as in the foreground state. As a downside, it requires a sticky notification, so the user will be aware that your app is running and consumes more resources even if backgrounded. (Available since Android 8.0)
This is how it looks like on my code:
I hope it helps!
@tsapeta any movement on this issue ? Let us know what we can do to help troubleshoot this issue.
@tuzz thanks for the code… it’s not substantially different from the example I posted, so I’m a bit confused as to why it would behave differently. When you say ‘it works’ can you share whether you mean that:
Please let us know (as well as expo SDK version android simulator version, etc…) to narrow down the difference.
No, I tested it on SDK36 and it’s still broken
This is likely due to the loader error I raised in https://github.com/expo/expo/issues/6290 and that I think ends up being the problem in https://github.com/expo/expo/issues/6017 as well.
But yes, as it stands I think the BackgroundFetch API is just broken
I’m also having the same problem as @amaurymartiny, where the background task is only executed when the app is in the Recents Screen. When the app is terminated the background task doesn’t execute.
If it’s not expected behavior I’d be happy to provide a simple snack to test.