-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathRNExpoReadSmsModule.java
More file actions
110 lines (94 loc) · 3.61 KB
/
Copy pathRNExpoReadSmsModule.java
File metadata and controls
110 lines (94 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.reactlibrary;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Build;
import android.telephony.SmsMessage;
import android.util.Log;
import androidx.core.content.ContextCompat;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
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;
private BroadcastReceiver msgReceiver;
public static final String NAME = "RNExpoReadSms";
public RNExpoReadSmsModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Override
public String getName() {
return NAME;
}
@ReactMethod
public void startReadSMS(final Callback success, final Callback error) {
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) {
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){
e.printStackTrace();
}
}
@ReactMethod
public void stopReadSMS() {
try {
if (reactContext != null && msgReceiver != null) {
reactContext.unregisterReceiver(msgReceiver);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private String getMessageFromMessageIntent(Intent intent) {
final Bundle bundle = intent.getExtras();
/*
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);
SMSReturnValues[0] = currentMessage.getDisplayOriginatingAddress();
SMSReturnValues[1] = currentMessage.getDisplayMessageBody();
}
}
}
Log.i("ReadSMSModule", "SMS Originating Address received is:"+SMSReturnValues[0]);
Log.i("ReadSMSModule", "SMS received is:"+SMSReturnValues[1]);
} catch (Exception e) {
e.printStackTrace();
}
final String finalSMSReturnValues = Arrays.toString(SMSReturnValues);
return finalSMSReturnValues;
}
}