react-native-geolocation
react-native-geolocation copied to clipboard
Location in app background
Location when app background
How I can get location when app in background? Actually work when app in first plane, greetings
I've have the same need. I'm working on developing a running application that tracks your position and calculates distance, time and average paces, etc.
What I found is that you can call:
Geolocation.setRNConfiguration({
authorizationLevel: 'always',
skipPermissionRequests: false,
});
Which along with adding this to your ios/AppName/Info.plist:
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>some reason why your app needs this</string>
Will result in the app asking if the user would like to enable geolocation for "Only while using the app" or "Always Allow":
While previously, it had only prompted me with one option... That being said, after testing this out further, I found that my app didn't actually receive the location updates while my phone was either off OR on with the app in the background (I was on the home screen):
In the above screenshot:
- start -> A: app OPEN
- A -> B: I ran/walked with my PHONE OFF
- B -> C: app OPEN
- C -> D: in BACKGROUND (on Home Screen)
- D -> end: app OPEN
And I also confirmed that my privacy settings were in fact set to "Always" after I opted for that preference in the modal:
One last thing I'll note here is that I'm using Geolocation.getCurrentPosition() inside a setInterval()... I'll try and use Geolocation.watchPosition() instead and see if that gives me any better results.
@andersryanc this configuration ok in Ios, the problem is in Android, from Android 8.0 don't send location in background, alone a few times per hour. https://developer.android.com/about/versions/oreo/background-location-limits?hl=es-419
It's not just an issue on Android or at least I'm having the same issue on iOS with the way I have it setup. @chernandezq do you have background location tracking working on iOS? Can you share you configuration / some details about your implementation?
I tried to switch to using Geolocation.watchPosition() but I'm having a hard time even getting it to send me more than 1 or 2 positions:
Even after I've started watching and waited for ~1 minute, I still only have one or two points of data. I even tried various different config options for watchPosition(), like:
// A short timeout
{
timeout: 1000,
maximumAge: 0,
enableHighAccuracy: true,
useSignificantChanges: false,
}
// A little bit longer timeout
{
timeout: 3000,
maximumAge: 0,
enableHighAccuracy: true,
useSignificantChanges: false,
}
// Default timeout + maxAge
{
enableHighAccuracy: true,
useSignificantChanges: false,
}
// All Defaults
undefined
but none of these really seem to effect the performance of watchPosition(), I still only end up getting 1 or 2 points of data and then it just seems to stop working...
Ok, so it turns out, I missed a crucial step in the install/setup. As it says in the readme:
In order to enable geolocation in the background, you need to include the 'NSLocationAlwaysUsageDescription' key in Info.plist and add location as a background mode in the 'Capabilities' tab in Xcode.
I missed that second part and didn't enable the location background mode in Capabilities.
That said, I'm still having issues with watchPosition() and I'm not sure why exactly... so, in the end, I've chosen to switch to react-native-location which is working much better for me.
@andersryanc did you figure this out? I"m currently building a running app & having EXACT SAME problem & at exact same position.
i am also facing same issue. any update on this ?
Hi @chernandezq, did you figure out any solution for this for Android?
Hi guys, any solutions for Android issue?
I found alternative solution and it is working perfectly https://github.com/mo22/react-native-mo-geolocation