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:
- Created 4 years ago
- Reactions:20
- Comments:59 (1 by maintainers)
Top Related StackOverflow Question
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.addEventListenernever got fired.So what I ended up doing is this: add an
AppStatelistener to check the url fromLinking.getInitialURL, and if it is a valid URL, redirect accordingly.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()