0

Below code is a cordova android sms acess permission plugin code. and it is working fine.

inside that i am trying to call my this.getSMS() function it is throughing error

setTimeout(() => {
      this.holders.dissmissLoadingCustom();
      platform.ready().then(() => { 

      var permissions = cordova.plugins.permissions;

        permissions.hasPermission(permissions.READ_SMS, checkPermissionCallback, null);

        function checkPermissionCallback(status) {
          if(!status.hasPermission) {
            var errorCallback = () => {
              console.log("invoking the errorCallback");
              alert('READ_SMS permission is not turned on');
            }

            permissions.requestPermission(
              permissions.READ_SMS,
              (status) => {
                console.log("invoking status");

              if(!status.hasPermission) {
                console.log("invoke !status.hasPermission");
                errorCallback();
              }
              else{
                console.log("invoking else part !status.hasPermission");
                this.getSMS();//this line through error
              }
            },
            errorCallback);
          }
        }

      });

    }, 10000);

if the user tap allow then i am trying to call my this.getSMS() as you can see in the above code. if user deny then i am just getting error callback.

i dont know what i am doing wrong.

eko
  • 34,608
  • 9
  • 60
  • 85
Mohan Gopi
  • 7,080
  • 16
  • 57
  • 107

1 Answers1

0

You have defined checkPermissionCallback as a regular function. The this keyword points to that function and not the actual class in which it is defined.

setTimeout(() => {
      this.holders.dissmissLoadingCustom();
      platform.ready().then(() => { 

      var permissions = cordova.plugins.permissions;

        permissions.hasPermission(permissions.READ_SMS, (status)=>checkPermissionCallback(status), null);//use arrow to call

        function checkPermissionCallback(status) {
          if(!status.hasPermission) {
            var errorCallback = () => {
              console.log("invoking the errorCallback");
              alert('READ_SMS permission is not turned on');
            }

            permissions.requestPermission(
              permissions.READ_SMS,
              (status) => {
                console.log("invoking status");

              if(!status.hasPermission) {
                console.log("invoke !status.hasPermission");
                errorCallback();
              }
              else{
                console.log("invoking else part !status.hasPermission");
                this.getSMS();//this line through error
              }
            },
            errorCallback);
          }
        }

      });

    }, 10000);

Or you can save this to another variable before the function definition or use bind.

Suraj Rao
  • 28,186
  • 10
  • 88
  • 94