0

I am developing an Android application with Ionic, I want to implement SMS function in my app. I've installed cordova-sms-plugin and added the following code to my app.

.controller('requestCtrl', function($scope,AuthService,RequestService,$cordovaSms) {

 $scope.sendsms = function(){
  document.addEventListener("deviceready",function(){
    var options = {
      replaceLineBreaks: false,
      android:{
        //intent : 'INTENT',
        intent : ''
      }
    };
    $cordovaSms.send('9487354083','This is a test sms', options).then(function(){
      alert("success! sms was sent");
    },function(error){
      console.log(error);
      alert('Error sending sms!');
    });
  });
}

  }) 

In the above code, while I use intent: 'INTENT' option I am able to send SMS with default SMS app of my device. But when I use intent: '' option I can't able to send SMS within my app. The Error message is displayed in the device.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nishanth
  • 47
  • 5
  • Can you give us the error message please (the console.log) ? See http://stackoverflow.com/a/30234049/2549619 if you don't know how to do it. – e666 Oct 14 '16 at 18:16
  • Hi Nishanth. Please do not restore your old question unless I have misrepresented any part of it. Edits to fix spelling, case and to trim chat/begging are valid on this site, and should be kept. We do ask also that posters refrain from asking for urgency - it is not well received here, since it is not an appropriate way to address volunteers. – halfer Oct 14 '16 at 20:38
  • the console.log doesn't throw any error in browser. while running the app in device. the error function I've just written shows up. – Nishanth Oct 15 '16 at 02:23
  • The console.log() doesn't show any error message in browser. while i test the app in device the error function alone executes. but for sending SMS with INTENT, the code works fine. the SMS app of the device opens, and i could send SMS through it. But this is not actually the case. i want to send SMS without any INTENT I.e through my app itself – Nishanth Oct 15 '16 at 02:31

1 Answers1

0

This code runs perfectly on both my iPhone and Galaxy S4. It's inside of my controller, and gets the student object and message from the UI. It should be pretty straight-forward.

   function sendMessageToParent(student, message) {
        $cordovaSMS.send(student.phone, message)
          .then(function () {
            $log.log('Message sent.');
      });
    }

Notice I'm not setting any options at all. I'm using the Ionic-Native SMS library through their bower package with Ionic 1.3. http://ionicframework.com/docs/v2/native/sms/ and this plugin, which appears to the same one you're using: https://github.com/cordova-sms/cordova-sms-plugin

MikeC
  • 441
  • 3
  • 8
  • so, the options part is not really necessary? does your code send SMS without opening any intent? or it uses SMS app of the device to send SMS? – Nishanth Oct 15 '16 at 02:27
  • The options might be necessary in some circumstances, or perhaps Ionic-native sets some sensible defaults. The code I posted definitely sends the SMS message without opening another app (on Android). – MikeC Oct 15 '16 at 22:25
  • what if i have two sim in my device, which number does my app uses to send message? does this SMS function related to any GSM services? – Nishanth Oct 16 '16 at 11:54
  • That's an interesting question. Unfortunately, I do not have any devices like that and would have no way to test it. – MikeC Oct 17 '16 at 14:33