Ionic Cordova Plugin To Read SMS Messages

Saturday, 31 December 2016

This article is for Ionic V1 for current version of ionic Please Read this article working on Android visit Ionic 3 Cordova Read SMS plugin .

In Ionic Framework to read SMS messages from device you can use cordova plugin.
So, first of all install cordova SMS plugin in your application.


cordova plugin add cordova-plugin-sms

You can use these following methods.


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);

setOptions(options, successCallback, failureCallback);

To Read List of SMS:-




angular.module('starter', ['ionic','ngCordova'])

.run(function($ionicPlatform) {

 $ionicPlatform.ready(function() {

  var filter = {
       box : 'inbox', // 'inbox' (default), 'sent', 'draft'
       indexFrom : 0, // start from index 0
       maxCount : 10, // count of SMS to return each time
            };

       if(SMS) SMS.listSMS(filter, function(data){

           console.log(data);
          },

          function(err){
          console.log('error list sms: ' + err);
          });
    
  });
    
 })

Make sure always use method in Ionic Framework inside the $ionicPlatform.ready(function() {}; else it will not work.

To Read SMS on Arrive:-

First startWatch() watching for new messages the and when new message arrive onSMSArrive event is triggered. To read OTP SMS you can use this feature.



angular.module('starter', ['ionic','ngCordova'])

.run(function($ionicPlatform) {

  $ionicPlatform.ready(function() {

   if(SMS) SMS.startWatch(function(){
           console.log('watching started');
        }, function(){
       console.log('failed to start watching');
   });

  document.addEventListener('onSMSArrive', function(e){
       var sms = e.data;
       console.log(sms);

       });
  });
        
})


To Send SMS-


angular.module('starter', ['ionic','ngCordova'])

.run(function($ionicPlatform) {

 $ionicPlatform.ready(function() {

 if(SMS) SMS.sendSMS("+9112345","msg",function(){
   console.log("Sent");
  }, function(e){
   console.log("Error occurs")
  });
  });
        
})

Reference is taken from gitHub https://github.com/floatinghotpot/cordova-plugin-sms/tree/master/docs

This article is for ionic V1 if you are looking for Ionic 3 please visit this link http://www.programmingworldtech.com/2017/09/ionic-3-cordova-read-sms-plugin.htmlIf you are facing any problem comment below.Thank You.


19 comments

  1. hi, how to use enableIntercept?

    I tried, but the sms is still go native app

    ReplyDelete
  2. var interceptEnabled = true;


    if(SMS) SMS.enableIntercept(interceptEnabled, function(){
    console.log("Enabled");
    }, function(e){
    console.log("error");
    });
    for Enable or Disable intercept set interceptEnabled value true or false.

    ReplyDelete
    Replies
    1. I tried, but the sms is still go native app. ( on android 6.0.1 )

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. the plugin is working fine with android but nothing is happening with ios app

    ReplyDelete
  5. Please help me I am using redmi prime 3s (android 6.0.1) but Not working sms receive

    ReplyDelete
    Replies
    1. You need to allow permission for read sms read this article for current version of ionic http://www.programmingworldtech.com/2017/09/ionic-3-cordova-read-sms-plugin.html

      Delete
  6. Replies
    1. make sure call the function inside the Platform ready.

      Delete
  7. If i start ionic project like "ionic start project_name blank' and write code into file 'home.ts' how i should to import variables angular and SMS?

    ReplyDelete
    Replies
    1. Hii Pomah, this code sample is for Ionic v1. For current version of ionic you can add ionic native sms plug-in. See here https://ionicframework.com/docs/native/sms/

      Delete
    2. But there read sms method doesnt exist

      Delete
    3. http://www.programmingworldtech.com/2017/09/ionic-3-cordova-read-sms-plugin.html Read this article working for android

      Delete
  8. How to use deleteSMS? it doesn't delete messages on inbox of my phone

    ReplyDelete
    Replies
    1. Hi, To delete SMS first you need to enable android WRITE_SMS permission.
      To enable WRITE_SMS permission first add cordova-plugin-android-permissions.

      Thank You.

      Delete
  9. Thank you for your reply. I have checked my permission using the said plugin. I do have permission on WRITE_SMS. It is also said in my log that the delete is successful. However it doesn't delete anything on my inbox.

    ReplyDelete
    Replies
    1. Hi, DAR
      This will help you https://github.com/floatinghotpot/cordova-plugin-sms/issues/67

      Delete
    2. delete function has been removed for android 5.0+ based on the issue. Anyways, Thanks for your help Nandan. :)

      Delete

Related Posts Plugin for WordPress, Blogger...
Related Posts Plugin for WordPress, Blogger...
 

Most Reading

Labels