【问题标题】:React-Native Expo send Firebase post when users close the application当用户关闭应用程序时,React-Native Expo 发送 Firebase 帖子
【发布时间】:2021-02-01 19:32:18
【问题描述】:

我目前有发送用户位置的发布路线,以及 isOnline: true 当应用程序打开时,当用户关闭应用程序时我想更新常规数据并更新 isOnline: false 到我的 Firebase Realtime D B。我尝试使用控制台日志来确定卸载发生的确切时间,但这并不足以让我知道。也许我对 componentWillUnmount 的工作方式有误!

(如果这是一个重复的问题,我很抱歉。我找了大约一个小时才决定发布这个)

这是我的卸载:

componentWillUnmount() {

    this.userDisconnectHandler();

    navigator.geolocation.clearWatch(this.watchID);

    this.stopTimer();
  }

这是我的 disconnectHandler:

userDisconnectHandler() {
    var postDataRef = firebase.database().ref(`live-data/${userName}`);
    console.log("disconnected")

    navigator.geolocation.getCurrentPosition(
      (position) => {
        postDataRef
          .set({
        coordinate: {
          longitude: position.coords.longitude,
          latitude: position.coords.latitude,
        },
        userName: `${userName}`,
        isOnline: false,
          })
          .then(() => console.log("finalPostComplete"))
          .catch((err) => console.log(err));
      },
      (err) => console.log(err)
    );
  }

【问题讨论】:

  • 你可能想试试应用状态https://reactnative.dev/docs/appstate
  • @SaachiTech 将此作为解决方案发布,以便我为您解决该问题提供信用
  • 已发布答案。谢谢

标签: react-native firebase-realtime-database expo


【解决方案1】:

你可能想试试 AppState https://reactnative.dev/docs/appstate

根据文档

AppState 可以告诉你应用是在前台还是后台, 并在状态发生变化时通知您。

AppState 经常用于确定意图和正确性 处理推送通知时的行为。

这应该适合你的情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多