Agontuk / react-native-geolocation-service

React native geolocation service for iOS and android
https://www.npmjs.com/package/react-native-geolocation-service
MIT License
1.61k stars 291 forks source link

watchPosition is not working, when app is background. #352

Open harleenarora opened 2 years ago

harleenarora commented 2 years ago

Platforms

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

Versions

Please add the used versions/branches

Description

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

bsor-dev commented 2 years ago

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

Naveenquara commented 2 years ago

use react native background actions

Rc85 commented 2 years ago

@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?

Naveenquara commented 2 years ago

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

Rc85 commented 2 years ago

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

Elolawyn commented 2 years ago

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:

kelvindominguez1595 commented 2 years ago

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

Elolawyn commented 2 years ago

You just have to read setup.md.

And have a look at this repository example.

The only things to remark:

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.