cordova-plugin-android-permissions icon indicating copy to clipboard operation
cordova-plugin-android-permissions copied to clipboard

Does not fire failure or success callback when requestPermission() is called

Open Truelion opened this issue 6 years ago • 8 comments

cordova plugin does not fire a failure or success callback when requestPermission() is called on the very first install of the apk via USB cable or on emulator or from installing an APK build from disk. It does pop the dialog to request geo permissions fine, I grant it, but it never fires a callback. (?)

When i close the app and reopen, geolocation works, because i previously granted. But on very first install, callbacks for requestPermission() does nothing, so app is at standstill. Does anyone know what it could be? Tried on several phones, all identical behavior.

USING window.cordova.plugins:

requestCordovaPermissions() {
    var self = this;
    window.cordova.plugins.permissions.requestPermission(
        window.cordova.plugins.permissions.ACCESS_FINE_LOCATION,
        function(status) {// <-------never fired
            if (status.hasPermission) {
                alert("permission granted") _ //never seen, even if i granted_
            } else {
                alert("unable to get geo location permissions")
            }
        },
        function(e) {//<------ never fired
            alert("Error : " + e)
        }
    )
}


Snippet from AndroidManifest.xml:
`
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="25" />
    <application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
`


config.xml:

`
<feature name="Geolocation">
        <param name="android-package" value="org.apache.cordova.GeoBroker" />
    </feature>
    <engine name="android" spec="^6.3.0" />
    <plugin name="cordova-plugin-android-permissions" spec="^1.0.0" />
    <plugin name="cordova-plugin-exclude-files" spec="^0.4.2" />
    <plugin name="cordova-plugin-fullscreen" spec="^1.2.0" />
    <plugin name="cordova-plugin-geolocation" spec="^4.0.1" />
    <plugin name="cordova-plugin-whitelist" spec="^1.3.3" />
`

Truelion avatar Mar 27 '19 11:03 Truelion

I think I have your solution in this video - https://youtu.be/W47ZJ1vgqqI

prof3ssorSt3v3 avatar Mar 27 '19 22:03 prof3ssorSt3v3

Thanks, going to watch vid and try it out and will reply back.

Truelion avatar Mar 28 '19 00:03 Truelion

You the man!!! @prof3ssorSt3v3

jamesta696 avatar Mar 28 '19 02:03 jamesta696

So I looked at video and implemented but did not use the config-file element, that did not work for me, i dropped my permissions into AndroidManifest.xml, same result. There appears to be a bug on first install where success/error never gets fired by the cordova-plugin-android-permissions plugin. It prompts for grant, but never calls the callbacks, the code is left in a hanging state.

closing the app and reopening, it works, it remembers the grant and runs as usual.

To replicate: fire off a requestPermissions() call as soon as the app boots after deviceready. You'' notice it prompts for granting, but never does anything after that. btw i am running: <engine name="android" spec="^6.3.0" />

Truelion avatar Mar 28 '19 13:03 Truelion

What i ended up doing was kicking off the requestPermissions() which opens a modal grant popup. And immediately I have a 1sec timeout that attempts to get another getCurrentPosition() call which should of been granted otherwise the error callback attempts another requestPermissions(), have to add retry logic only about 2 times (yet to do) but the timeout fixes it by placing assumption that it was granted.

cordova-plugin-android-permissions not kicking off the success/error callbacks does seem like a bug on first install of app for sure. Again to replicate, fire off a requestPermissions() call on app startup.

It works perfectly fine if you have a button on screen that does the requestPermissions() flow by a user click as your video shows and what seems to be the common use case. But for a weather app that needs to show weather for the users location when it boots, it makes alot of sense.

Truelion avatar Mar 28 '19 13:03 Truelion

It is often an issue of sequencing with these plugins. I have my students building apps using Google maps, geolocation, and other plugins. They have to get the sequence right so everything works properly. My demo app will crash if you rapidly click on the buttons to make multiple requestPermission calls at the same time. I would recommend updating your cordova-android version too.

prof3ssorSt3v3 avatar Mar 28 '19 15:03 prof3ssorSt3v3

i do know my sequencing is right, i traced it several times. I also do know for a fact that the requestPermission() success/fail callback never gets stepped into, ever, where if it did, fires off a chain of other events, one of them hides my splash overlay. So the app just spins.

btw what is the cli cmd for updating cordova-android ?

Nashorn avatar Mar 28 '19 18:03 Nashorn

cordova platform add [email protected] Just replace the x.x.x with the version you want.

If you update Cordova you will automatically get the latest version of cordova-android when calling cordova platform add android

prof3ssorSt3v3 avatar Mar 28 '19 22:03 prof3ssorSt3v3