sandeepdillerao / cordova-plugin-otp-auto-verification

13 stars 10 forks source link

Integrating with ionic 2 #3

Open jfreak opened 7 years ago

jfreak commented 7 years ago

Can you guide on how to integrate with ionic 2?

sandeepdillerao commented 7 years ago

@jfreak sorry i am not familiar with ionic 2 but i thing plugins are same for ionic 1 and 2 ionic 1 plugin work with ionic 2 also wihtout any change.. just try only controller logic will change according to ionic 2 coding standards

jfreak commented 7 years ago

I have tried the necessary changes for ionic 2 but when ever a new sms arrives with set options the event or function is not fired..

aitckamal commented 7 years ago

Hi guys,

Can you confirm me how to inject this module in ionic 2 project. I've installed this plugin by : ionic plugin add cordova-plugin-otp-auto-verification and also added it as node module in project by: npm i cordova-plugin-otp-auto-verification

Currently i am getting this error : Cannot find module "cordova-plugin-otp-auto-verification".

Any help would be appreciated.

Thanks Kamal

jfreak commented 7 years ago

@aitckamal I have tried varies way of implementing this module but failed.. i never had any issues in injecting this module, may be this plugin works better in cordova but not in ionic 2. I am still trying to find an optimal solution to make this plugin work. So for now you can use this plugin to read otp from sms inbox https://github.com/floatinghotpot/cordova-plugin-sms and here is my code how i implemented this plugin https://github.com/jfreak/ionic-2-read-otp

aitckamal commented 7 years ago

Thanks for your reply @jfreak. But its not worked for me. I tried as you did in your code but document.addEventListener('onSMSArrive', this.smsArived); is not listening onSMSArrive Event. So not able to read received SMS. And it is clearly mentioned that You can't receive SMS via this plugin on https://github.com/cordova-sms/cordova-sms-plugin#how-can-i-receive-sms

And i also didn't found any function for read SMS in Ionic native plugin https://ionicframework.com/docs/native/sms/

jfreak commented 7 years ago

@aitckamal Yeah i agree with you that the plugin cannot receive SMS. But in this code you will able to read the current sms by setting up some delimiters. And i think you missed one thing.. try accepting SMS permissions for your phone for the current App. Go to the app setting for the current ionic 2 app and give permission for reading SMS.

aitckamal commented 7 years ago

Thanks @jfreak. Now it works fine for me. It was permission level issue. But why it does not ask for permission if permission is not given. And how can i resolve this issue. Thank you so much for helping me.

jfreak commented 7 years ago

@aitckamal Use this to ask user the Permission in run time. https://ionicframework.com/docs/native/android-permissions/

aitckamal commented 7 years ago

Thanks @jfreak. It really helpful.

sandeepdillerao commented 7 years ago

@jfreak can you send me code snippet so that it will help me to make this plugin compatible with ionic 2

jfreak commented 7 years ago

@sandeepdillerao this is what i did using cordova-plugin-sms Plugin

if(window.SMS) window.SMS.startWatch(function(){
//To start sms watch listener
      }, function(){
        console.log('failed to start watching');
      });
//Event Listener
 document.addEventListener('onSMSArrive', this.smsArived);
 smsArived = (result: any) => {
 let smsData = result.data;
}

I am trying your code with the latest version.. I'll update you if i find any solutions for it!

sandeepdillerao commented 7 years ago

@jfreak please install plugin using gitlab url not using npm repository bzc on npm i have not updated code

jfreak commented 7 years ago

@sandeepdillerao ill try that

abhilashWipadika commented 6 years ago

hello @jfreak,@sandeeodillerao and @aitckamal , can u please tell me how to integrate otp-auto verification in ionic 2. any help would be very appreciatable.

aitckamal commented 6 years ago

@abhilashWipadika, you can follow these steps to integrate otp-auto verification in ionic 2 :-

  1. Install otp-auto-verification Plugin

ionic plugin add cordova-plugin-otp-auto-verification npm install --save cordova-plugin-otp-auto-verification

  1. declare a global variable

declare var window: any;

  1. Call watchSMS() after sendOTP Functionality done
  watchSMS() {
    let _scope = this;
    if(window.SMS) window.SMS.startWatch(function(){
        console.log('Succeed to start watching SMS');
        document.addEventListener('onSMSArrive', _scope.smsArived);
      }, function(){
        console.log('failed to start watching SMS');
      });
  }

  stopWatchSMS() {
    if(window.SMS) window.SMS.stopWatch(function(){
        console.log('Succeed to stop watching SMS');
      }, function(){
        console.log('failed to stop watching SMS');
      });
  }

  smsArived = (result: any) => {
    let sms = result.data;
    console.log('sms', sms);
    let sender = sms.address;
    console.log('sender', sender);
    let otp_text = sms.body;
    console.log('OTP', otp_text);
    this.stopWatchSMS();
  }
  1. Before calling watchSMS, Make sure you've permission to read SMS.

AndroidPermissions.PERMISSION.READ_SMS should be granted for this process. You can use Android Permission Plugin to request for READ_SMS Permission.

https://ionicframework.com/docs/native/android-permissions/

snkhan commented 6 years ago

watchSMS() { let _scope = this; if(window.SMS) window.SMS.startWatch(function(){ console.log('Succeed to start watching SMS'); document.addEventListener('onSMSArrive', _scope.smsArived); }, function(){ console.log('failed to start watching SMS'); }); }

stopWatchSMS() { if(window.SMS) window.SMS.stopWatch(function(){ console.log('Succeed to stop watching SMS'); }, function(){ console.log('failed to stop watching SMS'); }); }

smsArived = (result: any) => { let sms = result.data; console.log('sms', sms); let sender = sms.address; console.log('sender', sender); let otp_text = sms.body; console.log('OTP', otp_text); this.stopWatchSMS(); }

using the above code for reading otp from SMS for ionic 3 getting below error can any one help

file:///android_asset/www/cordova.js: Line 312 : Error in Success callbackId: SMS329868448 : TypeError: Cannot read property 'smsArived' of null I/chromium: [INFO:CONSOLE(312)] "Error in Success callbackId: SMS329868448 : TypeError: Cannot read property 'smsArived' of null",, source: file:///android_asset/www/cordova.js

abhinpai commented 6 years ago

document.addEventListener('onSMSArrive', this.smsArived); declare this callback method in the constructor, that would work