question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cordova AndroidPermissions is not working on Capacitor 3

See original GitHub issue

Bug Report

Capacitor Version

Latest Dependencies:

  @capacitor/cli: 3.3.1
  @capacitor/core: 3.3.1
  @capacitor/android: 3.3.1
  @capacitor/ios: 3.3.1

Installed Dependencies:

  @capacitor/ios: not installed
  @capacitor/cli: 3.3.1
  @capacitor/core: 3.3.1
  @capacitor/android: 3.3.1

Platform(s)

Android

Current Behavior

When i use Cordova requestPermissions the app is paused the resumed and don’t ask for Permissions

2021-11-07 17:55:43.787 31260-1252/io.ionic.starter V/Capacitor/Plugin: To native (Cordova plugin): callbackId: Permissions1937920063, service: Permissions, action: checkPermission, actionArgs: [null]
2021-11-07 17:55:43.827 31260-31260/io.ionic.starter D/InputMethodManager: HSIFW - flag : 0
2021-11-07 17:55:43.851 31260-1252/io.ionic.starter V/Capacitor/Plugin: To native (Cordova plugin): callbackId: Permissions1937920064, service: Permissions, action: requestPermissions, actionArgs: ["android.permission.ACCESS_BACKGROUND_LOCATION","android.permission.ACCESS_COARSE_LOCATION","android.permission.ACCESS_FINE_LOCATION",null,null,null]
2021-11-07 17:55:43.899 31260-31260/io.ionic.starter D/Capacitor: App paused
2021-11-07 17:55:43.923 31260-31260/io.ionic.starter I/SurfaceControl: nativeRelease nativeObject s[-5476376609291517120]
2021-11-07 17:55:43.923 31260-31260/io.ionic.starter I/SurfaceControl: nativeRelease nativeObject e[-5476376609291517120]
2021-11-07 17:55:43.926 31260-31260/io.ionic.starter I/SurfaceControl: assignNativeObject: nativeObject = 0 Surface(name=null)/@0xe3f662e / android.view.SurfaceControl.readFromParcel:1117 android.view.IWindowSession$Stub$Proxy.relayout:1820 android.view.ViewRootImpl.relayoutWindow:9005 android.view.ViewRootImpl.performTraversals:3360 android.view.ViewRootImpl.doTraversal:2618 android.view.ViewRootImpl$TraversalRunnable.run:9971 android.view.Choreographer$CallbackRecord.run:1010 android.view.Choreographer.doCallbacks:809 android.view.Choreographer.doFrame:744 android.view.Choreographer$FrameDisplayEventReceiver.run:995 
2021-11-07 17:55:43.927 31260-31260/io.ionic.starter I/SurfaceControl: nativeRelease nativeObject s[-5476376611317754944]
2021-11-07 17:55:43.927 31260-31260/io.ionic.starter I/SurfaceControl: nativeRelease nativeObject e[-5476376611317754944]
2021-11-07 17:55:43.927 31260-31260/io.ionic.starter I/SurfaceControl: nativeRelease nativeObject s[-5476376609291517024]
2021-11-07 17:55:43.927 31260-31260/io.ionic.starter I/SurfaceControl: nativeRelease nativeObject e[-5476376609291517024]
2021-11-07 17:55:43.928 31260-31260/io.ionic.starter I/SurfaceControl: nativeRelease nativeObject s[-5476376609291518752]
2021-11-07 17:55:43.928 31260-31260/io.ionic.starter I/SurfaceControl: nativeRelease nativeObject e[-5476376609291518752]
2021-11-07 17:55:43.930 31260-31260/io.ionic.starter I/ViewRootImpl@311ddf5[MainActivity]: Relayout returned: old=(0,0,720,1600) new=(0,0,720,1600) req=(720,1600)0 dur=11 res=0x1 s={true -5476376606574522368} ch=false fn=92
2021-11-07 17:55:43.936 31260-31260/io.ionic.starter I/ViewRootImpl@311ddf5[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0 1
2021-11-07 17:55:44.006 31260-31260/io.ionic.starter D/Capacitor: Unable to find a Capacitor plugin to handle permission requestCode, trying Cordova plugins 0
2021-11-07 17:55:44.008 31260-31260/io.ionic.starter I/ViewRootImpl@311ddf5[MainActivity]: stopped(false) old=false
2021-11-07 17:55:44.009 31260-31260/io.ionic.starter D/Capacitor/AppPlugin: Firing change: true
2021-11-07 17:55:44.009 31260-31260/io.ionic.starter V/Capacitor/AppPlugin: Notifying listeners for event appStateChange
2021-11-07 17:55:44.009 31260-31260/io.ionic.starter D/Capacitor/AppPlugin: No listeners found for event appStateChange
2021-11-07 17:55:44.010 31260-31260/io.ionic.starter D/Capacitor: App resumed
2021-11-07 17:55:44.019 31260-1252/io.ionic.starter V/Capacitor/Plugin: To native (Cordova plugin): callbackId: LaunchNavigator1937920065, service: LaunchNavigator, action: discoverSupportedApps, actionArgs: []
2021-11-07 17:55:44.019 31260-1252/io.ionic.starter D/LaunchNavigatorPlugin: Plugin action=discoverSupportedApps

The app asks for permissions when calling the method that requires permissions (BackgroundMode and Geolocation) Both cordova plugins

It was working fine on Capacitor 2

Code Reproduction

public async checkBackgroundPermission() {
    try {
      const result = await this.androidPermissions.checkPermission(
        this.androidPermissions.PERMISSION.ACCESS_BACKGROUND_LOCATION
      );
      if (!result || result.hasPermission === false) {
        this.requestPermissions();
      }
    } catch (error) {
      this.requestPermissions();
    }
  }

  private async requestPermissions() {
    try {
      const data = await this.androidPermissions.requestPermissions([
        'android.permission.ACCESS_BACKGROUND_LOCATION',
        'android.permission.ACCESS_COARSE_LOCATION',
        this.androidPermissions.PERMISSION.ACCESS_FINE_LOCATION,
        this.androidPermissions.PERMISSION.ACTIVITY_RECOGNITION,
        this.androidPermissions.PERMISSION.FOREGROUND_SERVICE,
        this.androidPermissions.PERMISSION.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
      ]);
    } catch (error) {
      await alert('Error : ' + error);
      console.log(error);
    }
  }

https://github.com/ahmadmasoum/cordovaPermissions.git

Other Technical Details

npm --version: 6.14.10

node --version: v14.15.4

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jcesarmobilecommented, Nov 9, 2021

When you target SDK 30 you can’t request background location permissions with other permissions, you have to do it separatelly

Caution: If your app targets Android 11 (API level 30) or higher, the system enforces this best practice. If you request a foreground location permission and the background location permission at the same time, the system ignores the request and doesn’t grant your app either permission.

https://developer.android.com/training/location/permissions

0reactions
ionitron-bot[bot]commented, Nov 10, 2022

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use cordova-plugin-android-permissions in Ionic 3?
I found this Ionic native plugin to handle these permissions in all versions of Android But exactly not getting how to use it...
Read more >
Capacitor and Cordova Android Permissions Plugin - Ionic.io
The Capacitor and Cordova Android Permissions plugins are designed to support Android's new permissions checking mechanism. Find all permissions here.
Read more >
Android Permissions - Ionic Framework
This plugin is designed to support Android new permissions checking mechanism. Learn how to install and use Android Device permissions on your Ionic...
Read more >
Using Cordova Plugins and Ionic Native - Capacitor
Compatibility Issues​. Some Cordova plugins don't work with Capacitor or Capacitor provides a conflicting alternative. See here for details and a known ...
Read more >
cordova-plugin-android-permissions - npm
Older Android plugins may not support this new approach or request the necessary permissions, but Cordova developers can work around this ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found