React Native 当应用程序在后台使用android中的深度链接时,getInitialUrl不起作用

amrnrhlw  于 2023-10-22  发布在  React
关注(0)|答案(2)|浏览(162)

当应用程序在后台时,我想从Chrome或消息通过链接导航到应用程序,但当我删除示例时,它正在导航

if (Platform.OS === 'android') {
      Linking.getInitialURL().then(url => {
        this.navigate(url);
      });
    } else {
      Linking.addEventListener('url', this.handleOpenURL);
    }
    //  AppState.addEventListener('change', this._handleAppStateChange);
  }

  componentWillUnmount() {
    Linking.removeEventListener('url', this.handleOpenURL);
    //  AppState.removeEventListener('change', this._handleAppStateChange);
  }

  navigate = url => {
    const {navigate} = this.props.navigation;
    var routeName;
    if (url != null) {
      console.log('Url not null:' + url);
      const route = url.replace(/.*?:\/\//g, '');
      const id = route.match(/\/([^\/]+)\/?$/)[1];
      routeName = route.split('/')[1];
    }

    if (routeName != null && routeName === 'B') {
      ///  alert('routeName: ' + routeName);
      navigate('People');
    }
  };

当我按下链接,它应该导航到应用程序,如果甚至应用程序是在后台

p4rjhz4m

p4rjhz4m1#

应用这些方法,而应用程序是在后台工程在我的情况下

componentDidMount() {
  Linking.addEventListener('url', this._handleOpenURL);
},
componentWillUnmount() {
  Linking.removeEventListener('url', this._handleOpenURL);
},
_handleOpenURL(event) {
  console.log(event.url);
}
rwqw0loc

rwqw0loc2#

这里是一个解决方案与挂钩,这将使您始终有从关闭或后台状态打开应用程序的URL:

useEffect(() => {
    const getUrlAsync = async () => {
        const initialUrl = await Linking.getInitialURL();
        console.log(initialUrl);
    };
    getUrlAsync();

    const handleOpenURL = (event) => { console.log(event.url); };
    Linking.addEventListener("url", handleOpenURL);
    return () => Linking.removeEventListener("url", handleOpenURL);
}, []);

相关问题