I am using Firebase-backend for my android application. I would like to create a user presence system for my chat. For this, I took a template from the 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 problem is that I donโt know when the onDisconnect event will be fired ?!
Each time I open the application, I write TRUE for the node " users / joe / connections ", but when I closed the application, nothing worked. When I turned off WIFI, the boolean parameter will also not be deleted. onDisconnect-event is triggered only when I stopped my application or reinstalled this application, or sometime else, when I could not determine.
So, as far as I understand, I have to manually handle such events:
1) close my application;
2) disable Wi-Fi;
3) maybe something else
to create a presence function in my application? But then when do you have onDisconnect-event ?
android firebase firebase-database
Alex Zezekalo
source share