如何在前台刷新本机深度链接应用程序?

How to Refresh Native Deeplink Application When In Foreground?

我在 React Native 上使用深度链接 android 时遇到问题。当应用程序处于前台状态时,当应用程序被深度链接触发时,应用程序不会在该屏幕上刷新。例如,我想要深入链接到主屏幕,但是当我的前景在个人资料屏幕上时,我的应用程序没有刷新?

这是我的深层链接代码:

  async componentDidMount() {
    //  This for deeplinking
    if (Platform.OS === 'android') {
      Linking.getInitialURL().then(url => {
        this.navigate(url);
      });
    } else {
      Linking.addEventListener('url', this.handleOpenURL);
    }
  navigate = (url) => {
    const { navigate } = this.props.navigation;

    const route = url.replace(/.*?:\/\//g, '');
    const id = route.match(/\/([^\/]+)\/?$/)[1];
    const routeName = route.split('/')[0];
    if (id === 'yes') {
      alert(id)
    }
  }

我们可以使用来自 React Native firebase 的 属性,它可以在前台、后台或应用程序中检测应用程序。 documentation 如果您为您的应用使用了通知。如果你不这样做,你可以使用反应导航中的 didFocus()

我遇到了同样的问题,这个 Whosebug 答案帮我解决了这个问题:

引用迈克尔的话

Finally I figured it out, Linking.addEventListener('url', callback) returns and event, to get the url it should be event.url, what I did before is Linking.addEventListener('url',(url) => setLink(url)) which must be Linking.addEventListener('url',({ url }) => setLink(url))