1

I used the this plugin to read the SMS for OTP. I am sending the SMS on the mobile number and then next screen I need to read the OTP number from the SMS and need to click on the Validate Pin. But somehow the code is not executing this event document.addEventListener('onSMSArrive', (e: any) => {

and then stuck here.

I am sharing the code also.

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();
    }
  }

but the code is not executing addEventListener event.

Getting this error.

SMSReceive is not defined ; Zone: ; Task: Promise.then ; Value: ReferenceError: SMSReceive is not defined

Question Warriors
  • 117
  • 1
  • 1
  • 12

1 Answers1

0