HaylLtd / cordova-background-geolocation-plugin

Background and foreground geolocation plugin for Cordova.
Apache License 2.0
55 stars 64 forks source link

permission issue on android X #170

Open dpsa-dev opened 10 months ago

dpsa-dev commented 10 months ago

Hi there

And thanks for the plugin and your time

The plugin is working pretty great on my Android 9 device, but when I want to test on my other device with Android 11 it crashes when calling the start function and I see the below

java.lang.SecurityException: Activity detection usage requires the ACTIVITY_RECOGNITION permission

To reproduce:

Install on device with Android 11 Run the start() function

Used on a Cordova version 12 app with

I have found online the following solution which makes the error go away but location tracking not starting as expect --also this additonal code makes the code stop working on device 1 with Android 9

public static final String[] PERMISSIONS = {
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION
};

changed to

public static final String[] PERMISSIONS = {
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACTIVITY_RECOGNITION
};
HarelM commented 10 months ago

The plugin is targeting Android 13, so you might need to use older version of the plugin, IDK. I suggest to try and debug it using Android studio and do the relevant changes accordingly...

dpsa-dev commented 10 months ago

Thanks for getting back:) that seldom happens hehe

Idk either I added a permission plugin for cordova https://www.npmjs.com/package/cordova-plugin-android-permissions

I dont think that helped, I think the below manifest entry did the job:

//this was already there...auto added by cordova /build <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" /> //added this additionally........? <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" /> <------

HarelM commented 10 months ago

If it helped then that's great!

bsprengelmeijer commented 9 months ago

I'm having the same issue, did you solve it already @dpsa-dev ?

I've tried asking for the permission before starting the service but it's not working

/edit nevermind I now see your comment about the added permission in the manifest. This also did the trick for me

dpsa-dev commented 9 months ago

I had to install a extra Cordova plugin in the end that was the only way to get it going: "cordova-plugin-android-permissions": "1.1.5", and then it should ask for the permission permissions.ACTIVITY_RECOGNITION which opens a popup window for the user asking permissions

and the below

Hope it helps

 var permissions = cordova.plugins.permissions;

        permissions.requestPermission(permissions.ACTIVITY_RECOGNITION, success, error);

        function error() {
            //alert('ACTIVITY_RECOGNITION is not turned on');
        }

        function success( status ) {

            BackgroundGeolocation.configure({
                locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
                desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
                stationaryRadius: 50,
                distanceFilter: 50,
                notificationTitle: 'Background tracking',
                notificationText: 'enabled',
                interval: 10000,
                debug: false,
                fastestInterval: 5000,
                activitiesInterval: 10000,
                url: `${baseUrlApi}update-live-share`,
                httpHeaders: {
                'X-FOO': 'bar'
                },
                // customize post properties
                postTemplate: {
                    "client" : CURRENT_CLIENT_ID,
                    "session" : CURRENT_TOKEN,
                    "liveShare" : CURRENT_MY_LIVE_SHARE_ID,
                    "latitude" : "@latitude",
                    "longitude" : "@longitude",
                    "accuracy" : CURRENT_ACCURACY 
                }
            });

            try {
                startTrackerTrack();
            } catch (e) { console.log(e); }
        }
Jurfix commented 8 months ago

Hello, I have Android 13, I use the full code from the example, and when I enable the use of GPS, the application crashes with the error: java.lang.SecurityException: Activity detection usage requires the ACTIVITY_RECOGNITION permission I added what you recommended to the manifest, but nothing changed: image

I also tried using the permission plugin, but it doesn’t even come with a request for GPS permission. If someone can help it would be great.

upd: So, I was still able to solve the problem using a permission request and a permissions plugin

dpsa-dev commented 8 months ago

In the end I had to do both the fixes above for it to work, eg 1. install the Cordova permission plugin and ask for permission before executing the navigation/geo code and 2. the manifest needs a extra line (which is still weird) <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />

when the permission plugin code is executed it should pop up a window on the app asking for activity permission, thats was for getting it to work on Android 12

Unless android 13 has new changes but I had the same general error message that permission missing

risinghero commented 8 months ago

To add extra line to manifest it's better to use config.xml like so:

<platform name="android">
        <config-file parent="/manifest" target="AndroidManifest.xml"
            xmlns:android="http://schemas.android.com/apk/res/android">
            <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
        </config-file>
    </platform>
dpsa-dev commented 8 months ago

I added it in the manifest file, just underneath the existing one which is <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />