Skip to content

Commit 64bcf8b

Browse files
committed
Merge pull request phonegap#1098 from Pyo25/master
SMS Sending Plugin for Android, working with Phonegap 2.5
2 parents ff686b5 + 8f33563 commit 64bcf8b

File tree

3 files changed

+197
-0
lines changed

3 files changed

+197
-0
lines changed

Android/SMSSendingPlugin/README.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
SMS Sending plugin for Phonegap
2+
===============================
3+
By Pierre-Yves Orban
4+
5+
This Android Phonegap plugin allows you to easily send SMS (using the native SMS Manager instead of the default SMS app)
6+
Works with Phonegap 2.x
7+
This plugin was successfully tested with Phonegap 2.5 and Android 4.2.2 (on a Samsung Galaxy Nexus device).
8+
9+
## Adding this plugin to your project ##
10+
0. (Make sure you are using Phonegap > 2.0)
11+
1. Move SmsSendingPlugin.js to your project's www folder and include a reference to it in your html files.
12+
2. Add the java file from src to your project's src hierarchy
13+
3. Reference the plugin in your res/config.xml file
14+
<plugin name="SendSmsPlugin" value="org.apache.cordova.plugin.SendSmsPlugin"/>
15+
4. Ensure that your manifest contains the necessary permissions to send SMS messages:
16+
17+
<uses-permission android:name="android.permission.SEND_SMS"/>
18+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
19+
20+
## Using the plugin ##
21+
To instantiate the plugin object:
22+
```javascript
23+
var smsSendingPlugin = cordova.require('cordova/plugin/smssendingplugin');
24+
```
25+
26+
### isSupported ###
27+
Check if the SMS technology is supported by the device. For example:
28+
29+
```javascript
30+
smsSendingPlugin.isSupported ((function(supported) {
31+
if(supported)
32+
alert("SMS supported !");
33+
else
34+
alert("SMS not supported");
35+
}), function() {
36+
alert("Error while checking the SMS support");
37+
});
38+
```
39+
40+
### send ###
41+
Send an SMS message. For example:
42+
43+
```javascript
44+
smsSendingPlugin.send ("0032472345678", "Hello World !", function() {
45+
alert("Message sent :-)");
46+
}, function() {
47+
alert("Message not sent :s");
48+
});
49+
```
50+
51+
## Licence ##
52+
53+
The MIT License
54+
55+
Copyright (c) 2013 Pierre-Yves Orban
56+
57+
Permission is hereby granted, free of charge, to any person obtaining a copy
58+
of this software and associated documentation files (the "Software"), to deal
59+
in the Software without restriction, including without limitation the rights
60+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
61+
copies of the Software, and to permit persons to whom the Software is
62+
furnished to do so, subject to the following conditions:
63+
64+
The above copyright notice and this permission notice shall be included in
65+
all copies or substantial portions of the Software.
66+
67+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
68+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
69+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
70+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
71+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
72+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
73+
THE SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright (C) 2011 by Pierre-Yves Orban
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
*/
22+
23+
cordova.define("cordova/plugin/smssendingplugin", function(require, exports, module) {
24+
var exec = require('cordova/exec');
25+
var SmsSendingPlugin = function() {};
26+
27+
/**
28+
* Check if the device has a possibility to send and receive SMS
29+
*/
30+
SmsSendingPlugin.prototype.isSupported = function(successCallback,failureCallback) {
31+
return exec(successCallback, failureCallback, 'SmsSendingPlugin', 'HasSMSPossibility', []);
32+
}
33+
/**
34+
* Send a message to the given phone number
35+
*/
36+
SmsSendingPlugin.prototype.send = function(phone, message, successCallback,failureCallback) {
37+
return exec(successCallback, failureCallback, 'SmsSendingPlugin', 'SendSMS', [phone, message]);
38+
}
39+
40+
var smssendingplugin = new SmsSendingPlugin();
41+
module.exports = smssendingplugin;
42+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright (C) 2013 by Pierre-Yves Orban
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
*/
22+
package org.apache.cordova.plugin;
23+
24+
import org.apache.cordova.api.CallbackContext;
25+
import org.apache.cordova.api.CordovaPlugin;
26+
import org.apache.cordova.api.PluginResult;
27+
import org.json.JSONArray;
28+
import org.json.JSONException;
29+
30+
import android.app.Activity;
31+
import android.app.PendingIntent;
32+
import android.content.Intent;
33+
import android.content.pm.PackageManager;
34+
import android.telephony.SmsManager;
35+
36+
public class SmsSendingPlugin extends CordovaPlugin {
37+
public final String ACTION_HAS_SMS_POSSIBILITY = "HasSMSPossibility";
38+
public final String ACTION_SEND_SMS = "SendSMS";
39+
40+
private SmsManager smsManager = null;
41+
42+
public SmsSendingPlugin() {
43+
super();
44+
smsManager = SmsManager.getDefault();
45+
}
46+
47+
@Override
48+
public boolean execute(String action, JSONArray args,
49+
final CallbackContext callbackContext) throws JSONException {
50+
51+
if (action.equals(ACTION_HAS_SMS_POSSIBILITY)) {
52+
53+
Activity ctx = this.cordova.getActivity();
54+
if(ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)){
55+
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
56+
} else {
57+
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, false));
58+
}
59+
return true;
60+
}
61+
else if (action.equals(ACTION_SEND_SMS)) {
62+
try {
63+
String phoneNumber = args.getString(0);
64+
String message = args.getString(1);
65+
this.sendSMS(phoneNumber, message);
66+
67+
callbackContext.sendPluginResult(new PluginResult(
68+
PluginResult.Status.OK));
69+
}
70+
catch (JSONException ex) {
71+
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, ex.getMessage()));
72+
}
73+
return true;
74+
}
75+
return false;
76+
}
77+
78+
private void sendSMS(String phoneNumber, String message) {
79+
PendingIntent sentIntent = PendingIntent.getActivity(this.cordova.getActivity(), 0, new Intent(), 0);
80+
smsManager.sendTextMessage(phoneNumber, null, message, sentIntent, null);
81+
}
82+
}

0 commit comments

Comments
 (0)