Doesn't asks for permissions on initial load

I have an ionic project. I am deploying this app into android. I want to access device’s microphone for which I have added permission in AndroidManifest.xml:

I have also added following code:

this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.RECORD_AUDIO).then(

        result => console.log('Has record audio permission?', result.hasPermission),

        err => {

          this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.RECORD_AUDIO).then((data: any) => {

            if (data.hasPermission) {

              console.log("have record audio permission");

            }

          });

        }

      );

yet it is not asking user for permissions on initial load. I am using Cordova. It is not going in the error section while checking for permissions.
Can anyone help me with this?

Not 100% sure on this, but permissions should be requested right before they are needed by android. This just might be android being android

I am facing the exact same problem. The permissions declared in the config.xml appears on the app info on Android and I can manually grant access to those permissions there, but that’s not how it should work. Did you manage to solve this ?

It makes no difference. The promise returned by hasPermission never resolves nor rejects.

You can try to request permission after the platform is ready

this.platform.ready().then(() => {
      this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.RECORD_AUDIO).then(

        result => console.log('Has record audio permission?', result.hasPermission),

        err => {

          this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.RECORD_AUDIO).then((data: any) => {

            if (data.hasPermission) {

              console.log("have record audio permission");

            }

          });

        }

      );
    })

At least in my case, I am already doing that and still is not working. What finally worked for me was to use the android diagnostic plugin.

Beware that this plugin is based on AndroidX, so you have to install the following files:

ionic cordova plugin add cordova-plugin-androidx
ionic cordova plugin add cordova-plugin-androidx-adapter

and then you can install the diagnostic plugin files:

ionic cordova plugin add cordova.plugins.diagnostic
npm install @ionic-native/diagnostic

Also remember to add Diagnostic to the providers list on app.module:

import { Diagnostic } from '@ionic-native/diagnostic/ngx';
...
providers: [..., Diagnostic]

To check if a permission is granted:

this.diagnostic.getPermissionAuthorizationStatus(this.diagnostic.permission.CAMERA)
    .then((status) => {
        switch(status){
            case this.diagnostic.permissionStatus.GRANTED:
                console.log("Permission granted to use the camera");
                break;
            case this.diagnostic.permissionStatus.NOT_REQUESTED:
                console.log("Permission to use the camera has not been requested yet");
                break;
            case this.diagnostic.permissionStatus.DENIED:
                console.log("Permission denied to use the camera - ask again?");
                break;
            case this.diagnostic.permissionStatus.DENIED_ALWAYS:
                console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
                break;
        }
    })
    .catch((error) => {
        console.error("The following error occurred: "+error);
    });

to request a permission:

this.diagnostic.requestRuntimePermission(this.diagnostic.permission.CAMERA)
    .then((status) => {
        switch(status){
            case this.diagnostic.permissionStatus.GRANTED:
                console.log("Permission granted to use the camera");
                break;
            case this.diagnostic.permissionStatus.NOT_REQUESTED:
                console.log("Permission to use the camera has not been requested yet");
                break;
            case this.diagnostic.permissionStatus.DENIED:
                console.log("Permission denied to use the camera - ask again?");
                break;
            case this.diagnostic.permissionStatus.DENIED_ALWAYS:
                console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
                break;
        }
    })
    .catch((error) => {
        console.error("The following error occurred: "+error);
    });
1 Like

Hey, I don’t remember the exact solution. But I do remember that I didn’t use any other plugin for this. It was me who was implementing this in a wrong way. Check your Android.manifest file and config.xml. Also make sure that you are checking permissions only before the code when they are needed, and once your ‘native’ platform is ready.

It would be nice to know how did you managed to get AndroidPermissions plugin to work. What exactly should I check in AndroidManifest or config.xml ? Regarding your other suggestions, I was checking for permission right before calling the code that would need that permission and long after the native platform was ready, and still it was not working.

I had a similar looking problem - neither the success or error code executed when requesting permissions, and the camera wouldn’t work. Weirdly, it also looked like deviceready never fired, but platform.ready() did.

What seemed to have changed in my app when the camera stopped working on android was moving to WkWebView, and using the webview plugin. When I added allow-navigation entries for http:///* and https:///*, everything just worked as expected. (I probably don’t need both.)