check sms OTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// STEP 1: | |
// {{ install these command in CMD, turn off adb.exe process if you already run, turn off in Window Task Manager }} | |
// {{ Open Administrator CMD, Must be Administrator}} | |
// ionic cordova plugin add cordova-plugin-android-permissions | |
// ionic cordova plugin add cordova-plugin-sms | |
// yarn add @ionic-native/android-permissions | |
// yarn | |
// {{ run last command to check all node_modules }} | |
// STEP 2: add 'AndroidPermissions' in app.module.ts 'providers' | |
import { Component } from '@angular/core'; | |
import { AndroidPermissions } from '@ionic-native/android-permissions'; | |
declare var SMS:any; | |
@IonicPage() | |
@Component({ | |
selector: 'page-home', | |
templateUrl: 'home.html' | |
}) | |
export class HomePage { | |
constructor( | |
public navCtrl: NavController, | |
public androidPermissions: AndroidPermissions, | |
public platform:Platform | |
) { | |
this.getProducts(); | |
this.getProductNumber(); | |
} | |
ReadListSMS() { | |
this.platform.ready().then((readySource) => { | |
let filter = { | |
box : 'inbox', // 'inbox' (default), 'sent', 'draft' | |
indexFrom : 0, // start from index 0 | |
maxCount : 10, // count of SMS to return each time | |
}; | |
if(SMS) SMS.listSMS(filter, (ListSms)=>{ | |
console.log("Sms",ListSms); | |
}, | |
Error=>{ | |
console.log('error list sms: ' + Error); | |
}); | |
}); | |
if(SMS) SMS.startWatch(()=>{ | |
console.log('watching started'); | |
}, Error=> { | |
console.log('failed to start watching'); | |
}); | |
document.addEventListener('onSMSArrive', (e:any)=>{ | |
var sms = e.data; | |
console.log(sms); | |
}); | |
} | |
checkPermission() { | |
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.READ_SMS).then( | |
success => { | |
this.ReadListSMS(); | |
console.log('Permission granted') | |
}, | |
err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_SMS) | |
); | |
this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.READ_SMS]); | |
} | |
ionViewWillEnter() { | |
this.checkPermission() | |
} | |
} | |
ionViewWillEnter() { | |
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]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment