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

watchPosition is not working, when app is background.

Open harleenarora opened this issue 3 years ago • 8 comments

Platforms

Is this issue related to Android, iOS, or both: Both

Versions

Please add the used versions/branches

  • Android: 10 and latest version
  • iOS: 14 and latest version
  • react-native-geolocation-service: 5.3.0-beta.4
  • react-native: 0.63
  • react: 16

Description

I have used geolocation.watchPosition callback in our app. Its working if app is foreground, but not working app in background.

harleenarora avatar Jun 04 '22 08:06 harleenarora

It won't work for background. Use background service or background geolocation instead.

bsor-dev avatar Jun 14 '22 04:06 bsor-dev

@rosnaib11 @Naveenquara unfortunately those libraries (besides background geolocation) still need to be coupled with a geolocation library.

I'm curious as to why watchPosition cannot work in the background?

Rc85 avatar Aug 09 '22 03:08 Rc85

In android mainfist add this line .

and ask location permission in this order

const statuses = await PermissionsAndroid.request( PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION, ); const statuses1 = await PermissionsAndroid.request( PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION, ); const statuses2 = await PermissionsAndroid.request( PERMISSIONS.ANDROID.ACCESS_BACKGROUND_LOCATION, );

then use react-native-background-actions library

Naveenquara avatar Aug 20 '22 17:08 Naveenquara

@Naveenquara Google is very strict on ACCESS_BACKGROUND_LOCATION and your app will most likely be rejected unless without the permission, your app is not functional. I solved the issue by using notifee foreground service notifications. You can request the service to use with foreground service and location happens to be one of them. The notification will be ongoing (stays in your notification center) and execute long running tasks in the background until it is stopped.

Rc85 avatar Aug 20 '22 17:08 Rc85

It says in setup.md:

3) Update info.plist
There are three info.plist keys for location service

NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
NSLocationAlwaysAndWhenInUseUsageDescription

Unless you need background location update, adding only the first key will be enough. To enable background location update, you need to add all the keys in info.plist and add location as a background mode in the Signing & Capabilities -> Capability tab in Xcode. <---------

The combination of Location In Use permission, watchPosition method and Background Mode -> Location Updates seems to work just fine.

Now a blue tag surrounding iPhone clock appears when app is minimized. It does not work when app is closed.

I used useAppState hook to check if it was working properly:

LOG active
LOG {"coords": {...}}
LOG active
LOG inactive
LOG background
LOG {"coords": {...}}
LOG background

It is even working with the phone locked with black screen.

It works on android if you follow this repository example and use react-native-foreground-service to create a notification to keep the app running even while minimized.

I'm using:

  • iOS: 14.7.1
  • react-native-geolocation-service: 5.3.0
  • react-native: 0.68.2
  • react: 17.0.2

Elolawyn avatar Aug 29 '22 12:08 Elolawyn

@Elolawyn Would you have an example of how to use the package correctly?

kelvindominguez1595 avatar Aug 30 '22 14:08 kelvindominguez1595

You just have to read setup.md.

And have a look at this repository example.

The only things to remark:

  • Android Manifest: location permission, foreground permission and the use of VIForegroundService from react-native-foreground-service to execute code while showing a system notification to keep app running while minimized.
  • iOS Info.plist: location UIBackgroundModes to be able to use in background and permissions.
  • In the example, they use an old version of react-native-foreground-service. The library is used a bit differently now but not too much.

I have not been able to create a use case that totally satisfies me yet but something acceptable. For now i can request locations both in foreground and background as long as i do not close app. The notification created with react-native-foreground-service (only Android) should have a close button on it (read react-native-foreground-service) or at least should be very well controlled in app in case user close it, because last time i test it (still working on it) i closed app whitout stoping notification and the telephone keeps requesting location with user beign unable to manually close notification.

Elolawyn avatar Aug 30 '22 15:08 Elolawyn