Plugin to operate SMS, send / list / intercept / delete / restore.
Use the plugin with Cordova CLI:
cordova plugin add com.rjfun.cordova.sms
# create a demo project
cordova create test1 com.rjfun.test1 Test1
cd test1
cordova platform add android
# now add plugin
cordova plugin add com.rjfun.cordova.sms
# copy the demo file
rm -r www/*; cp plugins/com.rjfun.cordova.sms/test/* www/;
# now build and run the demo in your device or emulator
cordova prepare;
cordova run android;
# or import into Xcode / eclipse
sendSMS(address(s), text, successCallback, failureCallback);
listSMS(filter, successCallback, failureCallback);
deleteSMS(filter, successCallback, failureCallback);
startWatch(successCallback, failureCallback);
stopWatch(successCallback, failureCallback);
enableIntercept(on_off, successCallback, failureCallback);
restoreSMS(msg_or_msgs, successCallback, failureCallback);
setOptions(options, successCallback, failureCallback);
'onSMSArrive'
Check the [test/index.html] (https://github.com/floatinghotpot/cordova-plugin-sms/blob/master/test/index.html).
- sendSMS(address(s), text, successCallback, failureCallback);
Example Code:
if(SMS) SMS.sendSMS("+8613612345678", "hello, raymond", function(){}, function(){});
if(SMS) SMS.sendSMS(["+8613612345678", "+8613987654321"], "hello, raymond", function(){}, function(){});
- listSMS(filter, successCallback, failureCallback);
Example Code:
var filter = {
box : 'inbox', // 'inbox' (default), 'sent', 'draft', 'outbox', 'failed', 'queued', and '' for all
// following 4 filters should NOT be used together, they are OR relationship
read : 0, // 0 for unread SMS, 1 for SMS already read
_id : 1234, // specify the msg id
address : '+8613601234567', // sender's phone number
body : 'This is a test SMS', // content to match
// following 2 filters can be used to list page up/down
indexFrom : 0, // start from index 0
maxCount : 10, // count of SMS to return each time
};
if(SMS) SMS.listSMS(filter, function(data){
updateStatus('sms listed as json array');
updateData( JSON.stringify(data) );
if(Array.isArray(data)) {
for(var i in data) {
var sms = data[i];
}
}
}, function(err){
updateStatus('error list sms: ' + err);
});
- deleteSMS(filter, successCallback, failureCallback);
Warning: Be cautious when use this API.
var filter = {
box : 'inbox', // 'inbox' (default), 'sent', 'draft', 'outbox', 'failed', 'queued', and '' for all
// the following 4 filters are OR relationship
_id : 1234, // given a sms id, recommend ONLY use this filter
read : 1, // delete all read SMS
address : '+8613601234567', // delete all SMS from this phone number
body : 'Test is a test SMS' // delete SMS by content
};
if(SMS) SMS.deleteSMS(filter, function( n ){
updateStatus( n + ' sms messages deleted' );
}, function(err){
updateStatus('error delete sms: ' + err);
});
- startWatch(successCallback, failureCallback);
start observing and send following events to javascript:
- onSMSArrive
- onBluetoothConnected
- onBluetoothDisconnected
if(SMS) SMS.startWatch(function(){
update('watching', 'watching started');
}, function(){
updateStatus('failed to start watching');
});
- stopWatch(successCallback, failureCallback);
if(SMS) SMS.stopWatch(function(){
update('watching', 'watching stopped');
}, function(){
updateStatus('failed to stop watching');
});
When intercept in ON, other SMS app will not receive when SMS incoming.
- enableIntercept(on_off, successCallback, failureCallback);
var interceptEnabled = false;
function toggleIntercept() {
interceptEnabled = ! interceptEnabled;
if(interceptEnabled) { // clear the list before we start intercept
smsList.length = 0;
}
if(SMS) SMS.enableIntercept(interceptEnabled, function(){}, function(){});
$('span#intercept').text( 'intercept ' + (interceptEnabled ? 'ON' : 'OFF') );
$('button#enable_intercept').text( interceptEnabled ? 'Disable' : 'Enable' );
}
Restore the intercepted SMS into Inbox.
- restoreSMS(msg_or_msgs, successCallback, failureCallback);
var smsList = [];
// after some intercept
if(SMS) SMS.restoreSMS(smsList, function( n ){
// clear the list if restore successfully
smsList.length = 0;
updateStatus(n + ' sms messages restored');
}, function(err){
updateStatus('error restore sms: ' + err);
});
- setOptions( options, successCallback, failureCallback);
Example Code:
if(SMS) SMS.setOptions({
license: '[email protected]/xxxxxxxxx'
});
Triggered when a new SMS is incoming. Need call startWatch() first.
document.addEventListener('onSMSArrive', function(e){
var sms = e.data;
smsList.push( sms );
updateStatus('SMS arrived, count: ' + smsList.length );
// sms.address
// sms.body
var divdata = $('div#data');
divdata.html( divdata.html() + JSON.stringify( sms ) );
});
Cordova/PhoneGap plugins for the world leading Mobile Ad services:
- AdMob PluginPro, for Google AdMob/DoubleClick.
- iAd PluginPro, for Apple iAd.
- FacebookAds PluginPro, for Facebook Audience Network.
- FlurryAds PluginPro, for Flurry Ads.
- mMedia PluginPro, for Millennial Meida.
- MobFox PluginPro, for MobFox.
- MoPub PluginPro, for MoPub.
More Cordova/PhoneGap plugins by Raymond Xie, visit http://rjfun.github.io/.
Project outsourcing and consulting service is also available. Please contact us if you have the business needs.