Send and Read the SMS content sent via native SMS app cordova

两盒软妹~` 提交于 2019-12-19 13:46:14

问题


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>


来源:https://stackoverflow.com/questions/26573172/send-and-read-the-sms-content-sent-via-native-sms-app-cordova

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!