I`m using Firebase-backend for my android applicaion. I'd like to build a users presence system for my chat. For this purpose I've taken the pattern from Firebase Guide
final Firebase myConnectionsRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/users/joe/connections");
// stores the timestamp of my last disconnect (the last time I was seen online)
final Firebase lastOnlineRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/users/joe/lastOnline");
final Firebase connectedRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/.info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
// add this device to my connections list
// this value could contain info about the device or a timestamp too
Firebase con = myConnectionsRef.push();
con.setValue(Boolean.TRUE);
// 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);
}
}
@Override
public void onCancelled(FirebaseError error) {
System.err.println("Listener was cancelled at .info/connected");
}
});
The trouble is I don't know when onDisconnect event would be fired?!
Every time I open the app I write TRUE to the node "users/joe/connections" but when I closed app nothing would be happend.
When I switched WIFI off then the boolean parameter wouldn't be removed as well.
onDisconnect-event only fired when I forcely stopped my app or reinstalled this app or somewhen else when I couldn`t determine.
So, as I understand right I have to handle manually such events:
1) close my app;
2) switch WiFi off;
3) maybe something else
for creating Presence feature in my app? But then when have onDisconnect-event to be fired ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…