Cordova plugin to retrieve SMS in Android using the SMS Retriever API.
npm install cordova-plugin-sms-retriever!npm !npm !GitHub package.json version !GitHub code size in bytes !GitHub top language !GitHub !GitHub last commit
xml
...
...
`
Installation
- Install stable from NPM:
`bash
cordova plugin add cordova-plugin-sms-retriever
`
- Install master from GitHub:
`bash
cordova plugin add https://github.com/andreszs/cordova-plugin-sms-retriever
`
- Create your project and Android app in Firebase Console
- Download the google-services.json file into your platforms/android folder.
- Make sure to sign your build with a keystore file.
Ionic
$3
`bash
npm i awesome-cordova-plugins-sms-retriever-api
`
$3
`bash
npm i @awesome-cordova-plugins/sms-retriever-api
`
Ionic Demo
https://github.com/MaximBelov/cordova-plugin-sms-retriever-lab
Methods
getPhoneNumber
Opens a dialog to select your mobile numbers saved in phone and returns selected phone number.
`javascript
var onSuccess = function (strSuccess) {
console.log(strSuccess);
};
var onFail = function (strError) {
console.log(strError);
};
cordova.plugins.SMSRetriever.getPhoneNumber(onSuccess, onFail);
`
startWatch
Start listening for a single incoming verification SMS for 5 minutes.
`javascript
cordova.plugins.SMSRetriever.startWatch(successCallback, errorCallback);
`
:warning: Method moved from window to cordova.plugins object in version 2.0.0
- When a valid SMS is intercepted, the onSMSArrive event is fired and SMS watching is stopped.
- When the 5 minutes timeout is reached, SMS watching is stopped and the failureCallback returns TIMEOUT.
$3
- SMS_RETRIEVER_STARTED: Retriever started and waiting for incoming SMS.
- SMS_RETRIEVER_ALREADY_STARTED: Your 5 minutes for SMS retrieval are already running and won’t be reset by calling this method again!.
- SMS_RETRIEVER_DONE: Second callback, triggered when an SMS was intercepted.
When the SMS is returned, the retriever API is automatically stopped and no further messages will be intercepted until you start a new one. This is by API design, not a plugin or a demo app restriction.
$3
`javascript
var onSuccess = function (strSuccess) {
console.log(strSuccess);
};
var onFail = function (strError) {
console.log(strError);
};
cordova.plugins.SMSRetriever.startWatch(onSuccess, onFail);
`
stopWatch
Stops listening for a single incoming verification SMS
$3
- SMS_RETRIEVER_DONE
$3
`javascript
var onSuccess = function (strSuccess) {
console.log(strSuccess);
};
var onFail = function (strError) {
console.log(strError);
};
cordova.plugins.SMSRetriever.stopWatch(onSuccess, onFail);
`
getHashString
Get the 11-character hash string for your app using the AppSignatureHelper class. This string must be appended to the SMS received in order for the API to read this message.
`javascript
cordova.plugins.SMSRetriever.getHashString(successCallback, errorCallback);
`
:warning: Method moved from window to cordova.plugins object in version 2.0.0
$3
- The hash will be different from debug and release builds, since they have different signatures.
- Play Store now re-signs signed APKs on upload. This will most certainly change the hash string.
- Calling this method with an active SMS retriever running will void the retriever and the SMS wont be incercepted.
- Google advices against dynamically retrieving your hash code before sending the SMS:
> Do not use hash strings dynamically computed on the client in your verification messages.
Therefore, do not invoke this method from the published app. The hash is the same for all users, and bound to your keystore signing keys, so you can get it once and never again call this method.
$3
- The 11-digit hash string for sending validation SMS.
$3
`javascript
var onSuccess = function (strHash) {
console.log(strHash);
};
var onFail = function (strError) {
console.log(strError);
};
cordova.plugins.SMSRetriever.getHashString(onSuccess , onFail);
`
Events
onSMSArrive
Event fired when a valid verification SMS with the hash string has arrived. You need call startWatch() first.
$3
- If the SMS is not retrieved in your debug build, try the signed production APK.
$3
`javascript
document.addEventListener('onSMSArrive', function(args) {
// SMS retrieved, get its contents
console.info(args.message);
// To Do: Extract the received one-time code and verify it on your server
});
``