Search code examples
react-nativedeep-linkingreact-navigation-v6

How can I determine the route of the application through deep linking based on the link


This is my code ‍App.js‍ :

function App() {
  const getInitialURL = async () => {
    const url = await Linking.getInitialURL();

    console.log('This is url',url)

    if (url !== null) {
      return url;
    }

    return undefined
  }
  const deepLinking = {
    prefixes: ['https://www.example.com','https://*.example.com','http://www.example.com','app://'],
    config: {
      initialRouteName:'HOME',
      screens: {
        HOME: 'home',
        LIVE: 'live/:uuid',
        TAGList: 'tag/:enVal'
      }
    },
    getInitialURL
  }
  
  return (
    <NavigationContainer linking={deepLinking}>
      <stack.Navigator 
      initialRouteName={'SPLASH'} 
      screenOptions={{headerShown: false}}>
        <stack.Screen name = "SPLASH" component={Splash}/> 
        <stack.Screen name = 'HOME' component = {Home}/>
        <stack.Screen name = "LIVE" component={LiveOne}/>
        <stack.Screen name = "TAGList" component={TagList}/>
      </stack.Navigator>
    </NavigationContainer>
  );
}

export default App;

android/app/AndroidManifest.xml

<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="app" />
  <data android:scheme="https" android:host="www.example.com" />
  <data android:scheme="http" android:host="www.example.com" />
  <data android:scheme="https" android:host="example.com" />
  <data android:scheme="http" android:host="example.com" />
</intent-filter>

So, with this code, when I click on the link started by example.com, the app opens HOME screen, but I need for example, when the example.com is clicked, the HOME screen opens, and when the example.com/m/uuid is clicked, the app opens and change the screen to the LIVE screen and send the uuid to the same screen

For now, I can only open LIVE screen app with same params with this command : npx uri-scheme open "app://LIVE/3a35ed925895A26" --android I need when click on the example.com/m/3a35ed925895A26 app opened LIVE screen with 3a35ed925895A26 params


Solution

  • Well, I found the solution myself

    1- I had to make changes in the code in order to be able to access the app both through app:// and through various link models (with http without http - with https without https - with www without www).

    In AndroidManifest.xml file :

    <intent-filter android:autoVerify="true">
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    
    //for app://
    <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="app" />
    </intent-filter>
    
    //for links
    <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="www.example.com" />
      <data android:scheme="http" android:host="www.example.com" />
      <data android:scheme="https" android:host="example.com" />
      <data android:scheme="http" android:host="example.com" />
    </intent-filter>
    

    2- And deep linking prefixes :

      prefixes: [
          'https://www.example.com',
          'http://www.example.com',
          'https://*.example.com',
          'http://example.com',
          'https://example.com',
          'app://'
        ]
    

    3- Now for example my link is something like this : www.example.com/m/876c875dDfV3, In order to be able to access the route /m through deep link, I need to define its route in screens as m/:uuid. /m is route in my links, And :uuid is my params to this route, You can use your params or you're link structure.

        config: {
          initialRouteName:'home',
          screens: {
            HOME: 'home',
            LIVE: 'm/:uuid',
            TAGList: 'tag/:enVal'
          }
        }