While working on how to send SMS from smartphone using PhoneGap, I found the plugin which is really very usefull & easy to integrate with Phonegap application. Given below is the example and steps to use the plugin & send message from Android device.
Targreted OS: Android
Technology: PhoneGap
1. First of all you need to download the plugins for Android from GitHub by using the following url
https://github.com/phonegap/phonegap-plugins/tree/master/Android/SMSPlugin
2. Copy the src/net/practicaldeveloper/phonegap/plugins/SmsPlugin.java file and put it inside the src hierarchy of your project.
3. Change the package name with the package name of your project at the top of the SmsPlugin.java file.
4. Copy the assets/www/smsplugins.js file, put it inside the www folder of your project and refer it in header section of your html page.
5. Create a textbox, textarea and a submit button like shown in the code below.
Contact Number:
Message:
6. Use the following javascript code to send the message by entering contact number and message in respective fields.
function sendSMS () { var myElement = document.getElementById('sendButton'); myElement.onclick = function() { var contactNumber = document.getElementById('contactBox').value; var message= document.getElementById('messageBox').value; window.plugins.sms.send(contactNumber , message, function () { alert('Message successfully sent to' + contactNumber); }, function (event) { alert('Message failed due to:' + event); } ); } }
7. Add the eventlistner to load the function when device gets ready.
document.addEventListener("deviceready", sendSMS , false);
Note: If you are using Cordova 2.0.0 or later you need to follow the steps bellow.
1. Replace the following codes from smsplugins.js file
PhoneGap.addConstructor(function() { PhoneGap.addPlugin("sms", new SmsPlugin()); }); with window.sms = new SmsPlugin();
2. Replace the line in smsplugins.js file
return PhoneGap.exec(successCallback, failureCallback, 'SmsPlugin', "SendSMS", [phone, message]); with return cordova.exec(successCallback, failureCallback, 'SmsPlugin', "SendSMS", [phone, message]);
3. Change the javascript code
window.sms.send(contactNumber , message, function () { alert('Message successfully sent to' + contactNumber); }, function (event) { alert('Message failed due to:' + event); } );
Now you are ready to send messages from your Android device.