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.6k stars 291 forks source link

Getting position times out when in the background on Android 10 #311

Closed Asghwor closed 2 years ago

Asghwor commented 2 years ago

Environment

Run npx react-native info in your terminal and copy the results here.

System:
    OS: Linux 5.4 Linux Mint 20.2 (Uma)
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 200.29 MB / 15.49 GB
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 14.17.3 - ~/.nvm/versions/node/v14.17.3/bin/node
    Yarn: 1.22.10 - ~/.nvm/versions/node/v14.17.3/bin/yarn
    npm: 6.14.13 - ~/.nvm/versions/node/v14.17.3/bin/npm
    Watchman: Not Found
  SDKs:
    Android SDK: Not Found
  IDEs:
    Android Studio: Not Found
  Languages:
    Java: 11.0.11 - /usr/bin/javac
    Python: Not Found
  npmPackages:
    @react-native-community/cli: Not Found
    react: Not Found
    react-native: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Platforms

Is this issue related to Android, iOS, or both ? Android

Versions

Please add the used versions/branches

Description

A callback is called every 30 seconds when it's either in the background or in foreground to get the current position and send to the server.

It works perfectly when the app is opened. But, as soon as the app is closed/minimized, getCurrentPosition() hangs until it times out. If there is no timeout, it just hangs indefinitely.

The background service itself is running perfectly, as I've checked by sending mocked positions. It just hangs when calling getCurrentPosition().

This problem has only manifested itself in more recent versions of Android; more specifically, Android 10 on Sansung Galaxy phones. All energy saving mechanisms had been disabled.

Reproducible Demo

  1. Start tracking.
  2. Close/minimize app.
  3. The service will be run until it reaches getCurrentPosition(), where it'll hang.

Expected Results

getCurrentPosition() would return the current device position and the background service would successfully finish its task.

Agontuk commented 2 years ago

Android 10 introduced background restrictions, maybe this is what causing the issue ? How did you implement background service ?

https://developer.android.com/about/versions/10/privacy/changes#background-activity-starts

Asghwor commented 2 years ago

Android 10 introduced background restrictions, maybe this is what causing the issue ? How did you implement background service ?

Thanks for answering!

The Android service is started when the user presses a tracking button in the app. It then starts a service that calls the JS side of the application every 30 seconds to run a callback that calls getCurrentPosition and sends the data to the server.

I made it keep a notification sticked in the notifications area so the service would not be killed in the background.

Asghwor commented 2 years ago

Adding ACCESS_BACKGROUND_LOCATION to AndroidManifest and asking for this permission after ACCESS_FINE_LOCATION makes it work.

zeeshan-shabbir commented 1 year ago

@Asghwor For background services which pkg used? I am in the same senior to send location to server but issue in bg. kindly help.