简体   繁体   English

从Deeplink打开时如何刷新应用程序?

[英]How to refresh app when opened from deeplink?

I am struggling with a react native app. 我正在努力使用本机应用程序。 I would implement react native firebase dynamic link, but now I am a little lost. 我会实现React Native Firebase动态链接,但是现在我有点迷路了。 I use this method on HomeScreen which working perfectly every times when somebody opens the app. 我在HomeScreen上使用此方法,每次有人打开应用程序时,它都能完美运行。

async componentWillMount() {
    try {
      let url = await firebase.links().getInitialLink();
      if(url) {
        let api = "example.com/user/123456";
        try {
          this.setState({ data: "John Doe" });
          this.props.navigation.navigate('Preview', {user: this.state.data })
        }
        catch {
        }
      }
    }
    catch {
    }
  }

But when the app is already opened this method doesn't work properly. 但是,当应用程序已经打开时,此方法将无法正常工作。 Is there a way where I can trigger a function every time when somebody comes back to the opened app? 有没有一种方法可以在有人回到打开的应用程序时每次触发功能?

Just a tip, you should place your code in componentDidMount so that you do not block the initial (first) render. 提示,您应该将代码放置在componentDidMount以免阻塞初始(第一个)渲染。

You could use AppState to listen out for changes to apps being put in the background/foreground. 您可以使用AppState侦听在后台/前景中放置的应用程序的更改。

componentDidMount() {
  this.showPreview();
  AppState.addEventListener('change', this.onAppStateChange);
}

componentWillUnmount() {
  AppState.removeEventListener('change', this.onAppStateChange);
}

const onAppStateChange = appState => {
  // You can check if appState is active/background/foreground
  this.showPreview();
}

const showPreview = async (appState) => {
    // You can check if appState is active/inactive/background
    try {
      let url = await firebase.links().getInitialLink();
      if(url) {
        let api = "example.com/user/123456";
        try {
          this.setState({ data: "John Doe" });
          this.props.navigation.navigate('Preview', {user: this.state.data })
        }
        catch {
        }
      }
    }
    catch(e) {
      console.error(e);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Deeplink:如何在未安装应用程序而不是 Playstore 时将用户重定向到网站 - Deeplink: How to redirect user to a website when app is not installed instead of playstore Android LiveData:如何避免从后台重新打开应用程序时从数据库传输数据? - Android LiveData: How to avoid data transfer from the database when an app is re-opened from the background? 一旦来自附近位置的新数据进入relatime数据库,如何发送通知(当应用程序未打开时)? - How to send a notification (when the app is not opened) as soon as new data from nearby location enters the relatime database? Android - 当我们从另一个网站调用深层链接 URL 而不是直接在 href 标签中传递它时,Firebase 深层链接 URL 会丢失 - Android - Firebase Deeplink URL is lost when we call the deeplink URL from another website instead of passing it directly in href tag xcode 12 firebase 应用程序在后台时通知如果应用程序已打开 webview 如何重新加载 - xcode 12 firebase notification when app in background how to do a reload if the app have a opened webview 从应用程序内打开时,停止 Firebase 动态链接重定向到浏览器 - Stop Firebase Dynamic Links from redirecting to browser when opened from within app 当应用程序已经打开时从动态链接中提取数据 - Flutter - Extract Data from Dynamic Link When App is already opened- Flutter 我如何知道应用程序是否通过AppDelegate的didFinishLaunchingWithOptions上的Firebase-Deeplink(动态链接)启动 - How do I know if app was launched via Firebase-Deeplink (Dynamic Links) at AppDelegate's didFinishLaunchingWithOptions 如何保存多个谷歌地图标记,它们在 Firebase 实时数据库中的位置并在应用程序打开时再次显示它们 - How to save multiple google maps marker, their positions in Firebase Realtime database and show them again when the app is opened 这些 Firebase 深层链接警告在已部署的应用中是否有害? - Are these Firebase deeplink warnings harmful in a deployed app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM