自动读取OTP

时间:2019-08-01 07:26:13

标签: angular ionic-framework ionic4

我是ionic4 / angular的新手。我需要将OTP自动输入OTP字段。该消息看起来像这样******(OTP) is your one-time password。验证OTP和所有操作已经由另一组代码处理。我只需要将此功能添加到应用中:

ionViewWillEnter() {
    this.status = false;
    this.menuCtrl.enable(false);
    this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.READ_SMS).then(
      success => console.log('Permission granted'),
    err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_SMS)
    );
    
    this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.READ_SMS]);
    
    SMSReceive.startWatch(
      () => {
        console.log('watch started');
        document.addEventListener('onSMSArrive', (e: any) => {
          console.log('onSMSArrive()');
          let IncomingSMS = e.data;
          console.log('sms.address:' + IncomingSMS.address);
          console.log('sms.body:' + IncomingSMS.body);
          /* Debug received SMS content (JSON) */
          console.log(JSON.stringify(IncomingSMS));
          this.processSMS(IncomingSMS);
        });
      },
      () => { console.log('watch start failed') }
    )
  }
  stop(){
    SMSReceive.stopWatch(
      () => { console.log('watch stopped') },
      () => { console.log('watch stop failed') }
    )
  }

  processSMS(data) {
    // Check SMS for a specific string sequence to identify it is you SMS
    // Design your SMS in a way so you can identify the OTP quickly i.e. first 6 letters
    // In this case, I am keeping the first 6 letters as OTP
    const message = data.body;
    if (message && message.indexOf('enappd_starters') != -1) {
      this.otp = data.body.slice(0, 6);
      console.log(this.otp);
      // this.OTPmessage = 'OTP received. Proceed to register'
      this.stop();
    }
  }
 <ion-item class="item" id="otp">
                        <ion-icon id=move name="lock" style="color:#ffbf00;" slot="start"></ion-icon>
                        <ion-label position="floating">OTP</ion-label>
                        <ion-input type="tel" #b id="movefocus" ngModel name="otp" minlength="6" maxlength="6"
                          pattern="^\d+$" #otpCtrl="ngModel" style="caret-color: #ffbf00;" tabindex="2" required>
                        </ion-input>

0 个答案:

没有答案