2017-04-18 42 views
0

我正在使用Cordova SMS插件从用Ionic开发的应用程序发送消息。当“friends”数组长度= 1时,发送SMS。但是当你有更多的时候,有时候这些信息不会到达,或者更糟糕的是,同样的数字会得到两条消息。这段代码有什么问题吗? 代码的问题部分如下:发送两个或多个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!'); 
}); 

回答

0

添加以下代码对你config.xml中

<access origin="sms:*" launch-external="yes"/> 

我希望这有助于。

OR

做出一些改变上

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 
    }); 
} 
+0

嗯...仍然没有工作... :-( –

+0

我已经加入申请选项,并做了一些变更,请再试一次让我知道 –

+0

我也试过了,没有工作。 –