From 669da955af59c4e7f359d2b373c76d87685ff3f3 Mon Sep 17 00:00:00 2001 From: amiru Date: Tue, 13 Feb 2018 12:16:16 +0200 Subject: [PATCH 1/3] added "direct_send" prop for Android --- README.md | 23 +++++++--- .../com/tkporter/sendsms/SendSMSModule.java | 46 ++++++++++++++++--- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index ace18ed..2a16730 100644 --- a/README.md +++ b/README.md @@ -3,16 +3,19 @@ ## SendSMS Use this RN component to send an SMS with a callback (completed/cancelled/error). iOS and Android are both supported. -Currently, only user-initiated sending of an SMS is supported. This means you can't use `react-native-sms` to send an SMS in the background-- this package displays the native SMS view (populated with any recipients/body you want), and gives a callback describing the status of the SMS (completed/cancelled/error). PRs are welcome! +Currently, only user-initiated sending of an SMS is supported. +This means you can't use `react-native-sms` to send an SMS in the background -- this package displays the native SMS view (populated with any recipients/body you want), and gives a callback describing the status of the SMS (completed/cancelled/error). PRs are welcome! +### Update +A new prop was added to enable direct sending of SMS from Android. see documentation below. ## How to install 1. `npm install react-native-sms --save` ## Getting things set up -The compiler needs to know how to find your sweet new module! (Make sure rnpm is installed via `npm install rnpm -g`) +The compiler needs to know how to find your sweet new module! -`rnpm link react-native-sms` +`react-native link react-native-sms` ### Additional Android Setup @@ -29,7 +32,7 @@ import android.content.Intent; // <-- include if not already there import com.tkporter.sendsms.SendSMSPackage; ``` -Inside MainActivity (place entire function if it's not there already) +Inside **`MainActivity`** (place entire function if it's not there already) ```Java @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { @@ -39,7 +42,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { } ``` -Then head to your [MyApp]Application.java (`MyApp/android/app/src/main/java/so/many/dirs/MyAppApplication.java`) +Then head to your **`[MyApp]Application.java`** (`MyApp/android/app/src/main/java/so/many/dirs/MyAppApplication.java`) Make sure `import com.tkporter.sendsms.SendSMSPackage;` is there @@ -56,15 +59,16 @@ protected List getPackages() { } ``` -Navigate to your `AndroidManifest.xml` (at `MyApp/android/app/src/main/AndroidManifest.xml`), and add this near the top with the other permssions +FYI: this permission will automatically be merged into your built `AndroidManifest.xml` (at `MyApp/android/app/src/main/AndroidManifest.xml`) ```XML ``` -Ensure your launchMode for `.MainActivity` is +If `direct_send` is `false` or undefined - then ensure your launchMode for `MainActivity` is ```XML android:launchMode="singleTask" ``` +in order for the "back" button to return to your app after the message window is closed. ## Using the module @@ -85,6 +89,11 @@ Provides the phone number recipients to show by default `successTypes` (Array (strings), Andriod only, required) +`direct_send` (boolean, optional) + +If true, the Android app will send the SMS directly (without a native messaging app). +It will loop on the recepients and send one by one. + An array of types that would trigger a "completed" response when using android Possible values: diff --git a/android/src/main/java/com/tkporter/sendsms/SendSMSModule.java b/android/src/main/java/com/tkporter/sendsms/SendSMSModule.java index 9fcc153..b6f9a7c 100644 --- a/android/src/main/java/com/tkporter/sendsms/SendSMSModule.java +++ b/android/src/main/java/com/tkporter/sendsms/SendSMSModule.java @@ -4,20 +4,21 @@ import android.content.Intent; import android.os.Build; import android.provider.Telephony; +import android.telephony.SmsManager; import com.facebook.react.bridge.ActivityEventListener; +import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; -import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableArray; -import com.facebook.react.bridge.Callback; +import com.facebook.react.bridge.ReadableMap; public class SendSMSModule extends ReactContextBaseJavaModule implements ActivityEventListener { + private static final int REQUEST_CODE = 5235; private final ReactApplicationContext reactContext; private Callback callback = null; - private static final int REQUEST_CODE = 5235; public SendSMSModule(ReactApplicationContext reactContext) { super(reactContext); @@ -56,6 +57,12 @@ public void sendCallback(Boolean completed, Boolean cancelled, Boolean error) { public void send(ReadableMap options, final Callback callback) { try { this.callback = callback; + + if (options.hasKey("direct_send") ? options.getBoolean("direct_send") : false) { + sendDirect(options, callback); + return; + } + new SendSMSObserver(reactContext, this, options).start(); String body = options.hasKey("body") ? options.getString("body") : ""; @@ -66,11 +73,11 @@ public void send(ReadableMap options, final Callback callback) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(reactContext); sendIntent = new Intent(Intent.ACTION_SEND); - if (defaultSmsPackageName != null){ + if (defaultSmsPackageName != null) { sendIntent.setPackage(defaultSmsPackageName); } sendIntent.setType("text/plain"); - }else { + } else { sendIntent = new Intent(Intent.ACTION_VIEW); sendIntent.setType("vnd.android-dir/mms-sms"); } @@ -83,7 +90,7 @@ public void send(ReadableMap options, final Callback callback) { if (recipients != null) { //Samsung for some reason uses commas and not semicolons as a delimiter String separator = ";"; - if(android.os.Build.MANUFACTURER.equalsIgnoreCase("Samsung")){ + if (android.os.Build.MANUFACTURER.equalsIgnoreCase("Samsung")) { separator = ","; } String recipientString = ""; @@ -102,4 +109,31 @@ public void send(ReadableMap options, final Callback callback) { } } + /** + * todo: do it in a background process + * + * @param options + * @param callback + */ + private void sendDirect(ReadableMap options, Callback callback) { + + String msg = options.hasKey("body") ? options.getString("body") : ""; + + ReadableArray recipients = options.hasKey("recipients") ? options.getArray("recipients") : null; + for (int i = 0; i < recipients.size(); i++) { + String phoneNo = recipients.getString(i); + + try { + SmsManager smsManager = SmsManager.getDefault(); + smsManager.sendTextMessage(phoneNo, null, msg, null, null); + } catch (Exception ex) { + ex.printStackTrace(); + sendCallback(false, false, true); + return; + } + } + + sendCallback(true, false, false); + + } } From 7d9da75ebcaeee51438d8f3d30b4fe8023700118 Mon Sep 17 00:00:00 2001 From: amiru Date: Tue, 13 Feb 2018 12:18:25 +0200 Subject: [PATCH 2/3] added "android.permission.SEND_SMS" permission to the Manifest --- android/src/main/AndroidManifest.xml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 6a0d952..865f41d 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,3 +1,6 @@ - + + + \ No newline at end of file From 5be30e2b21667a5a80abe852cfbf7fe976bec77f Mon Sep 17 00:00:00 2001 From: amiru Date: Tue, 13 Feb 2018 12:19:28 +0200 Subject: [PATCH 3/3] bump Android versions --- android/build.gradle | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 36c4a9a..bde539e 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,22 +1,23 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:1.1.3' + classpath 'com.android.tools.build:gradle:3.0.1' } } apply plugin: 'com.android.library' android { - compileSdkVersion 23 - buildToolsVersion "23.0.1" + compileSdkVersion 26 + buildToolsVersion "26.0.3" defaultConfig { minSdkVersion 16 - targetSdkVersion 22 + targetSdkVersion 25 versionCode 1 versionName "1.0" }