Skip to main content

Sending SMS With Ionic 2 App For Android

Share on FacebookTweet about this on TwitterShare on Google+Share on RedditShare on StumbleUponDigg thisFlattr the authorPin on PinterestShare on TumblrShare on LinkedInPrint this pageBuffer this page

In this Tutorial we will see how to send SMS with the cordova-plugin-sms plugin for Ionic 2 Apps For Android. This plugin not only can send SMSes but can Read SMSes as well.

A thing to note here about the plugin is that it can only be used for Android only. If you are looking for a cross-platform solution I already wrote a similar tutorial for sending SMS for Ionic 2 Apps here.

Step 1 )

Let’s create a new Ionic 2 project by issuing the following commands

After navigating into the project now let’s add the Android platform.

Step 2 )

Here we will add the cordova-plugin-sms plugin. Make sure you are inside the project directory and run the following commands

Step 3 )

Inside our project open up src\pages\home\home.html file. We will create two text fields and add a button the following way

Here I am adding two text fields with Two-way Data Binding [(ngModel)] which will create two objects smsNumber and smsText which we can use in the JavaScript.

Step 4)

Now we will code the JavaScript function that will send the SMS, you can ready more about the function in the plugin docs .Open up src\pages\home\home.ts file and put the following code in it.

While using TypeScript we have to declare a window variable which we are doing on line 4, this window variable will now allow us to access the plugin’s SMS object.

The plugin gives us a global SMS object which has asendSMS() method, this method takes four arguments the first is the number or numbers which can be text for one number and an array of numbers for multiple numbers.We are passing the number by using this.smsNumber.

The second is the text message which is to be sent, here we are using the this.smsText to pass the message.

The last two arguments are the success and failure callback functions respectively.

 

Step 5)

Now all we have to do is to test our app, we can do that by using the run command like so

Note:

This plugin will not work in the browser. You will need to install the app on a device and run the app for this to work.

 

Leave Me Your Questions And Feedback

Getting your feedback inspires me the most so hit me up in the comments here or on the comments section on my YouTube Channel or on twitter @pointDeveloper

 

Consider Subscribing / Donating
Subscribe To Mailing List
Donate
If my site was helpful to you, then please consider donating via paypal:

Prantik Vaghela (pointdeveloper)

Hi my name is Prantik Vaghela. I have done my Bachelors and Masters in Computer Science and am a web developer and now a Tutor through my blog. :-)

%d bloggers like this: