2

We have an app utilizing Branch.io universal links. Following the documentation on: https://help.branch.io/developers-hub/docs/react-native#read-deep-link

On the react-native app you setup a subscriber to receive deep and universal links in the javascript runtime.

Using the latest react-navigation from here: https://reactnavigation.org/docs/deep-linking

React Navigation would like to natively handle deep links. React Navigation does not appear to expose a good way to manually launch a link.

How can I utilize these two services together? Taking a deep link and decomposing it into a routable deep link is proving challenging. Our app has nested routers and redoing the translation from path to screens and parameters is something I don't want to do. Has anyone done this recently? Thanks.

Bramski
  • 179
  • 1
  • 10

1 Answers1

6

You can notify React Navigation about incoming links using the subscribe option:

const linking = {
  // Custom susbcription to handle incoming links
  subscribe(listener) {
    branch.subscribe(({ error, params, uri }) => {
      if (error) {
        console.error('Error from Branch: ' + error);
        return;
      }

      if (params['+non_branch_link']) {
        const nonBranchUrl = params['+non_branch_link'];
        // Route non-Branch URL if appropriate.
        return;
      }

      if (!params['+clicked_branch_link']) {
        // Indicates initialization success and some other conditions.
        // No link was opened.
        return;
      }

      // A Branch link was opened
      const url = params.$canonical_url;

      listener(url);
    });

    return () => {
      // Clean up the event listeners
      branch.unsubscribe();
    };
  },
  // Options such as prefixes, screens config etc.
  // ...
};

Docs: https://reactnavigation.org/docs/deep-linking/#third-party-integrations

satya164
  • 9,464
  • 2
  • 31
  • 42
  • It should be available in 5.8.0. How are you passing it? – satya164 Oct 26 '20 at 23:36
  • Nevermind. I had to nuke and rebuild node_modules and reset the haste cache. This appears to be new in 5.8.0 and I had previously been on 5.7.6 – Bramski Oct 26 '20 at 23:40
  • is this available with navigationcontainer of react-navigation. Navigationcontainer provide a linking prop to extract parameters from link. How to pass from branch to that linking prop of navigationcontainer – Yusuf Dec 22 '20 at 16:59
  • 1
    @Yusuf you pass this linking object to `NavigationContainer` – satya164 Dec 22 '20 at 20:47
  • 1
    Please note that `branch.unsubscribe` no longer works and can break the app. The new updated way to unsubscribe is documented here https://github.com/adriaandebolle/react-navigation.github.io/commit/ef59927601004f338463f699613adae0569ca98c#diff-1e930de1b42414222faf00adf1c17cc253af302f52bcba7a239712645386c8c1 – Obafemi Joseph Olorungbon Jan 30 '23 at 09:49