cordova plugin permission not working in my application

Pravin Maske

Friends Thanks in advance. This is my first question. I am not a Cordova developer but due to some circumstances, I have to work on Cordova application. My application previously targets the Android API level 22 and now I am targetting the API level 26.

From the above API level, 22 androids required runtime permission and I am trying to implement the permission code in my application.

I need to copy a pdf file from my application to device memory. I have already written the code for the copy file which is working fine up to android API level 22 but not working on above android API level 23.

To work this I need to add the permission in my Cordova app.

I have used the following plugin for permission cordova plugin add cordova-plugin-permission

Following is my index.js code

var app = {
    // Application Constructor
        initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
        bindEvents: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false);
        },
     //deviceready Event Handler
     //
     // The scope of 'this' is the event. In order to call the     'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
        onDeviceReady: function() {
           app.receivedEvent('deviceready');
		
         // **my permission code**
        var Permission = cordova.plugins.Permission

        var permission = 'android.permission.WRITE_EXTERNAL_STORAGE'

        Permission.has(permission,function(results){
            if(!results[permission])
            {
                Permission.request(permission,function(results){
                    if(results[permission]){
                          alert("permission granted");
                   }
                },alert("permission failed"))
                alert("permission granted failed");
            }
        }, alert("permission failed"))

              
                   asset2sd.copyDir({
                   asset_directory: "www/pdf",
                   destination_directory: "crisispdf",
                 },
                 function () {
                           //alert('success');
                 },
                 function () {
                     //alert('fail');
                 }
                 );  

        
	},
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

       

        console.log('Received Event: ' + id);
    }
     };

     app.initialize();

Following is my config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.atlascopco.crisis" version="1.0.13" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>Crisis Management</name>
    <description>
        Crisis Management 
    </description>
    <author email="[email protected]" href="http://cordova.io">
        Atlas Copco Team
    </author>
    <content src="index.html" />
    <access origin="*" />
    <access launch-external="yes" origin="mailto:*" />
    <access launch-external="yes" origin="tel:*" />
    <plugin name="cordova-plugin-inappbrowser" spec="^1.7.1" />
    <plugin name="cordova-plugin-wkwebview-engine" spec="^1.1.3" />
    <plugin name="cordova-plugin-permission" spec="^0.1.0" />
    <platform name="android">
        <uses-permission android:maxSdkVersion="26" android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:maxSdkVersion="26" android:name="android.permission.READ_EXTERNAL_STORAGE" />
    </platform>
    <engine name="android" spec="^7.1.4" />
</widget>

Following is my Android Manifest file form the android platform after creating the debug apk

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="10013" android:versionName="1.0.13" package="com.atlascopco.crisis" xmlns:android="http://schemas.android.com/apk/res/android">
    <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" />
    <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: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>
    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>

Please let me know what is the problem in my code when checking this in chrome browser it is showing the following result and the permission popup is not getting at the time of app start enter image description here

Diptyajit Mitra

Try This Code

var app = {
    // Application Constructor
        initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
        bindEvents: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false);
        },
     //deviceready Event Handler
     //
     // The scope of 'this' is the event. In order to call the     'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
        onDeviceReady: function() {
           app.receivedEvent('deviceready');
		
         // **my permission code**
        var permission = cordova.plugins.permissions;

        permission.hasPermission(permission.WRITE_EXTERNAL_STORAGE,function(results){
            if(!results[permission])
            {
                permission.requestPermission(permission.WRITE_EXTERNAL_STORAGE,function(results){
                    if(results[permission]){
                          alert("permission granted");
                   }
                },alert("permission failed"))
                alert("permission granted failed");
            }
        }, alert("permission failed"))

              
                   asset2sd.copyDir({
                   asset_directory: "www/pdf",
                   destination_directory: "crisispdf",
                 },
                 function () {
                           //alert('success');
                 },
                 function () {
                     //alert('fail');
                 }
                 );  

        
	},
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

       

        console.log('Received Event: ' + id);
    }
     };

     app.initialize();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cordova geoLocation plugin in iOS not working fine

From Dev

cordova cordova-plugin-statusbar StatusBarOverlaysWebView not working

From Dev

Request Permission in Android Cordova plugin does not prompt the user

From Dev

cordova camera plugin asks permission every time

From Dev

Cordova Plugin Camera Mediatype with Video is not working

From Dev

cordova-plugin-geolocation watchPosition() method not working

From Dev

Cordova Plugin not working

From Dev

my playlist plugin is not working on videojs?

From Dev

Ioniv-v4 : ionic cordova plugin add cordova-plugin-googleplus Not working

From Dev

Dependency not working with my application

From Dev

after installing ionic cordova plugin add cordova-plugin-fcm-with-dependecy-updated my android built not working

From Dev

Pagination not working on my application - CodeIgniter

From Dev

Cordova File plugin breaks my app

From Dev

Best database choice for my Cordova application

From Dev

Cordova "Console" plugin not working

From Dev

Error ActivateApplication in my Windows Cordova application

From Dev

Why is $http request not working in this Cordova Application?

From Dev

My JQuery Chosen plugin not working

From Dev

Cordova & Ionic on ios - Device plugin not working

From Dev

cordova-plugin-camera not working in IOS

From Dev

Why my wordpress plugin is not working

From Dev

CakePHP - CakeDC Plugin Permission Not Working

From Dev

How to create a cordova plugin for my android library?

From Dev

Angular resource not working in Cordova application

From Dev

TabPane is not working on my Application

From Dev

Cordova file-transfer plugin not working in simulator

From Dev

Razorpay cordova plugin not working for iOS

From Dev

Cordova Custom-URL-scheme plugin not working

From Dev

localStorage is working wrong in my application

Related Related

  1. 1

    Cordova geoLocation plugin in iOS not working fine

  2. 2

    cordova cordova-plugin-statusbar StatusBarOverlaysWebView not working

  3. 3

    Request Permission in Android Cordova plugin does not prompt the user

  4. 4

    cordova camera plugin asks permission every time

  5. 5

    Cordova Plugin Camera Mediatype with Video is not working

  6. 6

    cordova-plugin-geolocation watchPosition() method not working

  7. 7

    Cordova Plugin not working

  8. 8

    my playlist plugin is not working on videojs?

  9. 9

    Ioniv-v4 : ionic cordova plugin add cordova-plugin-googleplus Not working

  10. 10

    Dependency not working with my application

  11. 11

    after installing ionic cordova plugin add cordova-plugin-fcm-with-dependecy-updated my android built not working

  12. 12

    Pagination not working on my application - CodeIgniter

  13. 13

    Cordova File plugin breaks my app

  14. 14

    Best database choice for my Cordova application

  15. 15

    Cordova "Console" plugin not working

  16. 16

    Error ActivateApplication in my Windows Cordova application

  17. 17

    Why is $http request not working in this Cordova Application?

  18. 18

    My JQuery Chosen plugin not working

  19. 19

    Cordova & Ionic on ios - Device plugin not working

  20. 20

    cordova-plugin-camera not working in IOS

  21. 21

    Why my wordpress plugin is not working

  22. 22

    CakePHP - CakeDC Plugin Permission Not Working

  23. 23

    How to create a cordova plugin for my android library?

  24. 24

    Angular resource not working in Cordova application

  25. 25

    TabPane is not working on my Application

  26. 26

    Cordova file-transfer plugin not working in simulator

  27. 27

    Razorpay cordova plugin not working for iOS

  28. 28

    Cordova Custom-URL-scheme plugin not working

  29. 29

    localStorage is working wrong in my application

HotTag

Archive