arpansharma7474 / rn-sms-retriever

React Native Implementation of Android SMS retriever
MIT License
16 stars 8 forks source link

react-native 0.73 compatibility #7

Open Akatroj opened 8 months ago

Akatroj commented 8 months ago

Hello, while I don't use this library in our project (we are using react-native-sms-retriever instead), I noticed that this is basically a fork of that library, and the issue probably applies to this library as well.

When I was upgrading our project to react-native 0.73, I noticed that the sms retriever was not working, and with some debugging I found the reason: https://stackoverflow.com/a/77276774/17101184

Based on this answer, I made a patch for the original library. As I cannot publish it in that repo anymore (it was archived and is read-only now), I'm posting it here in the hopes that somebody using either of these libraries find this and it saves them some time:

diff --git a/node_modules/react-native-sms-retriever/android/src/main/java/me/furtado/smsretriever/SmsHelper.java b/node_modules/react-native-sms-retriever/android/src/main/java/me/furtado/smsretriever/SmsHelper.java
index b1ab5f4..5495d66 100644
--- a/node_modules/react-native-sms-retriever/android/src/main/java/me/furtado/smsretriever/SmsHelper.java
+++ b/node_modules/react-native-sms-retriever/android/src/main/java/me/furtado/smsretriever/SmsHelper.java
@@ -1,7 +1,11 @@
 package me.furtado.smsretriever;

+import static android.content.Context.RECEIVER_EXPORTED;
+
 import android.content.BroadcastReceiver;
 import android.content.IntentFilter;
+import android.os.Build;
+
 import androidx.annotation.NonNull;

 import com.facebook.react.bridge.Promise;
@@ -57,7 +61,11 @@ final class SmsHelper {
         final IntentFilter intentFilter = new IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION);

         try {
-            mContext.registerReceiver(mReceiver, intentFilter);
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+                mContext.registerReceiver(mReceiver, intentFilter, RECEIVER_EXPORTED);
+            } else {
+                mContext.registerReceiver(mReceiver, intentFilter);
+            }
             return true;
         } catch (Exception e) {
             e.printStackTrace();
Akatroj commented 8 months ago

this is specifically because react-native 0.73 bumped the targetSdkVersion to 34, which started throwing an exception when registering the receiver