Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

buildscript {
repositories {
mavenCentral()
jcenter()
google()
}

Expand All @@ -13,18 +11,32 @@ buildscript {

apply plugin: 'com.android.library'

// Helper to safely read properties from the root project,
// falling back to a default value if not defined.
def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

android {
namespace "com.reactlibrary"

compileSdkVersion safeExtGet('compileSdkVersion', 34)

defaultConfig {
compileSdk 34
minSdkVersion 34
targetSdkVersion 34
minSdkVersion safeExtGet('minSdkVersion', 23)
targetSdkVersion safeExtGet('targetSdkVersion', 34)
versionCode 1
versionName "1.0"
}

lintOptions {
abortOnError false
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

repositories {
Expand All @@ -33,7 +45,8 @@ repositories {
}

dependencies {
implementation 'com.facebook.react:react-native:0.73.6'
// Use the host app's React Native version instead of a hardcoded one.
// This ensures compatibility across SDK 51–54 (RN 0.74–0.81) and beyond.
implementation 'com.facebook.react:react-native:+'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
}

34 changes: 20 additions & 14 deletions android/src/main/java/com/reactlibrary/RNExpoReadSmsModule.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package com.reactlibrary;

import android.Manifest;
Expand All @@ -7,6 +6,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
Expand Down Expand Up @@ -39,28 +39,35 @@ public String getName() {

@ReactMethod
public void startReadSMS(final Callback success, final Callback error) {
try{
try {
if (ContextCompat.checkSelfPermission(reactContext, Manifest.permission.RECEIVE_SMS) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(reactContext, Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) {

msgReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("received_sms", getMessageFromMessageIntent(intent));
}
};

String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
if(Build.VERSION.SDK_INT >= 34 && getApplicationInfo().targetSdkVersion >= 34) {

// Android 14 (API 34)+ requires explicit RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED flag
// when registering a dynamic BroadcastReceiver. SMS_RECEIVED is a system broadcast,
// so RECEIVER_EXPORTED is the correct flag here.
if (Build.VERSION.SDK_INT >= 34 && getReactApplicationContext().getApplicationInfo().targetSdkVersion >= 34) {
reactContext.registerReceiver(msgReceiver, new IntentFilter(SMS_RECEIVED_ACTION), Context.RECEIVER_EXPORTED);
} else {
reactContext.registerReceiver(msgReceiver, new IntentFilter(SMS_RECEIVED_ACTION));
}

success.invoke("Start Read SMS successfully");
} else {
// Permission has not been granted
error.invoke("Required RECEIVE_SMS and READ_SMS permission");
}
} catch (Exception e){
} catch (Exception e) {
e.printStackTrace();
}
}
Expand All @@ -70,6 +77,7 @@ public void stopReadSMS() {
try {
if (reactContext != null && msgReceiver != null) {
reactContext.unregisterReceiver(msgReceiver);
msgReceiver = null;
}
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -78,13 +86,12 @@ public void stopReadSMS() {

private String getMessageFromMessageIntent(Intent intent) {
final Bundle bundle = intent.getExtras();

/*
Index 0 - to have originating Address
Index 1 - to have message body
Index 0 - originating address (phone number)
Index 1 - message body
*/

String SMSReturnValues [] = new String [2];
String[] SMSReturnValues = new String[2];

try {
if (bundle != null) {
Expand All @@ -97,13 +104,12 @@ private String getMessageFromMessageIntent(Intent intent) {
}
}
}
Log.i("ReadSMSModule", "SMS Originating Address received is:"+SMSReturnValues[0]);
Log.i("ReadSMSModule", "SMS received is:"+SMSReturnValues[1]);
Log.i("ReadSMSModule", "SMS Originating Address: " + SMSReturnValues[0]);
Log.i("ReadSMSModule", "SMS Body: " + SMSReturnValues[1]);
} catch (Exception e) {
e.printStackTrace();
}

final String finalSMSReturnValues = Arrays.toString(SMSReturnValues);
return finalSMSReturnValues;
return Arrays.toString(SMSReturnValues);
}
}
}
15 changes: 5 additions & 10 deletions android/src/main/java/com/reactlibrary/RNExpoReadSmsPackage.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package com.reactlibrary;

import java.util.Arrays;
Expand All @@ -9,20 +8,16 @@
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.bridge.JavaScriptModule;

public class RNExpoReadSmsPackage implements ReactPackage {

@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new RNExpoReadSmsModule(reactContext));
}

// Deprecated from RN 0.47
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
return Arrays.<NativeModule>asList(new RNExpoReadSmsModule(reactContext));
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
return Collections.emptyList();
}
}
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@maniac-tech/react-native-expo-read-sms",
"version": "9.0.2-alpha",
"version": "9.1.1",
"description": "Library to read incoming SMS in Android for Expo (React Native)",
"main": "index.js",
"scripts": {
Expand All @@ -10,12 +10,14 @@
"react-native",
"sms",
"read sms",
"android sms"
"android sms",
"expo",
"otp"
],
"author": "Abhishek Jain",
"license": "MIT",
"peerDependencies": {
"react-native": "*"
"react-native": ">=0.73"
},
"repository": {
"type": "git",
Expand Down
Loading