react-native - 如何创建用户上次在线时间的 'last seen at' 时间?

标签 react-native react-native-ios react-native-firebase

我想添加一个最后一次看到是在今天下午 1 点 或用户最后一次访问或打开我的应用程序的时间 - WhatsApp 和其他聊天应用程序具有的功能。

在我的 React Native 聊天应用程序中,我使用 redux 进行状态处理。我的后端使用 Firebase。我的聊天应用程序即将完成,但我不知道如何添加用户上次查看的功能。

最佳答案

您可以使用 Firebase 实时数据库 onDisconnect API建立一个存在系统,例如检测用户上次在线的时间。

以下示例包含用户状态,例如onlineoffline,以及 last_changed 时间戳。您可以结合使用这两种方法,为您的聊天应用中的每个用户创建丰富的在线状态。

import firebase from 'react-native-firebase';

// Fetch the current user's ID from Firebase Authentication.
const uid = firebase.auth().currentUser.uid;

// Create a reference to this user's specific status node.
// This is where we will store data about being online/offline.
const userStatusRef = firebase.database().ref('/status/' + uid);

// We'll create two constants which we will write to 
// the Realtime database when this device is offline
// or online.
const isOfflineForDatabase = {
    state: 'offline',
    last_changed: firebase.database.ServerValue.TIMESTAMP,
};

const isOnlineForDatabase = {
    state: 'online',
    last_changed: firebase.database.ServerValue.TIMESTAMP,
};

// Create a reference to the special '.info/connected' path in 
// Realtime Database. This path returns `true` when connected
// and `false` when disconnected.
firebase.database().ref('.info/connected').on('value', (snapshot) => {
    // If we're not currently connected, don't do anything.
    if (snapshot.val() == false) {
        return;
    };

    // If we are currently connected, then use the 'onDisconnect()' 
    // method to add a set which will only trigger once this 
    // client has disconnected by closing the app, 
    // losing internet, or any other means.
    userStatusRef.onDisconnect().set(isOfflineForDatabase).then(() => {
        // The promise returned from .onDisconnect().set() will
        // resolve as soon as the server acknowledges the onDisconnect() 
        // request, NOT once we've actually disconnected:
        // https://firebase.google.com/docs/reference/js/firebase.database.OnDisconnect

        // We can now safely set ourselves as 'online' knowing that the
        // server will mark us as offline once we lose connection.
        userStatusRef.set(isOnlineForDatabase);
    });
});

示例代码改编自:presence guide - Firebase website .

希望对您有所帮助。

关于react-native - 如何创建用户上次在线时间的 'last seen at' 时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51792570/

相关文章:

reactjs - 我正在尝试使用 npm 安装新组件。但它给出了错误

react-native - React Native - react 原生架构

react-native - React Native-Pod安装问题 "cannot load such file.......node_modules/react-native/scripts/react_native_pods"

reactjs - react native Firebase 消息传递点击通知

android - Firestore onSnapshot() 和 get() 导致 Android 应用程序崩溃

ios - Firebase 错误 : You must enable this service in the console

javascript - 如何在 React Native 的输入文本中添加图标左侧

ios - 在 iOS 上自动填充后 react-native Keyboard.dismiss() 不工作

react-native - 如何将 React-Native App 导出为 Android 库 (.aar)?

objective-c - 如何将新的 UIViewController 从 React-Native 推送到 Objective-C Native 桥