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.


Angularjs Use Filter On Search Bar for an Array List of Items

This article helps you to create a search bar using angularjs and search items from the list of array items.

First open your app.js file and create a list of array items. According to given below example.

angular.module('myapp', [])
.controller("myCtrl",function($scope){

 $scope.items=[
 {
  name: "Amit",
  address:"Delhi",
  id:1235
 },
 {
  name: "Yovraj",
  address:"Mumbai",
  id:65445
 },

 {
  name: "Mukesh",
  address:"Delhi",
  id:45312
 }];

})

Now create a custom filter if you want to search by using name, address or id.
Use below example to create your custom filter.


.filter('searchFor', function(){
 // All filters must return a function. The first parameter
 // is the data that is to be filtered, and the second is an
 // argument that may be passed with a colon (searchFor:searchString)

    return function(arr, searchString){
        if(!searchString){
            return arr;
        }
        var result = [];

       searchString = searchString.toLowerCase();
// Using the forEach helper method to loop through the array
        angular.forEach(arr, function(item){
       var searchitems=item.name+" "+item.address+" "+item.id; 
           if(searchitems.toLowerCase().indexOf(searchString)){
         result.push(item);
         }
         });
    
   return result; 
}
})

Now on html page create a search bar and show all the list of array items.


<div>
<p>Search:</p>
<input type="text" ng-model="searchString">
</div>

<div ng-repeat=i in items| searchFor:searchString>
{{i.name}}<br>
{{i.address}}<br>>
{{i.id}}
</div>

Follow all the instruction carefully if facing any problem comment below.
Thank You.


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

Most Reading

Labels