React native deep linking not working when app is in background state

See original GitHub issue

🐛 Bug Report

When the app is closed, I am able to get deep link url that is clicked by Linking.getInitialURL(). When the app is in the background state, then nothing is mounted. So, I am not able to get the url even by the Linking.addEventListener(‘url’, method_name).

What is the way to achieve this?

To Reproduce

Expected Behavior

Code Example

componentDidMount() { Linking.addEventListener(‘url’, this._handleOpenURL); }, componentWillUnmount() { Linking.removeEventListener(‘url’, this._handleOpenURL); }, _handleOpenURL(event) { console.log(event.url); } I have added this code in app.js

Environment

React Native Environment Info: System: OS: Linux 4.15 Ubuntu 18.04.1 LTS (Bionic Beaver) CPU: (4) x64 Intel® Core™ i3-6098P CPU @ 3.60GHz Memory: 1.73 GB / 15.57 GB Shell: 4.4.19 - /bin/bash Binaries: Node: 10.11.0 - /usr/bin/node npm: 6.7.0 - /usr/bin/npm npmPackages: react: 16.6.0-alpha.8af6728 => 16.6.0-alpha.8af6728 react-native: ^0.58.5 => 0.58.5 npmGlobalPackages: create-react-native-app: 2.0.2 react-native-cli: 2.0.1 react-native-rename: 2.4.0 react-native-slideshow: 1.0.1

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:20
  • Comments:59 (1 by maintainers)

github_iconTop GitHub Comments

24reactions
glocorecommented, May 14, 2019

For me, the issue was that although the app would come to the foreground on clicking on the app link (say from an SMS), the callback passed to Linking.addEventListener never got fired.

So what I ended up doing is this: add an AppState listener to check the url from Linking.getInitialURL, and if it is a valid URL, redirect accordingly.

componentDidMount() {
  // triggered when react native boots
  this._checkInitialUrl()

  AppState.addEventListener('change', this._handleAppStateChange)
  // never gets fired on Android, hence useless to me
  // Linking.addEventListener('url', url => console.log('url received: ', url))
}

componentWillUnmount() {
  AppState.removeEventListener('change', this._handleAppStateChange)
}

_handleAppStateChange = async (nextAppState) => {
  if (
    this.state.appState.match(/inactive|background/) &&
    nextAppState === 'active'
  ) {
    this._checkInitialUrl()
  }
  this.setState({ appState: nextAppState })
}

_checkInitialUrl = async () => {
  const url = await this._getInitialUrl()
  this._handleUrl(url)
}

_getInitialUrl = async () => {
  const url = await Linking.getInitialURL()
  return url
}

_handleUrl = (url) => {
  // write your url handling logic here, such as navigation
  console.log('received URL: ', url)
}
19reactions
krishnakumarrkcommented, Jan 15, 2020

Pls try this. componentDidMount() { Linking.addEventListener(“url”, this.handleOpenURL); this.handleDeepLinkingRequests(); } handleDeepLinkingRequests = () => { Linking.getInitialURL() .then(url => { if (url) { this.handleOpenURL(url); } }) .catch(error => { // Error handling }); } };

handleOpenURL = (url) => { // your navigation logic goes here }

Notes: -> Linking.getInitialURL() method should only be called for the first time when the app is launched via app-swap -> For subsequent app-swap calls, handleOpenURL() method will be called as it is configured with linking event listener. -> remember to unsubscribe linking events in componentwillunmount()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deep linking is not working when app is closed/killed #32350
It should open the specific screen through deep link when the app is in closed/killed state also. Snack, code example : linking.js. const...
Read more >
Deep linking not working when app is in background state ...
Deep linking not working when app is not in background React native iOS only. it only show first screen.but if app is in...
Read more >
React Navigation 5 - Deep linking not accepting given path ...
Coding example for the question React Navigation 5 - Deep linking not accepting given path and resolving to state as expected-React Native.
Read more >
How To Handle Deep Linking in a React Native App
In this tutorial, let's learn how to handle deep linking in a React Native app by creating an example app. We will create...
Read more >
Implementing Deep Linking in React Native apps - VentureDevs
Now, test if the custom scheme is working properly. Go to Safari on your device or in simulator and type in 'vd://'. This...
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