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;

publicclassSmsPluginextendsCordovaPlugin {
    publicfinalStringACTION_SEND_SMS="SendSMS";

    @Overridepublicbooleanexecute(String action, JSONArray args, final CallbackContext callbackContext)throws JSONException {
        if (action.equals(ACTION_SEND_SMS)) {
            try {               
                StringphoneNumber= args.getString(0);
                Stringmessage= args.getString(1);
                Stringmethod= args.getString(2);

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

                callbackContext.sendPluginResult(newPluginResult(PluginResult.Status.OK));
                returntrue;
            }
            catch (JSONException ex) {
                callbackContext.sendPluginResult(newPluginResult( PluginResult.Status.JSON_EXCEPTION));
            }           
        }
        returnfalse;
    }

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

    privatevoidsendSMS(String phoneNumber, String message) {
        SmsManagermanager= SmsManager.getDefault();
        PendingIntentsentIntent= PendingIntent.getActivity(this.cordova.getActivity(), 0, newIntent(), 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

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

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

config.xml

<featurename="SmsPlugin"><paramname="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:

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

Post a Comment for "Android Sms Sending Using Phonegap/cordova 3.1"