ajith-ab / react-native-receive-sharing-intent

A React Native plugin that enables React Native apps to receive sharing photos, videos, text, urls or any other file types from another app
MIT License
297 stars 108 forks source link

Crash app when build real device in Android #110

Open itsminh99 opened 2 years ago

itsminh99 commented 2 years ago
VBarzionov commented 2 years ago

Look at file ReceiveSharingIntentModule.java

  @ReactMethod
  public void getFileNames(Promise promise){
    Activity mActivity = getCurrentActivity();
    if(mActivity == null) { return; }
    Intent intent = mActivity.getIntent();
    receiveSharingIntentHelper.sendFileNames(reactContext, intent, promise);
    mActivity.setIntent(null); // <-------- this is Not Good!
  }

this cause Xcpt in RN Linking.getInitialURL here

  public void getInitialURL(Promise promise) {
    try {
      Activity currentActivity = getCurrentActivity();
      String initialURL = null;

      if (currentActivity != null) {
        Intent intent = currentActivity.getIntent();
        String action = intent.getAction(); // <--- access to NULL intent
vlesu commented 2 years ago

This also cause problems in another modules (for example, exception in RNPushNotifications...)

In my case, solution in file ReceiveSharingIntentModule.java was:

  private final ReactApplicationContext reactContext;
  private ReceiveSharingIntentHelper receiveSharingIntentHelper;
  private Intent oldIntent;  // <-- add this line

...

  protected void onNewIntent(Intent intent) {
    Activity mActivity = getCurrentActivity();
    if(mActivity == null) { return; }
    oldIntent = mActivity.getIntent();  // <-- add this line
    mActivity.setIntent(intent);
  }

  @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  @ReactMethod
  public void getFileNames(Promise promise){
    Activity mActivity = getCurrentActivity();
    if(mActivity == null) { return; }
    Intent intent = mActivity.getIntent();
    receiveSharingIntentHelper.sendFileNames(reactContext, intent, promise);
    if (oldIntent != null) {  // <-- add this line
      mActivity.setIntent(oldIntent);  // <-- change this line from mActivity.setIntent(null); 
    }  // <-- add this line
  }
alexkendall commented 2 years ago

Thanks you two. Those changes fixed the issue for me.

robbiedood commented 2 years ago

@vlesu Shall we also modify clearFileNames() based on the change ? Hope to hear your expertise. --Luke

vlesu commented 2 years ago

@vlesu Shall we also modify clearFileNames() based on the change ? Hope to hear your expertise. --Luke

Unfortunately, I'm just an enthusiast, not an expert.

I did not modify a code in clearFileNames:

@ReactMethod
  public void clearFileNames(){
    Activity mActivity = getCurrentActivity();
    if(mActivity == null) { return; }
    Intent intent = mActivity.getIntent();
    receiveSharingIntentHelper.clearFileNames(intent);
  }

But I had troubles with imcoming files appears TWICE when I switch to another app and switch back.

To resolve this, I have added into ReceiveSharingIntent.ts

    clearFileNames(){
      ReceiveSharingIntent.clearFileNames();
    }

And use this in my code like this:

ReceiveSharingIntent.getReceivedFiles(async (files) => {
...
           for(let i = 0; i < files.length; i++) {
            if (files[i].filePath) {
              files[i].base64 = await RNFS.readFile(files[i].filePath, "base64");
            }
          }
....
           ReceiveSharingIntent.clearFileNames();
...
OskarD commented 2 years ago

I still have this problem with both emulated and real device environments

Edit: Just noticed that it's because the fix hasn't been released yet. When are you planning on releasing it?

jensdev commented 2 years ago

Until this merges. You can use the snippet below. Put this in patches/react-native-receive-sharing-intent+2.0.0.patch and use https://www.npmjs.com/package/patch-package

diff --git a/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentModule.java b/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentModule.java
index f752144..d2542f9 100644
--- a/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentModule.java
+++ b/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentModule.java
@@ -18,6 +18,7 @@ public class ReceiveSharingIntentModule extends ReactContextBaseJavaModule {

   private final ReactApplicationContext reactContext;
   private ReceiveSharingIntentHelper receiveSharingIntentHelper;
+  private Intent oldIntent;

   public ReceiveSharingIntentModule(ReactApplicationContext reactContext) {
     super(reactContext);
@@ -30,6 +31,7 @@ public class ReceiveSharingIntentModule extends ReactContextBaseJavaModule {
   protected void onNewIntent(Intent intent) {
     Activity mActivity = getCurrentActivity();
     if(mActivity == null) { return; }
+    oldIntent = mActivity.getIntent();
     mActivity.setIntent(intent);
   }

@@ -40,7 +42,9 @@ public class ReceiveSharingIntentModule extends ReactContextBaseJavaModule {
     if(mActivity == null) { return; }
     Intent intent = mActivity.getIntent();
     receiveSharingIntentHelper.sendFileNames(reactContext, intent, promise);
-    mActivity.setIntent(null);
+    if (oldIntent != null) {
+      mActivity.setIntent(oldIntent);
+    }  
   }

   @ReactMethod
diff --git a/node_modules/react-native-receive-sharing-intent/src/ReceiveSharingIntent.ts b/node_modules/react-native-receive-sharing-intent/src/ReceiveSharingIntent.ts
index 735c191..91dab4b 100644
--- a/node_modules/react-native-receive-sharing-intent/src/ReceiveSharingIntent.ts
+++ b/node_modules/react-native-receive-sharing-intent/src/ReceiveSharingIntent.ts
@@ -33,7 +33,7 @@ class ReceiveSharingIntentModule implements IReceiveSharingIntent {
     }

     clearReceivedFiles(){
-        this.isClear = true;
+        ReceiveSharingIntent.clearFileNames();
     }