Cordova – SMS Phonegap 3.0 plugin for Android and iOS

cordovaphonegap-plugins

I'm looking for phonegap plugins that will work with Phonegap 3.x. I need it to work in Android and iOS. It would be preferable if there was 1 plugin for both, but it's ok if there are 2 seperate plugins that I can use. It's also preferable if I could install it with the command:

phonegap local plugin add

Is there such a plugin out there? Or are there instructions on how to upgrade an existing sms plugin to work with phonegap 3.0?

Edit

I forked a repo of a plugin that works on 2.9 and I'm trying to make it work in phonegap 3.x (https://github.com/aharris88/phonegap-sms-plugin)
and so far I can pull it into my phonegap project with the command

phonegap local plugin add https://github.com/aharris88/phonegap-sms-plugin

and it correctly puts the permissions it needs in the AndroidManifest.xml and it puts the feature in res/xml/config.xml, but when I install it on my phone it doesn't say it needs permission to send texts, and I don't get any success or error message from this code:

var number = $('#number').val();
var message = $('#text').val();
alert("Send text to "+number+" with message: "+message);
SmsPlugin.prototype.send(number, message, '',
    function () {
        alert('Message sent successfully');
    },
    function (e) {
        alert('Message Failed:' + e);
    }
);

Best Answer

The best way to debug it was to use the ADT (Android Developer Tools). There were a lot of small things wrong. This article was a very useful resource: http://devgirl.org/2013/09/17/how-to-write-a-phonegap-3-0-plugin-for-android/

Here is the sms.java code:

package com.adamwadeharris.sms;

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

public class Sms extends CordovaPlugin {
    public final String ACTION_SEND_SMS = "send";

    @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{
                    send(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.putExtra("address", phoneNumber);
        sendIntent.setType("vnd.android-dir/mms-sms");
        this.cordova.getActivity().startActivity(sendIntent);
    }

    private void send(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);
    }
}

Here's the sms.js code:

var sms = {
    send: function(phone, message, method, successCallback, failureCallback) {
        cordova.exec(
            successCallback,
            failureCallback,
            'Sms',
            'send',
            [phone, message, method]
        );
    }
}

module.exports = sms;

And here is the plugin.xml:

<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
           id="com.adamwadeharris.sms"
      version="0.1.0">
    <name>Sms</name>
    <description>Cordova SMS Send Plugin</description>
    <license>MIT</license>
    <keywords>cordova,phonegap,sms</keywords>


    <js-module src="www/sms.js" name="Sms">
        <clobbers target="window.sms" />
    </js-module>

    <!-- android -->
    <platform name="android">
        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="Sms">
                <param name="android-package" value="com.adamwadeharris.sms.Sms"/>
            </feature>
        </config-file>

        <config-file target="AndroidManifest.xml" parent="/manifest">
            <uses-permission android:name="android.permission.SEND_SMS" />
        </config-file>

        <source-file src="src/android/Sms.java" target-dir="src/com/adamwadeharris/sms" />
    </platform>

</plugin>

Edit Also, I have the plugin available on github: https://github.com/aharris88/phonegap-sms-plugin

Edit This plugin has now moved to: https://github.com/cordova-sms/cordova-sms-plugin

Related Topic