Send and Read the SMS content sent via native SMS

2019-02-21 13:33发布

问题:

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?

Thanks in advance.

回答1:

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.



回答2:

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


回答3:

Are you saying you want to know what the user typed in another app? If so, I don't believe you can. In theory you can try skipping the real SMS app and using a plugin: http://plugins.cordova.io/#/package/com.jsmobile.plugins.sms



回答4:

Nowadays there are ionic wrappers for cordova extensions. You can use $cordovaSMS (docs here)

module.controller('ThisCtrl', function($cordovaSms) {

 document.addEventListener("deviceready", function () {

    $cordovaSms
      .send('phonenumber', 'SMS content', options)
      .then(function() {
        // Success! SMS was sent
      }, function(error) {
        // An error occurred
      });

  });

});

To see what the user is typing you can have them write the 'SMS content' inside your app. One you leave your app I don't think you can grab text from the native SMS field



回答5:

you can use sms: url for opening native sms app for that you need to add
<access origin="sms:*" launch-external="yes"/> inside config.xml

after adding above line use sms link as follows

<a class="button icon-left ion-android-mail button-balanced button-outline" href="sms:9808350855">SMS</a>