【发布时间】:2018-09-14 19:24:48
【问题描述】:
我想保持用户在 Firestore 或 Firebase 中的存在。实际上,由多个移动设备和一台平板电脑处理的单个用户帐户以及该平板电脑维护或执行操作或提供针对我的 Firestore 数据更改的功能。所以我需要在 Firestore 或 Firebase 中保持平板电脑的存在,以便其他移动设备了解平板电脑是否工作。
所以请给我一些解决方案。
我使用 Firebase 节点检查连接状态。下面的代码,但我没有给出正确的响应,例如当我从路由器上拔下网线时,onDataChange not fire.for 断开连接。
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference myConnectionsRef = database.getReference(FirebaseAuth.getInstance().getCurrentUser().getUid()+"/tab/connections");
// stores the timestamp of my last disconnect (the last time I was seen online)
final DatabaseReference lastOnlineRef = database.getReference(FirebaseAuth.getInstance().getCurrentUser().getUid()+"/tab/lastOnline");
final DatabaseReference connectedRef = database.getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
DatabaseReference con = myConnectionsRef.push();
// when this device disconnects, remove it
con.onDisconnect().removeValue();
// when I disconnect, update the last time I was seen online
lastOnlineRef.onDisconnect().setValue(ServerValue.TIMESTAMP);
long timestamp = Calendar.getInstance().getTimeInMillis();
// add this device to my connections list
// this value could contain info about the device or a timestamp too
con.setValue(Boolean.TRUE);
}else {
long timestamp2 = Calendar.getInstance().getTimeInMillis();
}
}
@Override
public void onCancelled(DatabaseError error) {
System.err.println("Listener was cancelled at .info/connected");
}
});
【问题讨论】:
标签: java android firebase firebase-realtime-database google-cloud-firestore