Skip to content

Commit

Permalink
Add sms originator information to callback #20 (#26)
Browse files Browse the repository at this point in the history
* Extract originating address, and return the same in an array along with message body

* Update the peer dependency to 0.68.2

* Fix syntax error

* Syntax fixes

* Remove getUSerData

* Change the return type of getMessageFromMessageIntent from String [] to String

* Bump the package version to v1.1

* Update the readme.md
  • Loading branch information
maniac-tech authored Feb 26, 2023
1 parent cd5be17 commit 1857150
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ You may refer to this sample application for how to use the library: [GitHub Rep
|-------------|-------------|--------|---------|
| `checkIfHasSMSPermission` | Function which checks if the application has `READ_SMS` and `RECEIVE_SMS` permissions | - | ```{ hasReceiveSmsPermission: true/false, hasReadSmsPermission: true/false }``` |
| `requestReadSMSPermission` | Requests `READ_SMS` and `RECEIVE_SMS` permission, if missing | - | Returns `true` if granted, and `false` otherwise |
| `startReadSMS` | Starts listening for incoming messages. Note: SMS Permissions should be present. | callback fn | Incoming message body |
| `startReadSMS` | Starts listening for incoming messages. Note: SMS Permissions should be present. | callback fn | Return a string with message orginating address, and message body. Example: `[+919999999999, this is a sample message body]` |


### Important Note:
Expand Down
21 changes: 17 additions & 4 deletions android/src/main/java/com/reactlibrary/RNExpoReadSmsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.facebook.react.bridge.Callback;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.util.Arrays;

public class RNExpoReadSmsModule extends ReactContextBaseJavaModule {

private final ReactApplicationContext reactContext;
Expand Down Expand Up @@ -72,21 +74,32 @@ public void stopReadSMS() {

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

/*
Index 0 - to have originating Address
Index 1 - to have message body
*/

String SMSReturnValues [] = new String [2];

try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
if (pdusObj != null) {
for (Object aPdusObj : pdusObj) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) aPdusObj);
message = currentMessage.getDisplayMessageBody();
SMSReturnValues[0] = currentMessage.getDisplayOriginatingAddress();
SMSReturnValues[1] = currentMessage.getDisplayMessageBody();
}
}
}
Log.i("ReadSMSModule", "SMS received is:"+message);
Log.i("ReadSMSModule", "SMS Originating Address received is:"+SMSReturnValues[0]);
Log.i("ReadSMSModule", "SMS received is:"+SMSReturnValues[1]);
} catch (Exception e) {
e.printStackTrace();
}
return message;

final String finalSMSReturnValues = Arrays.toString(SMSReturnValues);
return finalSMSReturnValues;
}
}
4 changes: 2 additions & 2 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": "1.0.13",
"version": "1.1",
"description": "Library to read incoming SMS in Android for Expo (React Native)",
"main": "index.js",
"scripts": {
Expand All @@ -15,7 +15,7 @@
"author": "Abhishek Jain",
"license": "MIT",
"peerDependencies": {
"react-native": "^0.41.2"
"react-native": "^0.68.2"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 1857150

Please sign in to comment.