Using the Cordova API: SMS-Receive: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
(Created page with "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 w...")
 
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
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.
[[File:Receivesms.png]]
 
The sms-receive plugin can be used to receive incoming SMS messages (text messages) on Android devices. It does not intercept them - the messages still go to the Messages app. It only works if your app is compiled using VoltBuilder.


The messages will be received whenever your app is running, whether it is in the foreground or not.
The messages will be received whenever your app is running, whether it is in the foreground or not.
Line 5: Line 7:
Here is how to do it:
Here is how to do it:


1. Add the plug in to PhoneGap configxml in Project Properties, by adding this line:
1. Add the plugin to configxml in Project Properties, by adding this line:
<pre>
<pre>
<plugin name="cordova-plugin-sms-receive" source="npm" />
<plugin name="cordova-plugin-sms-receive" source="npm" />
Line 11: Line 13:


2. Start watching for incoming text messages by executing this code:
2. Start watching for incoming text messages by executing this code:
Basic:
 
BASIC
<pre>
<pre>
Function Button1_onclick()
Function Button1_onclick()
Line 27: Line 30:
JavaScript
JavaScript
<pre>
<pre>
Button1.onclick = function() {
  SMSReceive.startWatch(startSuccess, startFail);
};
function startSuccess() {
  Textarea1.value = "watching started.";
}
function startFail() {
  Textarea1.value = "watching start failed..";
}
</pre>
</pre>


3. To stop listening, use this code:
3. To stop watching, use the following code. It saves memory and battery life to turn this off.
BASIC:
 
BASIC
<pre>
<pre>
Function Button2_onclick()
Function Button2_onclick()
Line 46: Line 61:
JavaScript
JavaScript
<pre>
<pre>
Button2.onclick = function() {
  SMSReceive.stopWatch(stopSuccess, stopFail);
};
function stopSuccess() {
  Textarea1.value = "watching stopped.";
}
function stopFail() {
  Textarea1.value = "watching stop failed.";
}
</pre>
</pre>


4. When a text message comes in, an onSMSArrive event is sent to your app. Here is how to handle it:
4. When a text message comes in, an onSMSArrive event is sent to your app. Here is how to handle it:
BASIC
BASIC
<pre>
<pre>
Line 56: Line 83:
   IncomingSMS = e.data;
   IncomingSMS = e.data;
    
    
   Textarea1.value = "SMS Received\r"
   Textarea1.value = "SMS Received" + vbCRLF
   Textarea1.value += "sms.address:" + IncomingSMS.address + "\r"
   Textarea1.value += "sms.address:" + IncomingSMS.address + vbCRLF
   Textarea1.value += "sms.body:" + IncomingSMS.body + "\r"
   Textarea1.value += "sms.body:" + IncomingSMS.body + vbCRLF
   Textarea1.value += JSON.stringify(IncomingSMS)
   Textarea1.value += JSON.stringify(IncomingSMS)
End Function
End Function
Line 64: Line 91:
JavaScript
JavaScript
<pre>
<pre>
document.addEventListener("onSMSArrive" , onSMSArrive);
function onSMSArrive(e) {
  IncomingSMS = e.data;
  Textarea1.value = "SMS Received" & '\n';
  Textarea1.value += "sms.address:" & IncomingSMS.address & '\n';
  Textarea1.value += "sms.body:" & IncomingSMS.body & '\n';
  Textarea1.value += JSON.stringify(IncomingSMS);
}
</pre>
</pre>


More documentation on the plugin can be found here:
More documentation on the plugin can be found here:
https://github.com/andreszs/cordova-plugin-sms-receive
https://github.com/andreszs/cordova-plugin-sms-receive

Latest revision as of 16:30, 10 November 2020

The sms-receive plugin can be used to receive incoming SMS messages (text messages) on Android devices. It does not intercept them - the messages still go to the Messages app. It only works if your app is compiled using VoltBuilder.

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 plugin to 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

Button1.onclick = function() {
  SMSReceive.startWatch(startSuccess, startFail);
};

function startSuccess() {
  Textarea1.value = "watching started.";
}

function startFail() {
  Textarea1.value = "watching start failed..";
}

3. To stop watching, use the following code. It saves memory and battery life to turn this off.

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

Button2.onclick = function() {
  SMSReceive.stopWatch(stopSuccess, stopFail);
};

function stopSuccess() {
  Textarea1.value = "watching stopped.";
}

function stopFail() {
  Textarea1.value = "watching stop failed.";
}

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" + vbCRLF
  Textarea1.value += "sms.address:" + IncomingSMS.address  + vbCRLF
  Textarea1.value += "sms.body:" + IncomingSMS.body + vbCRLF
  Textarea1.value += JSON.stringify(IncomingSMS)
End Function

JavaScript

document.addEventListener("onSMSArrive" , onSMSArrive);

function onSMSArrive(e) {
  IncomingSMS = e.data;

  Textarea1.value = "SMS Received" & '\n';
  Textarea1.value += "sms.address:" & IncomingSMS.address & '\n';
  Textarea1.value += "sms.body:" & IncomingSMS.body & '\n';
  Textarea1.value += JSON.stringify(IncomingSMS);
}

More documentation on the plugin can be found here: https://github.com/andreszs/cordova-plugin-sms-receive