React Native application facing issues when opening Google Docs links on Android devices

I’m developing a React Native application that includes links to various websites, allowing users to click and navigate to specific pages. However, I’m encountering problems with Google Docs links on Android (iOS testing is pending). When users click any of these links, an error appears. If they attempt to open the link through the Docs app, the error states, “Document lookup failed. The document may have been removed,” while opening it in Chrome results in the message, “Sorry, the requested file does not exist.” Interestingly, if the link URL is copied and pasted directly into the Chrome search bar, the document appears correctly.

Here’s how the linking is implemented:

import { Linking } from 'react-native';

const handleLinkPress = async () => {
    let refinedLink = link.toLowerCase();
    if (!/^https?:\/\//.test(refinedLink) && !/^mailto:/.test(refinedLink)) {
        refinedLink = `https://${refinedLink}`;
    }
    Linking.openURL(refinedLink);
};

The Android Manifest.xml file contains:

<manifest ...>
    ...
    <uses-permission android:name="android.permission.INTERNET" />
    ...
    <queries>
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="https"/>
        </intent>
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="http"/>
        </intent>
    </queries>
</manifest>

Version details:

react-native: 0.66.5 (I realize an upgrade is necessary, but I’m uncertain if it will resolve this problem)
minSdkVersion = 21
compileSdkVersion = 33
targetSdkVersion = 33

I have verified that the link the application attempts to open is accurate. I looked into the React Native Linking documentation as well as the Android documentation, but it appears that the Android documentation primarily emphasizes incoming deep links rather than outgoing links.

hey! it sounds frustrating. it might be related to how android intents are resolving. try using encodingURIComponent to properly encode the links, sometimes they have special chars or spaces that mess things up. also, ensure you’re using ‘https://drive.google.com/’ links instead of ‘docs.google.com’ links. hope this helps!

It seems your issue might be occurring due to the way React Native handles the asynchronous nature of opening URLs. I would recommend checking if your issue persists by wrapping your Linking.openURL call in a try-catch block to ensure it handles any potential rejection. Another aspect to explore is confirming that the links have the correct sharing permissions, as restricted documents may not display appropriately via app routing. Furthermore, updating React Native might not be the silver bullet here but looking into the latest changes could offer additional insights or fixes.

i also had similar trouble. weirdly enuf, clearing the cache for the docs app worked for me. sometimes stale data causes hiccups in accessing docs that r actually available. go to apps settings and try clearing cache for google docs app. might just do the trick :slight_smile:

If you’re still experiencing issues with Google Docs links on Android, consider experimenting with a custom URL scheme handler. Sometimes, the default linking method can be unreliable when dealing with links that require a specific app to open. Implement an explicit intent chooser to let users pick how they want the link to open (like Chrome or Google Docs app). Additionally, pay attention to any potential redirect issues from docs.google.com that might affect the handling of your links. This approach generally offers users more control and can tackle unusual link behavior.