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

时间:2019-10-31 12:25:38

标签: react-native

当应用程序处于后台时,我想导航至应用程序 从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');
    }
  };

当我按下链接时,即使该应用程序处于后台,它也应导航到该应用程序

1 个答案:

答案 0 :(得分:0)

在我的情况下,当应用程序在后台运行时应用这些方法

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