发送两个或更多短信

时间:2017-04-18 11:05:21

标签: javascript android angularjs cordova ionic-framework

我正在使用Cordova SMS插件从使用Ionic开发的应用程序发送消息。当“friends”数组的长度= 1时,将发送SMS。但是当你有更多时,有时消息不会到达,或者更糟糕的是,相同的数字会得到两条消息。这段代码有问题吗? 代码中有问题的部分如下:

$rootScope.showLoading('Enviando mensagen(s)!');

var loopPromises = [];

$rootScope.friends = [{
            nome: 'David',
            telefone: '83777777777'
        }, {
            nome: 'Edvan',
            telefone: '83444444444'
        }, {
            nome: 'Débora',
            telefone: '83888888888'
        }]

var interno = {
      nome: "#App",
      telefone: "83222222222"
}
$rootScope.friends.push(interno)

angular.forEach($rootScope.friends, function (a) {
  var deferred = $q.defer();
  loopPromises.push(deferred.promise);
  var texto = '';
  if (a.telefone == "83222222222") {
    texto = "Internal report"
    $rootScope.friends.pop();
  } else {
    texto = "Hello! I'm here!"
  }

  $cordovaSms.send(a.telefone, texto)
    .then(deferred.resolve, deferred.reject);
});

$q.all(loopPromises).then(function (results) {
  $rootScope.hideLoading();
  $rootScope.showToast('Messages sent!');
  }, function (errors) {
  $rootScope.hideLoading();
  $rootScope.showToast('Some message was not sent!');
});

1 个答案:

答案 0 :(得分:0)

config.xml

上添加以下代码
<access origin="sms:*" launch-external="yes"/>

我希望这会有所帮助。

进行一些更改
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
        }
    };

for(var j=0;j<$rootScope.friends.length;j++){

    var texto = '';
    if (a.telefone == "83222222222") {
         texto = "Internal report"
         $rootScope.friends.pop();
    } else {
     texto = "Hello! I'm here!"
    }

    $cordovaSms
    .send($rootScope.friends[0].telefone, texto', options)
    .then(function() {
         console.log('Success');
     }, function(error) {
         alert('Not Sent'); // An error occurred
     });
}