Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linking.getInitialURL() for android #25675

Closed
sajaddp opened this issue Jul 16, 2019 · 87 comments
Closed

Linking.getInitialURL() for android #25675

sajaddp opened this issue Jul 16, 2019 · 87 comments
Labels
API: Linking Bug Never gets stale Prevent those issues and PRs from getting stale Platform: Android Android applications. Platform: Linux Building on Linux.

Comments

@sajaddp
Copy link

sajaddp commented Jul 16, 2019

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
@sajaddp sajaddp added the Bug label Jul 16, 2019
@react-native-bot react-native-bot added API: Linking Platform: Android Android applications. Platform: Linux Building on Linux. labels Jul 16, 2019
@tgensol
Copy link

tgensol commented Sep 6, 2019

I can confirm that on Android, if the app is closed, InteractionManager.runAfterInteractions, will not work and then the getInitialURL will not return anything.

But if the app was opened, everything works just fine.

I removed the InteractionManager.runAfterInteractions and the deep linking on Android, when the app is closed, works again

Thanks @sajaddp

@i7soft
Copy link

i7soft commented Sep 26, 2019

I fixed by that:

var NativeLinking=require("./node_modules/react-native/Libraries/Linking/NativeLinking").default;
NativeLinking.getInitialURL().then((url) => {
      console.log('Initial url is: ' + url);
  }).catch(err => console.error('An error occurred', err));

@shubham234
Copy link

I'm also facing this issue on RN v0.59.8. I'm trying to use aws-amplify library and they make use of Linking.getInitialUrl() to listen to the Deeplink URL but it is always returning null. Can anyone suggest some solutions that I can try?

@joaocac
Copy link

joaocac commented Jan 28, 2020

Same here iOS is working great but Android fails.

System:
    OS: macOS 10.15.2
    CPU: (8) x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
    Memory: 68.70 MB / 16.00 GB
Packages:
    react: 16.9.0 => 16.9.0
    react-native: 0.61.2 => 0.61.2
    react-native-cli: 2.0.1

@mileszs
Copy link

mileszs commented Jan 28, 2020

We are seeing what we think is the same issue—works great on iOS, fails on Android cold starts. In particular, it fails on Android 10. It works on an old Android 7 phone I have.

@Stephen2
Copy link

Any update on this? Or workaround?

@Stephen2
Copy link

Stephen2 commented Feb 19, 2020

Interestingly, getInitialUrl works on Google Pixel XL2 but doesn't work on Samsung S9 - both running Android 10.

I even changed code to:
import NativeLinking from "react-native/Libraries/Linking/NativeLinking";
NativeLinking.getInitialURL().then(handleDeepLinking);
(^^ code is cut down, I actually went as far as to put url into state and display in a <Text> for debug build, but nothing)

But to no avail... Samsung S9 refuses to call the handleDeepLinking function from cold start :( Warm start is fine, and anyway warm start is handled by Linking.addEventListener("url", ev => {

@Stephen2
Copy link

"react": "16.9.0",
"react-native": "0.61.4",
"expo": "^36.0.0",

@Stephen2
Copy link

UPDATE:

And please note the differing behaviour on different Android devices during my testing. This was a consistent failure on Samsung, but consistently fine on Google Pixel

I will attempt to do a git log --follow on a few files between these 2 react-native versions, to get more info, but at this point I don't know which files/folders are most likely culprits.

@aurasphere
Copy link

I can confirm that I have this issue since I've updated from 0.59.* to 0.61.*

@phillipkey
Copy link

Is there any update on this issue? I'm experiencing the same. Not being able to utilize deep linking is a huge miss right now.

@bsonmez
Copy link

bsonmez commented Mar 17, 2020

Is there any update? The bug still continues

@openwell
Copy link

Finally am not dumb.... Is there any update? The bug still continues

@ryzhak
Copy link

ryzhak commented Mar 20, 2020

The same issue. Linking.getInitialURL works fine on ios, but on android(emulator SDK 28) this method seems not even to be called.

"react-native": "0.61.5",
"react-native-navigation": "^4.5.3",

@danieltrizzuto
Copy link

Same here on 0.61.5

@xanderdeseyn
Copy link

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

@norbertschuler
Copy link

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.

@xanderdeseyn
Copy link

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.

@rsmelo92
Copy link
Contributor

The culprit seems to be this line here

? InteractionManager.runAfterInteractions().then(() =>

InteractionManager.runAfterInteractions takes too long and Linking.getInitialURL never resolves the desired effect!

After removing it it worked 100% of the times, so @Stephen2 workaround actually fixes it by the time being...

import NativeLinking from 'react-native/Libraries/Linking/NativeLinking';
NativeLinking.getInitialURL().then(url => {
      handleDeepLink({ url });
 });

@Stephen2
Copy link

import NativeLinking from 'react-native/Libraries/Linking/NativeLinking';
NativeLinking.getInitialURL().then(url => {
      handleDeepLink({ url });
 });

I've tried that earlier

But to no avail... Samsung S9 refuses to call the handleDeepLinking function from cold start

@lnwanghao
Copy link

It's seems still a bug when app is from background to foreground Linking.getInitialURL return null

@Balthazar33
Copy link

In our case, it turned out that our intentFilters were not configured properly. Everything works fine on Android without any workarounds.

Hi @footniko
Could you please mention what was missing?

@Balthazar33
Copy link

I ended up using this workaround:

In App.js:

import NfcManager from 'react-native-nfc-manager';

useEffect(() => {
    getUrl();
  }, []);
  
async function getUrl() {
    let event = await NfcManager.getLaunchTagEvent();
    console.log('Event', event);
    //Outputs {"id": "A1B2C3D4", "techTypes": ["android.nfc.tech.IsoDep", "android.nfc.tech.NfcA"]}
  }

My AndroidManifest.xml looks like this:

<uses-permission android:name="android.permission.NFC" />

<application>
  <activity>
       <intent-filter>
          <action android:name="android.nfc.action.TAG_DISCOVERED" />
          <action android:name="android.nfc.action.NDEF_DISCOVERED" />
          <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
 </application>

RN 0.68.0

@kivohin
Copy link

kivohin commented Apr 14, 2022

In our case, it turned out that our intentFilters were not configured properly. Everything works fine on Android without any workarounds.

Hi @footniko Could you please mention what was missing?

To expand on @footniko answer, we are using Expo SDK v44 (React Native v0.64).
The reason Linking.getInitialURL() was always returning null on Android was because we never configured the intentFilters for Android.

In order to do so when using Expo you need to follow the instructions here:
https://docs.expo.dev/guides/linking/#deep-links-on-android
https://docs.expo.dev/versions/latest/config/app/#intentfilters

Keep in mind, in Expo when you change the app.json or app.config.ts files you need to do a full app store release in order for the changes to apply. You can not do an over-the-air update.

If you are not using Expo then you will probably need to apply the same Intent Filter changes but in the AndroidManifest.xml file.

@soulfresh
Copy link

import { Linking, Platform } from 'react-native'
import NativeIntentAndroid from 'react-native/Libraries/Linking/NativeIntentAndroid'

const NativeLinking = Platform.OS === 'android' ? NativeIntentAndroid : Linking

NativeLinking.getInitialURL().then(url => console.log(url))

I was struggling with this issue recently as well and found two things. 1) The above resolved the issue on Android for me. 2) The root cause of my issue actually turned out to be a custom animation using React Native Animation running at the same time that Linking was trying to get the initial URL. I haven't tracked down exactly what it is about the animation that was preventing InteractionManager.runAfterInteractions from firing on Android but preventing my animation from running resolved the Linking error without having to resort to NativeIntentAndroid.

Hope that helps someone.

javache added a commit to javache/react-native that referenced this issue Nov 9, 2022
Summary:
This was originally added in D15258046 (facebook@c802d0b) but seems to be the wrong solution to the problem from my perspective. InteractionManager does not provide timing information on the activity being available, but ReactContext's LifecycleEventListener does.

This should also address some of the issues raised in facebook#25675

Changelog: [Android][Fixed] Linking.getInitialUrl should not wait for InteractionManager

Differential Revision: D41157646

fbshipit-source-id: 486088e2948c0c0c83bbfbd25036e1f34017429f
javache added a commit to javache/react-native that referenced this issue Nov 9, 2022
Summary:
Pull Request resolved: facebook#35289

This was originally added in D15258046 (facebook@c802d0b) but seems to be the wrong solution to the problem from my perspective. InteractionManager does not provide timing information on the activity being available, but ReactContext's LifecycleEventListener does.

This should also address some of the issues raised in facebook#25675

Changelog: [Android][Fixed] Linking.getInitialUrl should not wait for InteractionManager

Differential Revision: D41157646

fbshipit-source-id: 610ee440c38cb6e5b7e0b8d808e0e2e73599404c
facebook-github-bot pushed a commit that referenced this issue Nov 14, 2022
Summary:
Pull Request resolved: #35289

This was originally added in D15258046 (c802d0b) but seems to be the wrong solution to the problem from my perspective. InteractionManager does not provide timing information on the activity being available, but ReactContext's LifecycleEventListener does.

This should also address some of the issues raised in #25675

Changelog: [Android][Fixed] Linking.getInitialUrl should not wait for InteractionManager

Reviewed By: mdvacca

Differential Revision: D41157646

fbshipit-source-id: 6e23969212570204a7e076b6d4d9db004412da09
@vincent-odukwe
Copy link

In our case, it turned out that our intentFilters were not configured properly. Everything works fine on Android without any workarounds.

what did you fix in your intentFilters @footniko ?

@footniko
Copy link

In our case, it turned out that our intentFilters were not configured properly. Everything works fine on Android without any workarounds.

what did you fix in your intentFilters @footniko ?

Check this reply: #25675 (comment)

@rveruna
Copy link

rveruna commented Apr 25, 2023

Still an issue in 0.68.5.
I went aroud it by using
import NativeIntentAndroid from 'react-native/Libraries/Linking/NativeIntentAndroid';
NativeIntentAndroid.getInitialURL().then(handleUrl);

OlimpiaZurek pushed a commit to OlimpiaZurek/react-native that referenced this issue May 22, 2023
Summary:
Pull Request resolved: facebook#35289

This was originally added in D15258046 (facebook@c802d0b) but seems to be the wrong solution to the problem from my perspective. InteractionManager does not provide timing information on the activity being available, but ReactContext's LifecycleEventListener does.

This should also address some of the issues raised in facebook#25675

Changelog: [Android][Fixed] Linking.getInitialUrl should not wait for InteractionManager

Reviewed By: mdvacca

Differential Revision: D41157646

fbshipit-source-id: 6e23969212570204a7e076b6d4d9db004412da09
@github-actions
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Oct 23, 2023
@GuillaumeHemmen
Copy link

This issue is still valid IMO; please remove the stale label.

@github-actions github-actions bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Oct 23, 2023
@cortinico cortinico added the Never gets stale Prevent those issues and PRs from getting stale label Oct 24, 2023
@MaryKilduff68
Copy link

Has there been any update on this issue?

@fabOnReact
Copy link
Contributor

fabOnReact commented Jan 31, 2024

could you provide a minimum reproducible example?
Can you reproduce this issue using an existing minimum reproducible example (like this one #25675 (comment)) and adding steps explained by #25675 (comment)?

It is documented also in the react-native documentation https://reactnative.dev/docs/next/linking#getinitialurl

To support deep linking on Android, refer https://developer.android.com/training/app-indexing/deep-linking.html#handling-intents
getInitialURL may return null while debugging is enabled. Disable the debugger to ensure it gets passed.

@tmitchel2
Copy link

I've just started to get this issue after upgrading to 0.73, it is adding approximately 10s onto my app startup time waiting to resolve the promise.

It looks like in 0.73 the code has been moved to native code so the above workarounds will now no longer work.

Also for me I think the reason I get this is due to the fact im calling Linking.getInitialUrl as early as possible in my app start, even before a screen has been rendered. I don't really want to change this because the initial url can actually determine alot about how my app starts up in testing scenarios.

I can only think that it should be optional to wait for an activity? (im not up on android app lifecycles though)

@stephenkopylov
Copy link

Facing this issue sometimes while trying to get initial url as soon as possible on the startup

Seems like something relates to the this topic

Error
Cannot await activity from more than one call to getInitialURL

    IntentModule:84 com.facebook.react.modules.intent.IntentModule.waitForActivityAndGetInitialURL
    IntentModule:59 com.facebook.react.modules.intent.IntentModule.getInitialURL
    Method.java:-2 java.lang.reflect.Method.invoke
    JavaMethodWrapper:372 com.facebook.react.bridge.JavaMethodWrapper.invoke
    JavaModuleWrapper:149 com.facebook.react.bridge.JavaModuleWrapper.invoke
    SourceFile:-2 com.facebook.jni.NativeRunnable.run
    Handler.java:743 android.os.Handler.handleCallback
    Handler.java:95 android.os.Handler.dispatchMessage
    MessageQueueThreadHandler:27 com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage
    Looper.java:150 android.os.Looper.loop
    MessageQueueThreadImpl:234 com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run
    Thread.java:833 java.lang.Thread.run
    node_modules/react-native/Libraries/BatchedBridge/NativeModules.js:105:59 promiseMethodWrapper
    node_modules/react-native/Libraries/Linking/Linking.js:98:53 getInitialURL
    node_modules/@react-navigation/native/src/useLinking.native.tsx:31:29 anonymous
    node_modules/@react-navigation/native/src/useLinking.native.tsx:141:42 anonymous
    node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js:3771:68 mountState
    node_modules/react/cjs/react.production.min.js:25:401 useState
    node_modules/@react-navigation/native/src/useThenable.tsx:4:34 useThenable
    node_modules/@react-navigation/native/src/NavigationContainer.tsx:103:48 NavigationContainerInner
    node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js:3525:21 renderWithHooks
    node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js:4316:29 updateForwardRef
    node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js:7929:24 beginWork$1
    node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js:7304:24 performUnitOfWork
    node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js:7297:53 workLoopSync
    node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js:7279:18 renderRootSync
    node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js:6975:33 performSyncWorkOnRoot
    node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js:2145:30 flushSyncCallbacks
    node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js:8462:57 batchedUpdatesImpl
    node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js:1106:29 batchedUpdates
    node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js:1137:16 _receiveRootNodeIDEvent
    node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js:1178:27 receiveEvent
    (native) apply
    node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:433:33 __callFunction
    node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:113:25 anonymous
    node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:368:10 __guard
    node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:112:16 callFunctionReturnFlushedQueue

@TomasSestak
Copy link

still valid

@pwltr
Copy link

pwltr commented May 3, 2024

@TomasSestak I did extensive testing on this yesterday and didn't find an issue with getInitialUrl on Android. In my case it was a mistake on my part dealing with NFC. I suggest trying to create a minimal reproducible example to figure out if the bug is with your application.

@omerts
Copy link

omerts commented May 9, 2024

Just got it from crashyltics with latest RN.
issue_022302da0d55dfb152b0c7d7d51a24a7_crash_session_663CA92000DE0001293FA011CE78595B_DNE_1_v2_stacktrace.txt

@BlackMonday-ai
Copy link

BlackMonday-ai commented May 26, 2024

I am also facing this problem
"expo": "~51.0.8",
"react-native": "0.74.1",

other
"dependencies": {
"@expo/metro-runtime": "~3.2.1",
"@expo/vector-icons": "^14.0.0",
"@react-native-community/masked-view": "^0.1.11",
"@react-navigation/bottom-tabs": "^6.5.20",
"@react-navigation/drawer": "^6.6.15",
"@react-navigation/material-top-tabs": "^6.6.13",
"@react-navigation/native": "^6.1.17",
"@react-navigation/native-stack": "^6.9.26",
"expo": "~51.0.8",
"expo-application": "~5.9.1",
"expo-auth-session": "~5.5.2",
"expo-constants": "~16.0.1",
"expo-crypto": "~13.0.2",
"expo-dev-client": "~4.0.14",
"expo-device": "~6.0.2",
"expo-font": "~12.0.5",
"expo-linear-gradient": "~13.0.2",
"expo-linking": "~6.3.1",
"expo-notifications": "~0.28.3",
"expo-secure-store": "~13.0.1",
"expo-status-bar": "~1.12.1",
"expo-web-browser": "~13.0.3",
"lottie-react-native": "6.7.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-native": "0.74.1",
"react-native-gesture-handler": "~2.16.1",
"react-native-linear-gradient": "^2.8.3",
"react-native-modal": "^13.0.1",
"react-native-reanimated": "~3.10.1",
"react-native-safe-area-context": "4.10.1",
"react-native-screens": "3.31.1",
"react-native-swipe-up-down": "^1.2.0",
"react-native-tab-view": "^3.5.2",
"react-native-vector-icons": "^10.0.3",
"react-native-web": "~0.19.6",
"react-redux": "^9.1.0",
"redux": "^5.0.1"
},

import NativeIntentAndroid from 'react-native/Libraries/Linking/NativeIntentAndroid'
const NativeLinking = NativeIntentAndroid;
const getUrl = await NativeLinking.getInitialURL();
still got null

@BlackMonday-ai
Copy link

any work around?

@sajaddp sajaddp closed this as not planned Won't fix, can't repro, duplicate, stale Jun 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API: Linking Bug Never gets stale Prevent those issues and PRs from getting stale Platform: Android Android applications. Platform: Linux Building on Linux.
Projects
None yet
Development

No branches or pull requests