-
-
Save fm-eb/d629a7e236a0066624696c9561aaf8ab to your computer and use it in GitHub Desktop.
private FirebaseDatabase firebaseDatabase; | |
private DatabaseReference connectedRef; | |
private ValueEventListener connectionStateListener; | |
public FirebaseHeartbeatConnector(FirebaseApp firebaseApp) { | |
L.i("FirebaseHeartbeatConnector() called with: firebaseApp = [" + firebaseApp + "]"); | |
firebaseDatabase = FirebaseDatabase.getInstance(firebaseApp); | |
firebaseDatabase.setLogLevel(Logger.Level.DEBUG); | |
} | |
private void doSendFirebaseHeartbeatRequest() { | |
L.i("doSendFirebaseHeartbeatRequest() called"); | |
connectedRef = firebaseDatabase.getReference(); | |
connectedRef.keepSynced(true); | |
connectionStateListener = connectedRef.addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(@NonNull DataSnapshot snapshot) { | |
Boolean connected = snapshot.getValue(Boolean.class); | |
if (connected == null) { | |
changeState(FirebaseConnectionState.error); | |
return; | |
} | |
if (connected) { | |
changeState(FirebaseConnectionState.connected); | |
} else { | |
changeState(FirebaseConnectionState.not_connected); | |
} | |
} | |
@Override | |
public void onCancelled(@NonNull DatabaseError error) { | |
L.i("FirebaseConnectionStateListener was cancelled"); | |
} | |
}); | |
} |
Nope, I still get the timeout after 60 seconds :(
i also tried it by giving a firebaseInstance to get getInstance()
method, but to no avail
I tested this with the below code and it didn't timeout after 60 seconds for me.
FirebaseDatabase.getInstance().getReference().keepSynced(true);
DatabaseReference 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 == null) return;
if (connected) Log.d("presence", "connected");
else Log.d("presence", "disconnected");
}
@Override
public void onCancelled(@NonNull DatabaseError error) { }
});
That' really strange. I copy/pasted your code and it worked once. Then I added my firebaseapp-instance as paramter for FirebaseDatabase.getInstance() and it didn't work anymore (1 minute timeout). Then I removed the firebaseapp-instance and it also didn't work anymore...
Then I completely uninstalled the app and went back to your code again => does not work anymore....
i have no idea what's going on there and unfortunately I have no time left to play around with it. So I will implement a normal online-check using the CONNECTIVITY_SERVICE.
Thank you for your time and effort trying to figure this out!! I hope the documentation will improve on this topic.
ah okay, I thought that I have to replace the .info/connected listener. I will try this right now, thanks!