读取SMS以自动在ionic4中无法正常工作

问题描述 投票:1回答:1

我使用this插件读取OTP的SMS。我要通过手机号码发送SMS,然后在下一个屏幕上,我需要从SMS读取OTP号码,然后单击Validate Pin。但是以某种方式,代码没有执行此事件document.addEventListener('onSMSArrive', (e: any) => {

然后卡在这里。

我也在共享代码。

declare var SMSReceive: any;

ngOnInit() {
    this.next();
  }

  OTP: string = '';
  OTPmessage: string = 'An OTP is sent to your number. You should receive it in 15 s'

  next() {
    this.start();
  }

  start() {
    alert("start");
    SMSReceive.startWatch(
      () => {
        console.log('watch started');
        document.addEventListener('onSMSArrive', (e: any) => {
          console.log('onSMSArrive()');
          alert('onSMSArrive()');
          var IncomingSMS = e.data;
          console.log('sms.address:' + IncomingSMS.address);
          console.log('sms.body:' + IncomingSMS.body);
          alert('sms.address:' + IncomingSMS.address);
          alert('sms.body:' + IncomingSMS.body);
          /* Debug received SMS content (JSON) */
          console.log(JSON.stringify(IncomingSMS));
          alert(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);
      alert(this.OTP);
      this.OTPmessage = 'OTP received. Proceed to register'
      this.stop();
    }
  }

但是代码未执行addEventListener事件。

出现此错误。

SMSReceive未定义;区域:任务:Promise.then;值:ReferenceError:未定义SMSReceive

angular ionic-framework ionic3 ionic4 hybrid-mobile-app
1个回答
0
投票
  • 声明像这样的SMSReceive插入了SMSReceive
  • 存储在全局变量this.SMSReceive =(SMSReceive)中
© www.soinside.com 2019 - 2024. All rights reserved.