How to keep HTTP requests running in background? - Android
See original GitHub issueFirst of all, awesome plugin. Now, I understand this is not exactly a plugin’s issue, but probably a very common scenario for it. I’m trying to watch and send the location via HTTP Post request to server as in the following code:
BackgroundGeolocation.addWatcher(
{
backgroundMessage:
"Location is still tracked in background.",
requestPermissions: true,
stale: true,
distanceFilter: 200,
},
async function (location) {
if (location) {
let formData = new FormData();
formData.append("location", JSON.stringify(location));
await ApiService.sendLocation(formData); // Does a simple HTTP post request using Axios.
}
}
);
This is working fine in iOS indefinitely (as long as I grant the ‘Always’ location permission).
However for Android, this is not the case. I granted all battery/data usage permissions for my app in particular. After 5 minutes in background, the server will stop receiving locations. As soon as I enter the app again (it is not being killed btw, I can tell because it does not reload the splash screen) it will start sending all the buffered requests to server. So the location is being tracked correctly, but it seems like the HTTP requests get blocked and start to pile up.
Does this mean that I should actually do the HTTP request inside the native callback code?
public void onLocationResult(LocationResult locationResult)
I would appreciate any insight, but as I said, I understand this is not precisely this plugin’s issue.
Issue Analytics
- State:
- Created 3 years ago
- Comments:26 (3 by maintainers)
Top GitHub Comments
@gbrits Yeah I switched from Axios to the Http plugin for this specific call.
So on this plugin’s callback I do:
And this method will use Http community plugin, not axios.
If you need a quick try, you may overwrite this plugin’s native callback function and add something like the following.