Reputation: 3762
I am following this ionic 4 article for cordova-plugin-sms-receive
but I am getting the error SMSReceive is not defined
. According to the article declare var SMSReceive: any; will make SMSReceive variable available for the page but I can't seem to get it to work. The aim is to read and extract SMS content. Any suggestions?
import { Component } from '@angular/core';
import { ToastController } from '@ionic/angular';
declare var SMSReceive: any;
@Component({
selector: 'app-tab1',
templateUrl: 'tab1.page.html',
styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
OTP: string = '';
showOTPInput: boolean = false;
OTPmessage: string = 'An OTP is sent to your number. You should receive it in 15 s'
constructor(private toastCtrl: ToastController) { }
async presentToast(message, show_button, position, duration) {
const toast = await this.toastCtrl.create({
message: message,
showCloseButton: show_button,
position: position,
duration: duration
});
toast.present();
}
next() {
this.showOTPInput = true;
this.start();
}
start() {
SMSReceive.startWatch(
() => {
document.addEventListener('onSMSArrive', (e: any) => {
var IncomingSMS = e.data;
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);
this.OTPmessage = 'OTP received. Proceed to register'
this.stop();
}
}
register() {
if (this.OTP != '') {
this.presentToast('You are successfully registered', false, 'top', 1500);
} else {
this.presentToast('Your OTP is not valid', false, 'bottom', 1500);
}
}
}
Upvotes: 0
Views: 1447
Reputation: 1871
First execute the following command
cordova plugin list
and check if cordova-plugin-sms-receive is there in the output. If it's not there then execute the following command
ionic cordova plugin add cordova-plugin-sms-receive
Upvotes: 1