Tuesday, April 10, 2012

Changes to SMS Plugin for PhoneGap 1.5

The PhoneGap transition to Cordova introduced some changes to the plugin Architecture. I've been focusing a little on Sencha / Titanium (just broadening my horizons not abandoning the platform), so I haven't really had an opportunity to look at the full scope of the changes. Since at least one person is having adapting the SMSPlugin to 1.5, here's a short list of changes. I'll get these changes into the git repository when I have a chance. It appears that the only changes required in the plugin are: Remove the import "com.phonegap.api.PluginResult.Status" and replace it with "org.apache.cordova.api.PluginResult.Status", and change the first parameter of the pendingIntent.getActivity call from "this.ctx" to "this.ctx.getContext()"

UPDATE: These changes have been merged with the phonegap-plugins repository on GitHub

/*
Copyright (C) 2011 by Daniel Shookowsky

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package net.practicaldeveloper.phonegap.plugins;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;

public class DemoPlugin extends Plugin {
 public final String ACTION_SEND_SMS = "SendSMS";
 
 @Override
 public PluginResult execute(String action, JSONArray arg1, String callbackId) {
  PluginResult result = new PluginResult(Status.INVALID_ACTION);
  
  if (action.equals(ACTION_SEND_SMS)) {
   try {
    String phoneNumber = arg1.getString(0);
    String message = arg1.getString(1);
    sendSMS(phoneNumber, message);
    result = new PluginResult(Status.OK);
   }
   catch (JSONException ex) {
    result = new PluginResult(Status.ERROR, ex.getMessage());
   }   
  }
  
  return result;
 }

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

}

3 comments:

  1. Does this plugin work with Phonegap 2.2.0? I'm getting the error cannot read sms property.

    ReplyDelete
  2. I haven't yet had an opportunity to update this plugin for 2.1/2.2. It's definitely on my list of things to do, I just have to find some free time.

    ReplyDelete