[英]read SMS for automatically in ionic4 not working
我使用這個插件來讀取 OTP 的 SMS。 我在手機號碼上發送短信,然后下一個屏幕我需要從短信中讀取 OTP 號碼並需要點擊驗證密碼。 但不知何故,代碼沒有執行這個事件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 未定義
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.