Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send and read the SMS content sent via native SMS app in Cordova

I'm building an Ionic/Cordova app. From my application, user can click on a phone number in order to send SMS. When click on that number, native SMS app will open and then user can type his/her own SMS and send the SMS. Is there any way, from my app I can get the SMS content?

like image 506
Rashidul Islam Avatar asked Oct 26 '14 12:10

Rashidul Islam


2 Answers

As you are using ionic/cordova, a SMS plugin is required to do such work.

Besides jsmobile SMS plugin, there is another one with more APIs: http://plugins.cordova.io/#/package/com.rjfun.cordova.sms

As mentioned in the description:

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);

Events

'onSMSArrive'

You can get the SMS content in either of the following ways:

  1. implement the UI, get user's input, and call plugin's API to send the SMS.

Or,

  1. let the default SMS app send the message, then read the SMS from the sent box.

BTW, the plugin works for Android only, FYI.

like image 101
MikeX Avatar answered Nov 15 '22 16:11

MikeX


There is cordova-plugin-sms option, as @MikeX suggested, but notice that its license is not free use.

So I chose using Phonegap-SMS-reception-plugin, which is MIT license.

and then it just

 var smsInboxPlugin = cordova.require('cordova/plugin/smsinboxplugin');
  smsInboxPlugin.startReception (function(msg) {
     alert(msg);
     stopReadingSMS(smsInboxPlugin);
   }, function() {
     alert("Error while receiving messages");
     stopReadingSMS(smsInboxPlugin);
   });

function stopReadingSMS(smsInboxPlugin) {
    smsInboxPlugin.stopReception (function() {
        console.log("Correctly stopped SMS receiver");
    }, function() {
        console.log("Error while stopping the SMS receiver");
    });
}
like image 43
yishaiz Avatar answered Nov 15 '22 15:11

yishaiz