Firebase 在React native中检查用户是否联机
我有社交应用程序,我想检查用户是否在线: 我知道react native中的Appstate,我有一页web JS HTML用于管理它(社交应用程序) 所以我的问题是如何在myAPP中看到在线用户,以及如何在myWeb中看到这一点 我有一个这样的解决办法:Firebase 在React native中检查用户是否联机,firebase,react-native,google-cloud-firestore,Firebase,React Native,Google Cloud Firestore,我有社交应用程序,我想检查用户是否在线: 我知道react native中的Appstate,我有一页web JS HTML用于管理它(社交应用程序) 所以我的问题是如何在myAPP中看到在线用户,以及如何在myWeb中看到这一点 我有一个这样的解决办法: class AppStateExample extends Component { state = { appState: AppState.currentState }; componentDidMount() {
class AppStateExample extends Component {
state = {
appState: AppState.currentState
};
componentDidMount() {
AppState.addEventListener("change", this._handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener("change", this._handleAppStateChange);
}
_handleAppStateChange = nextAppState => {
if (
this.state.appState.match(/inactive|background/) &&
nextAppState === "active"
) {
console.log("App has come to the foreground!");
}
this.setState({ appState: nextAppState });
};
一旦我上线=>我会将状态添加到firestore 1数据中
db.collection("data").doc("userA").set(
....
status:'online'
).then(() => {
console.log("change status is sussecd");
});
然后在MyWeb中
db.collectionGroup('User').where('status', '==','online').onSnapshot(user => {
someQuery.....//
})
还有其他方法吗?问题不在于写得太多以至于用户在线,而在于当这种情况发生时,他们会很活跃。问题是当它们失去连接时,将其标记为脱机 Firestore没有很好的内置支持,因为它的协议是无连接的。但有一种解决方案是将其与实时数据库结合起来,并在此处进行了说明:
您也可以在应用程序中直接使用实时数据库,而不使用云功能或Firestore。我刚开始使用云功能,但可能现在他们已经收取了费用,可以用其他方式使用吗?您可以在应用程序中直接使用实时数据库,而不使用云功能或Firestore。