I am trying to add online/offline feature in my android chat app using .info/connected path I wrote the following code inside onCreate() method
studentref = FirebaseDatabase.getInstance().getReference("student").child(user.getUid());
connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
studentref.child("status").onDisconnect().setValue("offline");
studentref.child("status").setValue("Online");
} else {
studentref.child("status").setValue("offline");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
But the else part does not execute when i minimized the app for more than 60 seconds It only works when i killed the app or when i switch off the internet for more than 60 seconds
How to make it works , when the app is in foreground it should set the value "online" and when the app is in background or killed it should set the value to "offline"