Deep linking - doesn't work if app is closed

Viewed 2894

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;
  }
  
},
3 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.

There are a couple of things you can check.

  • Verify that the structure for linking.config matches your navigation structure. I've had a similar issue in the past, and resolved it by making sure my config structure was correct.

  • Ensure that the linking object is setup properly. Refer to the docs to verify. From the looks of it, the linking object you've showed doesn't have the getInitialURL property in it.

  • Confirm that you've setup the native side of things as documented.

Hopefully something works out! Let me know if it doesn't.

I was having the same problem. In iOS(flutter build) I solved this by adding "Content Available." The article is here: Apple Content Available Document. I am using OneSignal so in the api I added that field. Now even if the app is forced closed it awakes and deep links work. For Onesignal I had to use "content_available" : true. The complete Onesignal postman code is:

{
  "app_id": "1234",
  "included_segments": ["Test"],
  "content_available" : true,
  "contents": {
                "en": "Hi"
            },
            "data": {
                "dynamic_link": "https://google.com"
            },
            "headings": {
                "en": "Testing"
            }
}
Related