Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deep linking - doesn't work if app is closed

I'm implementing deep linking with expo in my react native app. I've managed to do it using this code with this tutorial and this documentation for adjusting it to my nested stacks:

const linking = {
  prefixes:[prefix],
  config: {
    screens: {
      Drawer: {
        screens: {
          Tabs: {
            screens: {
              Profile:"profile"
            }
          }
        }
      },
    }
  }
}
return (
  <NavigationContainer linking={linking}>
    <RootStackScreen actions={actions} showLoader={showLoader} user={user} {...props} />
  </NavigationContainer>
)

}

If I use myscheme://profile it works as expected, but only if the app is opened in the background. When the app is closed, then it just open it in my initial home screen, I tried googling and searching but couldn't find any explanation that fits what I did. I also tried adding the getInitialRoute function to linking, which triggers when the app was closed and was opened from a deep link, but couldn't figure how I can use it to activate the navigation.

async getInitialURL() {
  const url = await Linking.getInitialURL(); // This returns the link that was used to open the app
  if (url != null) {
    //const { path, queryParams } = Linking.parse(url);
    //console.log(path,queryParams)
    //Linking.openURL(url) 
    return url;
  }
  
},
like image 847
sagi Avatar asked Jun 01 '21 13:06

sagi


1 Answers

I suppose that you confirmed that your function getInitialURL is getting called when your app is launched? Also, the commented code within the if (url != null) { aren't supposed to be commented right?

If the above is fine then the issue could be related to the debugger being enabled. As per React Native's documentation (https://reactnative.dev/docs/linking#getinitialurl):

getInitialURL may return null while debugging is enabled. Disable the debugger to ensure it gets passed.

like image 114
Ketan Malhotra Avatar answered Oct 23 '22 05:10

Ketan Malhotra