Open harleenarora opened 2 years ago
It won't work for background. Use background service or background geolocation instead.
@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?
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 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.
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:
14.7.1
5.3.0
0.68.2
17.0.2
@Elolawyn Would you have an example of how to use the package correctly?
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.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.
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.