question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Linking.getInitialURL() for android

See original GitHub issue

Hi, On Android Linking.getInitialURL() has a problem and not return anything. More precisely, none of the following logs are executed.

  Linking.getInitialURL().then((url) => {
      console.log('Initial url is: ' + url);
  }).catch(err => console.error('An error occurred', err));

but when i change https://github.com/facebook/react-native/blob/master/Libraries/Linking/Linking.js#L86-L92 to:

  getInitialURL(): Promise<?string> {
    return NativeLinking.getInitialURL();
  }

the problem was fixed.

React Native version:

System:
    OS: Linux 4.18 Ubuntu 18.04.2 LTS (Bionic Beaver)
    CPU: (8) x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
    Memory: 582.26 MB / 15.50 GB
    Shell: 4.4.20 - /bin/bash
  Binaries:
    Node: 10.16.0 - /usr/bin/node
    Yarn: 1.17.3 - /usr/bin/yarn
    npm: 6.9.0 - /usr/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    Android SDK:
      API Levels: 28
      Build Tools: 28.0.3, 29.0.0, 29.0.1
      System Images: android-28 | Intel x86 Atom, android-28 | Intel x86 Atom_64, android-28 | Google APIs Intel x86 Atom, android-28 | Google APIs Intel x86 Atom_64, android-28 | Google Play Intel x86 Atom, android-28 | Google Play Intel x86 Atom_64, android-29 | Google APIs Intel x86 Atom
      Android NDK: 20.0.5594570
  npmPackages:
    react: 16.8.6 => 16.8.6 
    react-native: 0.60.3 => 0.60.3 
  npmGlobalPackages:
    react-native-cli: 2.0.1

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:71
  • Comments:73 (6 by maintainers)

github_iconTop GitHub Comments

21reactions
xanderdeseyncommented, Apr 7, 2020

Any progress on this? Seems mind boggling to me that deeplinking is just completely broken in RN for Android?

It’s not completely broken for me. It works for an already started app, but not for a newly started one.

Well I’d argue that is almost completely broken then. Deeplinking is usually used when the app is not open.

18reactions
jtbeachcommented, May 27, 2020

I think there are probably 2 different issues/bugs here:

  1. The promise returned by Linking.getInitialURL() seems not to resolve for some people (see example of @crisvergara above)
  2. The promise returned by Linking.getInitialURL() does resolve and either returns null or a default Expo URL starting with https://expo.io (only if using the managed Expo workflow)

I think it probably makes sense to separate these 2 issues or at least declare which of the 2 people are seeing. For me, I am seeing number 2 above.

I did some testing around this and have come to similar conclusions to @Stephen2:

  • It does not seem related to a particular Android version, but actually depends on timing. That’s why it seems to happen on some phones rather than others I think. In addition to it only happening on certain phones, I found that you can get inconsistent results on the same phone if you repeat enough.
  • It is resolved by moving back from React Native 0.61 to React Native 0.59. Unfortunately, for those using Expo, this means going back to the Expo 35 SDK, which Apple is no longer accepting submissions from due to usage of deprecated UIWebView (see https://github.com/expo/expo/issues/5497)
  • It is not related to Expo and can be reproduced by a trivial example in latest React Native after running react-native init and making some changes to App.js and AndroidManifest.xml (to register intent to view certain URLs). I have pushed this sample app at https://github.com/stellaai/rndeeplink

Since the bug seems timing dependent and having put tracing debug statements throughout the native code, my hunch is that this relates to whether or not the React bridge is ready when the native code wants to set the initial URL by sending a message to the JS app via EventEmitter (if my reading of the code is correct and this is how it is set).

A workaround/hack (if using vanilla React Native or willing to eject from Expo managed workflow) might be to override the getLaunchOptions method in your own ReactActivityDelegate subclass to explicitly get the URI from the Intent and pass it through as a prop to the root React component rather than relying on Linking.getInitialUrl(). See https://medium.com/@anutoshdatta/pass-initial-properties-to-react-native-from-java-or-objective-c-code-12880ba6198e for some details on this.

I have put a video of these inconsistent results up here: https://github.com/stellaai/rndeeplink/blob/master/deeplinkbug.mp4 – note that pressing the the back button on Android destroys the activity, so this is showing a cold start every time.

Have also logged a bug in Expo prior to determining that this was a bug with RN upstream with some more logs, etc.: https://github.com/expo/expo/issues/8408

Read more comments on GitHub >

github_iconTop Results From Across the Web

Linking - React Native
Android calls it Deep Links (Universal Links - iOS). ... getInitialURL() - it returns a Promise that resolves to the URL, if there...
Read more >
React-native linking getinitialurl() works even when I don't call ...
When I tap the link for first in-browser my app run but incoming URL don't clear after that and linking.GetInitialUrl() always run with...
Read more >
Linking · React Native
getInitialURL().then((url) => { if (url) { console.log('Initial url is: ' + ... for deep linking on Android, refer to Enabling Deep Links for...
Read more >
Linking - Expo Documentation
Android Device, Android Emulator, iOS Device, iOS Simulator, Web ... Linking.getInitialURL(). Get the URL that was used to launch the app if it...
Read more >
Deep Linking Your React Native App | by Nader Dabit - Medium
Now, let's go into index.ios.js / index.android.js and import the router will ... getInitialURL().then(url => { this.navigate(url); }); } else { Linking.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found