Deep Linking not working in React Native app on Android

4.5k Views Asked by At

I have setup my React Native app to use Deep Linking with expo-linking, but for some reason it just does not work on Android (yet to implement on iOS). Opening the link just opens it in the web browser, and doesn't open the app as it should. Any idea why?

app.json

"android": {
  "adaptiveIcon": {
    "foregroundImage": "./assets/adaptive-icon.png",
    "backgroundColor": "#FFFFFF"
  },
  "package": "com.example.myapp",
  "intentFilters": [
    {
      "action": "VIEW",
      "data": [
        {
          "scheme": "https",
          "host": "testlink.com",
        }
      ],
      "category": [
        "BROWSABLE",
        "DEFAULT"
      ]
    }
  ]
},

This did not update the AndroidManifest, so I manually edited it:

AndroidManifest.xml

<intent-filter>
  <action android:name="android.intent.action.MAIN"/>
  <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
   <action android:name="android.intent.action.VIEW"/>
   <category android:name="android.intent.category.DEFAULT"/>
   <category android:name="android.intent.category.BROWSABLE"/>
   <data android:scheme="https" android:host="testlink.com"/>
</intent-filter>

App.js

const linking = {
    prefixes: ["https://testlink.com"],    
};

useEffect(() => {
    Linking.addEventListener("url", handleDeepLink);

    return () => {
        Linking.removeEventListener("url", handleDeepLink);
    };
}, []);

return (
    <NavigationContainer /*linking={linking}*/>
        ....
    </NavigationContainer>
);

This worked fine with just the normal expo links, but is not working now I want a custom URL so that it opens in web browser on a computer, or on the app if it is installed.

1

There are 1 best solutions below

8
On

At the core, the iOS Safari browser has built-in deep linking which makes it possible to deep-link to custom app schema- myapp:///link-to-resources. Chromium-based browsers used mostly on android don't support custom app schema in URL input field.

The work-around is a setup simple web page that can redirect your app custom schema using Browser DOM Window API.

 const launchApp = (deepLink = "", fallBack = "") => {
  var now = new Date().valueOf();
  setTimeout(function () {
    if (new Date().valueOf() - now > 100) return;
    window.location = fallBack;
  }, 25);
  window.location = deepLink;
}


 const deepLink = "myapp:///path_to_ressource/";
 const fallbackLink = "http://play.google.com/store/apps/details?id=com.yourcompany.appname"

launchApp(deepLink,fallbackLink)