实现点击可以跳转到手机自带的短信应用界面来收发短信

时间:2024-02-22 16:19:54

首先用到的插件名字:cordova-sms-plugin

  • 在Cordova环境下安装cordova-sms-plugin插件:
  1. 第一种方式:cordova plugin add https://github.com/cordova-sms/cordova-sms-plugin.git
  2. 第二种方式:ionic plugin add cordova-sms-plugin

这2种方式哪一种都可以。

  • app.js中注入ngCordova           controller.js中注入$cordovaSms
var App = angular.module(\'starter\', [\'ionic\', \'ngCordova\',\'LocalStorageModule\',\'ionic-datepicker\',\'ngImgCrop\',\'gdi2290.md5\'])
  .config( [\'$compileProvider\', function( $compileProvider ) {
    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|tel|file|sms):/);
  }
])

需要发短信的controller中引入$cordovaSms

App.controller(\'MemberDataController\', [\'MemberDataService\',\'$scope\',\'$stateParams\',\'$state\',\'Storage\',\'$cordovaSms\',\'warningForm\',
    function (MemberDataService,$scope,$stateParams,$state,Storage,$cordovaSms,warningForm) {
  • 调用方法,发送短信
        //打开发送短信的popup
        $scope.openSendMessage = function (phonenumber) {
            console.log("发送消息:" + phonenumber);
            var options = {
                replaceLineBreaks: false, // true to replace \n by a new line, false by default
                android: {
                    intent: \'INTENT\' // send SMS with the native android SMS messaging
                    //intent: \'\' // send SMS without open any other app
                }
            };
            sms.send(phonenumber, \'内容\', options)
        };