Skip to content

Commit

Permalink
Merge pull request #1 from Telerik-Verified-Plugins/tnikolov/merge-up…
Browse files Browse the repository at this point in the history
…stream

Merge remote-tracking branch 'upstream/master'
  • Loading branch information
TomaNikolov authored Mar 14, 2017
2 parents fba0634 + 060f82a commit bb00a3e
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 79 deletions.
40 changes: 40 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "cordova-sms-plugin",
"version": "0.1.11",
"description": "Cross-platform plugin for Cordova / PhoneGap to to easily send SMS. Available for Android, iOS and WP.",
"cordova": {
"id": "cordova-sms-plugin",
"platforms": [
"android",
"wp8",
"ios",
"windows"
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/cordova-sms/cordova-sms-plugin.git"
},
"keywords": [
"cordova",
"phonegap",
"sms",
"ecosystem:cordova",
"cordova-android",
"cordova-wp8",
"cordova-ios",
"cordova-windows"
],
"engines": [
{
"name": "cordova",
"version": ">=3.0.0"
}
],
"author": "Didier Baquier",
"license": "MIT",
"bugs": {
"url": "https://github.com/cordova-sms/cordova-sms-plugin/issues"
},
"homepage": "https://github.com/cordova-sms/cordova-sms-plugin#readme"
}
18 changes: 13 additions & 5 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="org.apache.cordova.plugin.sms"
version="0.1.1">
id="org.apache.cordova.plugin.sms"
version="0.1.2">
<name>Sms</name>
<description>Cordova SMS Send Plugin</description>
<license>MIT</license>
<keywords>cordova,phonegap,sms</keywords>

<engines>
<engine name="cordova" version=">=3.0.0" />
</engines>
</engines>

<js-module src="www/sms.js" name="Sms">
<clobbers target="window.sms" />
Expand All @@ -20,7 +20,7 @@
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="Sms">
<param name="android-package" value="org.apache.cordova.plugin.sms.Sms"/>
<param name="android-package" value="com.cordova.plugins.sms.Sms"/>
</feature>
</config-file>

Expand All @@ -30,9 +30,10 @@
<uses-feature android:name="android.hardware.telephony" android:required="false" />
</config-file>

<source-file src="src/android/Sms.java" target-dir="src/org/apache/cordova/plugin/sms" />
<source-file src="src/android/Sms.java" target-dir="src/com/cordova/plugins/sms" />
</platform>

<!-- wp8 -->
<platform name="wp8">
<source-file src="src/wp8/Sms.cs" />

Expand All @@ -54,4 +55,11 @@
<source-file src="src/ios/Sms.m" />
<framework src="MessageUI.framework" weak="true" />
</platform>

<!-- windows -->
<platform name="windows">
<js-module src="src/windows/SmsProxy.js" name="SmsProxy">
<merges target="" />
</js-module>
</platform>
</plugin>
57 changes: 49 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#Cordova SMS Plugin

Cross-platform plugin for Cordova / PhoneGap to to easily send SMS. Available for **Android**, **iOS**, and **Windows Phone 8**.

This plugin works with Cordova 3.x and 4.x version.
Cross-platform plugin for Cordova / PhoneGap to to easily send SMS. Available for **Android**, **iOS**, **Windows Phone 8** and **Windows 10 Universal (BETA)**.

##Installing the plugin

Using the Cordova CLI, run:
Using the Cordova CLI and NPM, run:

cordova plugin add cordova-sms-plugin

It is also possible to install via repo url directly (unstable), run :

cordova plugin add https://github.com/cordova-sms/cordova-sms-plugin.git

Expand All @@ -23,11 +25,11 @@ Javascript
sendSms: function() {
var number = document.getElementById('numberTxt').value;
var message = document.getElementById('messageTxt').value;
alert(number);
alert(message);
console.log("number=" + number + ", message= " + message);

//CONFIGURATION
var options = {
replaceLineBreaks: false, // true to replace \n by a new line, false by default
android: {
intent: 'INTENT' // send SMS with the native android SMS messaging
//intent: '' // send SMS without open any other app
Expand All @@ -40,9 +42,48 @@ Javascript
}
};

On Android, an extra function is exposed to know whether or not you have the permission to send a SMS (Android Marshmallow permission).

var app = {
checkSMSPermission: function() {
var success = function (hasPermission) {
if (hasPermission) {
sms.send(...);
}
else {
// show a helpful message to explain why you need to require the permission to send a SMS
// read http://developer.android.com/training/permissions/requesting.html#explain for more best practices
}
};
var error = function (e) { alert('Something went wrong:' + e); };
sms.hasPermission(success, error);
}
};

##FAQ
####`sms` is undefined

Please go through all the [closed issues about this subject](https://github.com/cordova-sms/cordova-sms-plugin/issues?q=is%3Aissue+is%3Aclosed+sms+label%3A%22sms+undefined%22). The issue is mostly coming from the way you installed the plugin, please double check everything before opening another issue.

####When building my project for android I get the following error: `cannot find symbol: cordova.hasPermission(string)`

You need to update `cordova-android` to the latest version (recommended), or at least to the version 5.1.1.

`cordova platform update android` or `cordova platform update [email protected]`

####Is the plugin available on [Adobe PhoneGap Build](https://build.phonegap.com)?

Yes, the plugin is available, please see instructions here: http://docs.phonegap.com/phonegap-build/configuring/plugins/. Use the npm or github source.

####How can I receive SMS?

You can't receive SMS via this plugin. This plugin only sends SMS.

####Android immediately passes success back to app?

Please read [#issue 26](https://github.com/cordova-sms/cordova-sms-plugin/issues/26)

###I get this error. What's wrong?
####I get this error. What's wrong?

compile:
[javac] Compiling 4 source files to /Users/username/MyProject/platforms/android/bin/classes
Expand All @@ -69,7 +110,7 @@ The problem is that you need to make sure that you set the target to android-19
target=android-19


#### How can I send an sms in my iOS app without passing control to the native app like it can be done on Android?
##### How can I send an sms in my iOS app without passing control to the native app like it can be done on Android?

This isn't possible on iOS. It requires that you show the user the native sms composer, to be able to send an sms.

Expand Down
114 changes: 87 additions & 27 deletions src/android/Sms.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.apache.cordova.plugin.sms;
package com.cordova.plugins.sms;

import android.annotation.SuppressLint;
import android.app.Activity;
Expand All @@ -20,38 +20,96 @@
import org.json.JSONException;

public class Sms extends CordovaPlugin {

public final String ACTION_SEND_SMS = "send";

public final String ACTION_HAS_PERMISSION = "has_permission";

private static final String INTENT_FILTER_SMS_SENT = "SMS_SENT";

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
private static final int SEND_SMS_REQ_CODE = 0;

if (action.equals(ACTION_SEND_SMS)) {
try {
String phoneNumber = args.getJSONArray(0).join(";").replace("\"", "");
String message = args.getString(1);
String method = args.getString(2);

if (!checkSupport()) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "SMS not supported on this platform"));
return true;
}
private CallbackContext callbackContext;

if (method.equalsIgnoreCase("INTENT")) {
invokeSMSIntent(phoneNumber, message);
// always passes success back to the app
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
} else {
send(callbackContext, phoneNumber, message);
}
return true;
} catch (JSONException ex) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
private JSONArray args;

@Override
public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
this.callbackContext = callbackContext;
this.args = args;
if (action.equals(ACTION_SEND_SMS)) {
if (hasPermission()) {
sendSMS();
} else {
requestPermission();
}
return true;
}
else if (action.equals(ACTION_HAS_PERMISSION)) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, hasPermission()));
return true;
}
return false;
}

private boolean hasPermission() {
return cordova.hasPermission(android.Manifest.permission.SEND_SMS);
}

private void requestPermission() {
cordova.requestPermission(this, SEND_SMS_REQ_CODE, android.Manifest.permission.SEND_SMS);
}

public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
for (int r : grantResults) {
if (r == PackageManager.PERMISSION_DENIED) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "User has denied permission"));
return;
}
}
sendSMS();
}

private boolean sendSMS() {
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
try {
//parsing arguments
String separator = ";";
if (android.os.Build.MANUFACTURER.equalsIgnoreCase("Samsung")) {
// See http://stackoverflow.com/questions/18974898/send-sms-through-intent-to-multiple-phone-numbers/18975676#18975676
separator = ",";
}
String phoneNumber = args.getJSONArray(0).join(separator).replace("\"", "");
String message = args.getString(1);
String method = args.getString(2);
boolean replaceLineBreaks = Boolean.parseBoolean(args.getString(3));

// replacing \n by new line if the parameter replaceLineBreaks is set to true
if (replaceLineBreaks) {
message = message.replace("\\n", System.getProperty("line.separator"));
}
if (!checkSupport()) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "SMS not supported on this platform"));
return;
}
if (method.equalsIgnoreCase("INTENT")) {
invokeSMSIntent(phoneNumber, message);
// always passes success back to the app
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
} else {
send(callbackContext, phoneNumber, message);
}
return;
} catch (JSONException ex) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
}
}
});
return true;
}

private boolean checkSupport() {
Activity ctx = this.cordova.getActivity();
return ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
Expand All @@ -72,8 +130,10 @@ private void invokeSMSIntent(String phoneNumber, String message) {
}
} else {
sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("smsto:" + Uri.encode(phoneNumber)));
sendIntent.putExtra("sms_body", message);
// See http://stackoverflow.com/questions/7242190/sending-sms-using-intent-does-not-add-recipients-on-some-devices
sendIntent.putExtra("address", phoneNumber);
sendIntent.setData(Uri.parse("smsto:" + Uri.encode(phoneNumber)));
}
this.cordova.getActivity().startActivity(sendIntent);
}
Expand All @@ -82,9 +142,9 @@ private void send(final CallbackContext callbackContext, String phoneNumber, Str
SmsManager manager = SmsManager.getDefault();
final ArrayList<String> parts = manager.divideMessage(message);

// by creating this broadcast receiver we can check whether or not the SMS was sent
// by creating this broadcast receiver we can check whether or not the SMS was sent
final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

boolean anyError = false; //use to detect if one of the parts failed
int partsCount = parts.size(); //number of parts to send

Expand Down Expand Up @@ -132,4 +192,4 @@ public void onReceive(Context context, Intent intent) {
manager.sendTextMessage(phoneNumber, null, message, sentIntent, null);
}
}
}
}
Loading

0 comments on commit bb00a3e

Please sign in to comment.