transistorsoft / cordova-background-geolocation-SampleApp

Cordova Background Geolocation Sample Application
https://www.transistorsoft.com/shop/products/cordova-background-geolocation
MIT License
122 stars 78 forks source link

Trouble with location on Android 10 when location is allowed only while using the app #126

Closed bennyt2 closed 4 years ago

bennyt2 commented 4 years ago

Device: Google Pixel 3 with Android 10

Use Case: I am building a run tracker app and am trying to mirror permissions in other run tracking apps, which work while location is allowed only when using the app. I'm using this sample app as a guide because we can't get the following scenario working on Android 10.

From what I read on Android permissions, it sounds like the following scenario should work on this app:

  1. Set location permission to "Allow only while using the app"
  2. Open app
  3. Go to "Advanced"
  4. Turn location on by clicking the toggle in the top right.
  5. Click green Play button in the bottom right
  6. Click phone's home button to go back to the home screen. Foreground service starts. Note that I have not exited the app.
  7. Click button to turn screen off.
  8. Walk for 400 meters
  9. Observe that the phone tracked the location.

However, that doesn't work ((http://tracker.transistorsoft.com/SpikeFalcon?device=15805&end=2020-5-4&start=2020-5-4%2013%3A0). I walked a loop around Overhill Dr and Dunloggin Rd). Almost immediately after the screen turns off, location stops tracking.

Could this be related to foreground service needing to have type "location" in Android 10?

This scenario does work on an Android 8 phone and an ipad.

Has this app been updated to target Android API 29? If not, perhaps that's the issue.

christocracy commented 4 years ago

I would love to add the new attribute android:foregroundServiceType="location" to the library but it would break everyone not building with compileSdkVersion 29.

Try this in the config.xml:

<widget
+  xmlns:tools="http://schemas.android.com/tools"
>
  <platform name="android">
+     <edit-config file="AndroidManifest.xml" mode="merge" target="/manifest">
+             <manifest xmlns:tools="http://schemas.android.com/tools" />
+     </edit-config>
       <config-file parent="/manifest/application" target="app/src/main/AndroidManifest.xml">
            <meta-data android:name="com.transistorsoft.locationmanager.license_key" android:value="6da948b0a8e4eebd2af03d2defe363bc3d5ca380b4bb846cb8342c6446c69975" />
            <meta-data android:name="com.transistorsoft.locationmanager.ENCRYPTION_PASSWORD" android:value="transistorsoft" />
+            <service android:foregroundServiceType="location" android:name="com.transistorsoft.locationmanager.service.TrackingService" tools:node="replace" />
        </config-file>
bennyt2 commented 4 years ago

Thanks for your quick reply!

I added those lines and also updated defaultTargetSdkVersion and defaultCompileSdkVersion in build.gradle to 29.

Still having the same problem on Android 10. It worked on an Android 9 tablet though.

bennyt2 commented 4 years ago

Is it fair to say that this plugin will not work with the following conditions:

  1. Android 10 device
  2. "When In Use" location
  3. App is running and using a foreground service but is not active on the screen

If that's true, then Android 10 users must have their location set to "Always". Just double checking. Thank you.

christocracy commented 4 years ago

I've figured out how to do this. It does involve android:foregroundServiceType="location", however there's a slight modification required in the plugin's build.gradle as well. I've just merged the required changes to master of private repo.

After installing latest master (3.7.0-rc.3), and ensuring your Cordova app is configured for compileSdkVersion 29, add the following to your config.xml (there is no need for the xmlns:tools/tools:replace stuff):

<platform name="android">
        <config-file parent="/manifest/application" target="app/src/main/AndroidManifest.xml">
            <meta-data android:name="com.transistorsoft.locationmanager.license_key" android:value="YOUR_LICENSE_KEY" />
+            <service android:foregroundServiceType="location" android:name="com.transistorsoft.locationmanager.service.TrackingService" />
+            <service android:foregroundServiceType="location" android:name="com.transistorsoft.locationmanager.service.LocationRequestService" />
        </config-file>

While your app is in the foreground, execute changePace(true) then put app in background (or terminate it). In the Sample app, the [ > ] button execute changePace(true). Also, while debugging this, it's best to configure distanceFilter: 0, locationUpdateInterval: 1000, debug: true, LOG_LEVEL_VERBOSE in order to generate lots of location updates quickly.

bennyt2 commented 4 years ago

@christocracy This works! Thanks for digging into it :)