【问题标题】:How to Refresh Native Deeplink Application When In Foreground?如何在前台刷新本机 Deeplink 应用程序?
【发布时间】:2019-12-24 03:32:25
【问题描述】:

我在 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)
    }
  }

【问题讨论】:

  • android的情况下为什么不监听url变化?
  • 怎么听?在前台,屏幕未刷新。
  • 使用android,无法注册这个监听器Linking.addEventListener('url', this.handleOpenURL);。您有上述if 条件。
  • 是的,它适用于 iOS 和 Android。但我认为这不是问题。
  • 或者更快的你能给我链接或者你有没有经历过与 react native 相同的案例?

标签: android react-native react-native-android deep-linking


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,这个 StackOverflow 答案为我解决了这个问题: https://stackoverflow.com/a/59351886/5901486

    引用迈克尔的话

    终于想通了,Linking.addEventListener('url', callback) 返回和事件,得到的url应该是event.url,我之前做的是Linking.addEventListener('url',(url) => setLink(url)) 必须是 Linking.addEventListener('url',({ url }) => setLink(url))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多