Using the Cordova API: SMS-Receive
The sms-receive plugin can be used to receive incoming sms messages on Android devices. It does not intercept them - the messages still go to the Messages app.
The messages will be received whenever your app is running, whether it is in the foreground or not.
Here is how to do it:
1. Add the plug in to PhoneGap configxml in Project Properties, by adding this line:
<plugin name="cordova-plugin-sms-receive" source="npm" />
2. Start watching for incoming text messages by executing this code: Basic:
Function Button1_onclick() SMSReceive.startWatch(startSuccess, startFail) End Function Function startSuccess() Textarea1.value = "watching started." End Function Function startFail() Textarea1.value = "watching start failed.." End Function
JavaScript
3. To stop listening, use this code: BASIC:
Function Button2_onclick() SMSReceive.stopWatch(stopSuccess, stopFail) End Function Function stopSuccess() Textarea1.value = "watching stopped." End Function Function stopFail() Textarea1.value = "watching stop failed." End Function
JavaScript
4. When a text message comes in, an onSMSArrive event is sent to your app. Here is how to handle it: BASIC
document.addEventListener("onSMSArrive", onSMSArrive) Function onSMSArrive(e) IncomingSMS = e.data; Textarea1.value = "SMS Received\r" Textarea1.value += "sms.address:" + IncomingSMS.address + "\r" Textarea1.value += "sms.body:" + IncomingSMS.body + "\r" Textarea1.value += JSON.stringify(IncomingSMS) End Function
JavaScript
More documentation on the plugin can be found here: https://github.com/andreszs/cordova-plugin-sms-receive