15

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);
    }
);
aharris88
  • 3,560
  • 3
  • 27
  • 45

3 Answers3

11

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

aharris88
  • 3,560
  • 3
  • 27
  • 45
  • Are you using the plugin I wrote? You should probably submit an issue there with more detail or start a new question on stackoverflow. – aharris88 Dec 30 '13 at 18:01
  • I have followed the mentioned steps. I want to keep the intent as blank so I used var intent = ""; sms.send(number, message, intent, success, error); I am getting alert click but I didn't get any response message and also message is not sending. Can you please tell me the reason for my issue – Vinod Jul 09 '14 at 15:41
  • I am using android platform with cordova version 3.5 – Vinod Jul 09 '14 at 15:48
  • 1
    You might want to just use my plugin on github because there have been some changes to the code since this answer. If you have any problems, feel free to put in an issue on github: https://github.com/aharris88/phonegap-sms-plugin – aharris88 Jul 09 '14 at 18:24
  • Yes aharris88 I am using your plugin only from the following link github.com/aharris88/phonegap-sms-plugin. Plugin is installed and the message is not sending and also I didn't get any response either success or failure. Please help me out of this issue – Vinod Jul 10 '14 at 05:21
  • 2
    That's the exact same link. Please put in an issue on github or create your own question on stackoverflow. – aharris88 Jul 10 '14 at 17:39
  • Yeah I have added my issue as a new question. Link is as fllows, http://stackoverflow.com/questions/24690858/issue-with-sms-cordova-3-5-plugin-for-android – Vinod Jul 11 '14 at 05:51
0

You may have a look into below link for sending SMS in android. All other PhoneGap plugins that available on Git are outdated.

https://github.com/javatechig/phonegap-sms-plugin

You can find usage steps for the above plugin here

** I am not sure if it works with PhoneGap3.0, but this works great for me in 2.9 version.**

Nilanchala
  • 5,891
  • 8
  • 42
  • 72
0

I just recently converted an old plugin to 3.0, I had a question on it but the full code is included along with the answer to my question which may prove helpful to you. See below

Phonegap 3.0 Custom Plugin

This one returns a value to your javascript caller. Hope it helps, I'll be happy to answer any questions.

Community
  • 1
  • 1
Obi
  • 3,091
  • 4
  • 34
  • 56