-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BackgroundFetch not running #3582
Comments
Hey, @leighquince! As noted in the documentation, |
In my |
Would help if you could give us a snack or toy repo demonstrating the issue if changing the call context doesn't resolve it |
A quick snack https://snack.expo.io/@domosafety/background-fetch. Task is defined at top level, registered when App mounts, no logs are seen to show that the background task runs. Also defining the task directly rather than through a call had no effect in my actual App. |
Struggling with this too, i have defined a task in Global scope (GET_STEPS), but console logging the promise returns "undefined".
I put BackgroundFetch.registerTaskAsync in my app.js render method for now, but it appears to not work. |
@Oakyden there is no data income in BackgroundFetch. you can call fetch, process it and return BackgroundFetch.Result.### result. |
i can confirm that BackgroundFetch work for me, but very strange. looks like i can not call anything like in main app(asyncstorage, get coordinates or something like this) and frequency of calling very various, it's making debug hard. So i decide to wait android implementation. |
Can you share an example expo snack or similar @superyarik ? especially if you use a simple API example like https://jsonplaceholder.typicode.com/ |
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 So, you have to
Be aware that an emulator does not run your task, that happens only on real devices. The following worked for me: export async function registerFetchTask() {
TaskManager.defineTask(FETCH_TASKNAME, runBackgroundSaga);
const status = await BackgroundFetch.getStatusAsync();
switch (status) {
case BackgroundFetch.Status.Restricted:
case BackgroundFetch.Status.Denied:
logger.log("Background execution is disabled");
return;
default: {
logger.debug("Background execution allowed");
let tasks = await TaskManager.getRegisteredTasksAsync();
if (tasks.find(f => f.taskName === FETCH_TASKNAME) == null) {
logger.log("Registering task");
await BackgroundFetch.registerTaskAsync(FETCH_TASKNAME);
tasks = await TaskManager.getRegisteredTasksAsync();
logger.debug("Registered tasks", tasks);
} else {
logger.log(`Task ${FETCH_TASKNAME} already registered, skipping`);
}
logger.log("Setting interval to", INTERVAL);
await BackgroundFetch.setMinimumIntervalAsync(INTERVAL);
}
}
} |
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. |
Hello, it looks like we need to use |
@mskg you should call On other hand, remember that there is a function already created for doing that check: |
@BrodaNoel no, you MUST call it every time when the App starts. See https://developer.apple.com/documentation/uikit/uiapplication/1623100-setminimumbackgroundfetchinterva The |
Is it really true? This is the only command that fires the task? Does not it make sense? Is there any other command not related to Location? |
@SowinskiMateusz no, see my example above. |
Could you provide a more complete example, or possibly add comments to your original |
Doesnt appear to work for me either, my setup for geofencing works so following same convention, define task in global scope etc
Manual trigger inside a component
adding this at the end appears to make it work, as suggested by @tomas1000r why? @tsapeta
|
The example provided here: https://github.com/expo/expo/blob/master/apps/native-component-list/screens/BackgroundFetchScreen.js I've tailored it to work without the alpha release and without react navigation, |
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 |
@tsapeta can you please explain why this doesnt work: https://pastebin.com/gNU5QnJv |
@farzd |
thanks for your time @tsapeta, i realised the arguments were for SDK33 but assumed it would no op anyway. Background app refresh is on in settings etc, not sure what else to check |
At least according to my experience here: On iOS it is actually really like @tsapeta described. The time you set is just an indication on how often data might change. If it doesn’t (depending on the result), you‘re scheduled less frequent. Also, if you don’t use the App, the fetch will most likely not run. In my case, the fetch currently runs directly after/when I open the App in the morning. I seem to do that regularly :) |
I directly tested the snack on a ios device and on a simulator using the expo app, and I cannot get console.log('background fetch running') ; to display, with or without remote debugging on, even after waiting 10min for it to occur. How can I debug further? |
I m unable to make it works on ios with SDK33. Can have a working snack or example? |
I am having trouble with SDK33 on ios as well. A working snack would be very helpful in debugging the issue and solidify the steps required to make the background fetch work. |
BackgroundFetch not working with me. I use expo client test it. Is it only working in standalone app? |
@mczernek 👍 Upvote for more reliable background tasks via BGTaskScheduler |
I have the same problem. Have there been any developments? |
+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. |
There is example for virtually every module we provide available within our repository. Here's one for BackgroundFetch.
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. |
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! |
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. |
This is how I define the task, just before the methods above.
If you have any problems getting current location, you can also use this code in the task function
|
@tsapeta Can you think of anything that can be done from within Expo that can replicate this 'keep active' functionality without requiring location updates constantly being run in the background? Unfortunately with iOS now actively showing at all times when an app is using locational data, having our app constantly checking the location of our users when it has no need to is a really bad look for our application. |
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. |
Have you tried
|
@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. |
When would this be resolved. It is such a 'must-have' feature. I need to fetch data periodically and most of the time an app is not in the background/foreground |
@artsnr1 Does background fetch work for you when the app is in background/foreground? If Yes, do you use location? |
@srikanthkh I haven't tried it yet as it doesn't provide for my need to work even on app termination |
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 |
Unfortunately, ejection is the last resort |
Well, finally I ejected, any recommendations for the alternatives? |
To me, I used mauron combined with headlessJS. Because mauron location listener doesn't work if app is terminated. That's why I used headlessJS. But sending of location using postTemplate will work even if app is terminated. I used headlessJS just for my own purpose. |
I have an MVCE that shows this problem https://github.com/trajano/expo-mvce it's based off https://github.com/expo/expo/blob/master/apps/native-component-list/src/screens/BackgroundFetchScreen.tsx in my case it works on Android, but not iOS Expo clients. I am using a custom Expo client as well I also added BackgroundFetch.setMinimumIntervalAsync(60); |
Why was this issue closed if no resolution - it seems such a fundamental feature of BackGround fetch? What is the alternative? |
Was a solution ever found for this problem? |
@LeifS @sicero it does work, my MVCE just needs to update the README but the MVCE does work. There's one caveat, on iOS you can't really predict when the background fetch will fire. You need to keep the phone fully charged and plugged into get it more frequent https://github.com/trajano/expo-mvce. I lost track of this issue number so I wasn't able to update it till I saw the alert. |
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. |
@marksalpeter I just did the opposite: I'm ditching semi-abandoned volunteer-run react-native libraries and been enjoying Expo unimodules a lot more. Best of luck! |
for anyone that stumbles on this - @ajsmth recently wrote up some useful instructions for trigger background fetch manually, which is much easier than waiting for the operating system to kick in for you: https://docs.expo.io/versions/latest/sdk/background-fetch/#triggering-background-fetches keep in mind the following limitation for iOS as well: BackgroundFetch only works when the app is backgrounded, not if the app was terminated or upon device reboot. i'm going to lock this thread because as far as we know this is working as expected, please make a new issue if you continue to encounter issues. |
Environment
app's target - iOS, Standalone
Steps to Reproduce
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?
The text was updated successfully, but these errors were encountered: