lawonga lawonga - 6 months ago 114
Android Question

Firebase addValueEventListener only working for a couple of hours

has anyone experienced this issue? My firebase code only works for basically a couple of hours (fully functional and all), and then when I try again it doesn't work anymore. See below code for how I am calling it:

ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.e(TAG, "onDataChange: Job found");
for (DataSnapshot jobSnapShot : dataSnapshot.getChildren()) {
Log.e(TAG, "onDataChange: Job +1");
Job job = jobSnapShot.getValue(Job.class);
// Add the ID into the job
job.setId(dataSnapshot.getKey());

// Set the job
arrayList.add(job);
subscriber.onNext(job);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled: " + databaseError.getMessage());
}
};
Log.e(TAG, "call: id:" + userId + ", reference:" + FirebaseDatabase.getInstance().getReference().toString());
Log.e(TAG, "call: Calling Jobs...");
FirebaseDatabase.getInstance()
.getReference()
.child(context.getString(R.string.firebase_jobs))
.child(userId).
addValueEventListener(valueEventListener);


The lines:

Log.e(TAG, "call: id:" + userId + ", reference:" + FirebaseDatabase.getInstance().getReference().toString());
Log.e(TAG, "call: Calling Jobs...");


Execute every single time. UserId and getReference returns correct values. However the addValueEventListener does not seem to be adding the listener after basically several hours later. The only way to fix this is to log off and log back on.

EDIT:

My auth state listener code:

firebaseAccount = getFirebaseAccount();
firebaseAccount.getAuth().addAuthStateListener(firebaseAccount.getAuthListener());


In firebaseAccount:

public FirebaseAuth.AuthStateListener getAuthListener() {
return authStateListener;
}

FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
String id = firebaseUser.getUid();
// User is signed in
Log.e(TAG, "onAuthStateChanged: Signed in as " + id);
// Start loginActivity when signed in
loginActivity.onLoginSuccess(id);
} else {
// User is not signed in
Log.e(TAG, "onAuthStateChanged: Signed out");

// User probably logged out. Finish the loginActivity and launch the login screen
}
}
};

Answer

This issue is caused by Firebase Auth tokens not refreshing themselves properly, which itself is caused by an underlying misconfiguration in your Firebase project.

You can tell if the token refresh is failing by calling the following snippet after you sign a user in:

FirebaseUser user = mAuth.getCurrentUser(); // mAuth is your current firebase auth instance
user.getToken(true).addOnCompleteListener(this, new OnCompleteListener<GetTokenResult>() {
    @Override
    public void onComplete(@NonNull Task<GetTokenResult> task) {
        if (task.isSuccessful()) {
            Log.d(TAG, "token=" + task.getResult().getToken());
        } else {
            Log.e(TAG, "exception=" +task.getException().toString());
        }
    }
});

(If there is an issue, you will get an exception).

You can follow this guide that we have put together in the Firebase team to troubleshoot and fix any issues with configuration that can cause this problem.

The above steps should be a permanent fix for the issue, however, we are also working hard to implement a way of automatically detecting misconfigurations and fixing them transparently for you. Apologies for any problem this may have caused you.