carsten-klaffke/send-intent

Not receiving actual URL when sharing from chrome

Closed this issue · 6 comments

Describe the bug
Sharing from iOS Safari gives the actual (HTTP/S) URL in the result object of SendIntent.checkSendIntentReceived
But sharing from Android Chrome gives a url that is a file:// string, and the contents of that file are not clear - they are random characters. (Base64 decoding results in binary)
On Android, the result object is :
{"title":"There's an Urgent New Warning for Walmart and Amazon Shoppers","type":"text/plain","url":"file:///data/user/0/<>/files/.com.google.Chrome.rKLucE"}

To Reproduce
Steps to reproduce the behavior:
Follow instructions from https://github.com/carsten-klaffke/send-intent

Expected behavior
url property of result has the actual URL of the page being shared from Chrome

Screenshots

Desktop (please complete the following information):
Android

Smartphone (please complete the following information):
Android simulator

Additional context
Add any other context about the problem here.

Hi! Please give me a public URL where this occurs to reproduce it! Do you share a file or a weblink from Chrome?

I share a web link - when I share it to Gmail app, for example, the Gmail app gets both the title (that it puts in the subject), and then the actual Url (that it puts into the body of the email).
From Chrome on Android, I click the 3 dots, and then share, and then my app. (Good thing is that, after following your instructions, my app does show up in the share menu!) After I click my app, my app is opened, and I see that SendIntent.checkSendIntentReceived() is fired, but only that the result object is as I mentioned above.

Unfortunately, I do not have a public URL to try this, yet.

This is what I have:
In app.component.ts:
inside the constructor

    this.handleIntentReceived();
    window.addEventListener("sendIntentReceived", () => {
      this.handleIntentReceived();
    });

  handleIntentReceived() {
    SendIntent.checkSendIntentReceived().then((result: any) => {
      if (result) {
        console.log('SendIntent received');
        const resultString = JSON.stringify(result);
        this.myUtilsService.presentErrorAlert(resultString)
        console.log(resultString);
      }
      if (result.url) {
        const resultUrl = decodeURIComponent(result.url);
        console.log('Result URL: ' + resultUrl);
        Filesystem.readFile({path: resultUrl})
          .then((content) => {
            console.log('Read file contents...')
            console.log(content);
          })
          .catch((err) => console.error(err));
      }
    });

  }

My MainActivity.java

public class MainActivity extends BridgeActivity {
  @Override
  protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    String action = intent.getAction();
    String type = intent.getType();
    if ((Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action)) && type != null) {
      bridge.getActivity().setIntent(intent);
      bridge.eval("window.dispatchEvent(new Event('sendIntentReceived'))", new ValueCallback<String>() {
        @Override
        public void onReceiveValue(String s) {
        }
      });
    }
  }
}

In AndroidManifest.xml

            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/*" />
            </intent-filter>

For what it is worth, I have modified SendIntent.java to comment out writing to file part, and just taking the Intent.EXTRA_TEXT as below. And that works - as of now, I am not interested in images or other binary data, so this is fine with me.
However, feel that there may need to be a check for type variable, inside of readItemAt, and write to file only if a binary type (video, image, etc.)

Below is my edited code

        /* Original START
        if (uri != null) {
            final Uri copyfileUri = copyfile(uri);
            url = (copyfileUri != null) ? copyfileUri.toString() : null;
        } else {
            url = intent.getStringExtra(Intent.EXTRA_TEXT);
        }
         Original END */

        /* Modified START */
        url = intent.getStringExtra(Intent.EXTRA_TEXT); // If you uncomment above, remove this line
        /* Modified END */

I was able to reproduce it and just published a fix in "send-intent@3.0.8". Thank you for reporting. Please test it and close this issue if successful!

And regarding your last comment: There can also be files with type "text/plain". So this solely is not sufficient to distinguish. I now took both, the type and existance of Intent.EXTRA_TEXT to decide whether it is a web link or a file.

Perfect! Thank you very much! It worked.