react-native-geolocation icon indicating copy to clipboard operation
react-native-geolocation copied to clipboard

Location in app background

Open chernandezq opened this issue 6 years ago • 9 comments

Location when app background

How I can get location when app in background? Actually work when app in first plane, greetings

chernandezq avatar Jul 18 '19 21:07 chernandezq

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":

IMG_8333

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):

IMG_8335

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:

IMG_8336

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 avatar Jul 20 '19 00:07 andersryanc

@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

chernandezq avatar Jul 22 '19 15:07 chernandezq

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:

IMG_8363 IMG_8364

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...

andersryanc avatar Jul 22 '19 19:07 andersryanc

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 avatar Jul 22 '19 22:07 andersryanc

@andersryanc did you figure this out? I"m currently building a running app & having EXACT SAME problem & at exact same position.

yilakt avatar Jan 22 '20 18:01 yilakt

i am also facing same issue. any update on this ?

ansarikhurshid786 avatar Nov 11 '21 11:11 ansarikhurshid786

Hi @chernandezq, did you figure out any solution for this for Android?

Leipip avatar Jun 01 '22 00:06 Leipip

Hi guys, any solutions for Android issue?

GabrielDuarteJr avatar Jun 14 '23 20:06 GabrielDuarteJr

I found alternative solution and it is working perfectly https://github.com/mo22/react-native-mo-geolocation

ansarikhurshid786 avatar Jun 15 '23 05:06 ansarikhurshid786