This Cordova Android plugin allows you to receive incoming SMS. You have the possibility to stop the message broadcasting and, thus, avoid the incoming message native popup.
cordova plugin add cordova-plugin-sms-receiver --save
Check if the SMS technology is supported by the device.
SmsReceiver.isSupported((supported) => {
if (supported) {
alert("SMS supported!")
} else {
alert("SMS not supported")
}
}, () => {
alert("Error while checking the SMS support")
})
Start the SMS receiver waiting for incoming message The success callback function will be called everytime a new message is received. The given parameter is the received message formatted such as: phoneNumber>message (Example: +32472345678>Hello World) The error callback is called if an error occurs.
Example:
SmsReceiver.startReception(({messageBody, originatingAddress}) => {
alert(messageBody)
}, () => {
alert("Error while receiving messages")
})
Stop the SMS receiver
Example:
SmsReceiver.stopReception(() => {
alert("Correctly stopped")
}, () => {
alert("Error while stopping the SMS receiver")
})
If you abort the broadcast using this plugin (see broadcast
boolean variable
in the name.ratson.cordova.sms_receiver.SmsReceiver
), the SMS will not be broadcast to other
applications like the native SMS app. So ... be careful !
A good way to manage this is to stop the SMS reception when the onPause event is fired and, when the onResume event is fired, restart the reception.