Skip to content Skip to sidebar Skip to footer

Android SMS Sending Using Phonegap/Cordova 3.1

How do I send SMS using Cordova/Phonegap 3.1? I have searched but all the tutorials/plugins seems to be outdated and the examples doesn't work.

Solution 1:

Here I share my working sms plugin. please try with this. I hope this will help you. If any issue let me know.

SmsPlugin.java

package org.apache.cordova.plugin;

import org.json.JSONArray;
import org.json.JSONException;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;

public class SmsPlugin extends CordovaPlugin {
    public final String ACTION_SEND_SMS = "SendSMS";

    @Override
    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
        if (action.equals(ACTION_SEND_SMS)) {
            try {               
                String phoneNumber = args.getString(0);
                String message = args.getString(1);
                String method = args.getString(2);

                if(method.equalsIgnoreCase("INTENT")){
                    invokeSMSIntent(phoneNumber, message);
                    callbackContext.sendPluginResult(new PluginResult( PluginResult.Status.NO_RESULT));
                } else{
                    sendSMS(phoneNumber, message);
                }

                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
                return true;
            }
            catch (JSONException ex) {
                callbackContext.sendPluginResult(new PluginResult( PluginResult.Status.JSON_EXCEPTION));
            }           
        }
        return false;
    }

    private void invokeSMSIntent(String phoneNumber, String message) {
        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.putExtra("sms_body", message);
        sendIntent.setType("vnd.android-dir/mms-sms");
        this.cordova.getActivity().startActivity(sendIntent);
    }

    private void sendSMS(String phoneNumber, String message) {
        SmsManager manager = SmsManager.getDefault();
        PendingIntent sentIntent = PendingIntent.getActivity(this.cordova.getActivity(), 0, new Intent(), 0);
        manager.sendTextMessage(phoneNumber, null, message, sentIntent, null);
    }   
}

index.html

$(document).ready(function() {
             $("#btnDefaultSMS").click(function(){             
                var number = $("#numberTxt").val();
                var message = $("#messageTxt").val();
                SmsPlugin.prototype.send(number, message, 'INTENT',
                    function () { 
                       alert('Message sent successfully');  
                    },
                    function (e) {
                        alert('Message Failed:' + e);
                    }
                );               
             });            
         });

HTML Part

<div data-role="fieldcontain">
               <input name="" id="numberTxt" placeholder="Enter mobile number" value="" type="tel" data-mini="true"><br>
               <textarea name="" id="messageTxt" placeholder="Enter message" data-mini="false"></textarea>
               <br>
               <input id="btnDefaultSMS" type="submit" data-theme="e" value="Send SMS" data-mini="false">
               <input id="btnShare" type="submit" data-theme="e" value="Share" data-mini="false">
            </div>  

smsplugin.js

var SmsPlugin = function () {};
SmsPlugin.prototype.send = function (phone, message, method, successCallback, failureCallback) {    
    return PhoneGap.exec(successCallback, failureCallback, 'SmsPlugin', "SendSMS", [phone, message, method]);
};

PhoneGap.addConstructor(function() {
    PhoneGap.addPlugin("sms", new SmsPlugin());
});

config.xml

<feature name="SmsPlugin">
      <param name="android-package" value="org.apache.cordova.plugin.SmsPlugin"/>
    </feature>

Solution 2:

I'm sending SMSs from phonegap, using the native sms sender by creating a button with a link that uses the sms protocol, similar to bradorego's suggestion for another question:

<a href="sms:/* phone number here */?body=/* body text here */">Link</a>

Post a Comment for "Android SMS Sending Using Phonegap/Cordova 3.1"