Firebase for Android - W / PersistentConnection: pc_0 - if authentication data is invalid

I am using Firebase (version 10.0.0) in my Android project and have encountered the following problem with Firebase Database:

Prerequisite: A user logs in through Firebase Auth with a Google account (FirebaseAuth.getInstance (). GetCurrentUser () returns a nonzero value).

  • In main onCreate mode, I read some value from the Firebase database:

FirebaseDatabase.getInstance() .getReference() .child(NODE_USERS).child(user.getUid()).child(NODE_DICTIONARY_VERSION) .addListenerForSingleValueEvent(...);

  1. It works great. But after a while, the method described above goes into some kind of infinite loop (without errors, without calling OnCancelled in the ValueEventListener). The following message also appears in the log:
  W / PersistentConnection: pc_0 - Provided authentication credentials are invalid. 
 This usually indicates your FirebaseApp instance was not initialized correctly. 
 Make sure your google-services.json file has the correct firebase_url and api_key. 
 You can re-download google-services.json from https://console.firebase.google.com/ 

After that, all other calls to the Firebase database (read, write, delete, etc.) also do not work.

To fix this, I tried

  • do a quiet login at application startup before accessing the Firebase database:
  FirebaseAuth.getInstance (). GetCurrentUser ()
 .reauthenticate (credential) .addOnCompleteListener (...); 
  1. Connect to the Firebase database:
  FirebaseDatabase.getInstance (). GoOffline ();
 FirebaseDatabase.getInstance (). GoOnline (); 

As a result, a problem with the Firebase database does not appear as often, and at least after a day has passed. Also, the problem disappears for some time if you restart the entire application (without these fixes, the problem is reproduced every time, only for re-entering the system).

In any case, the problem still exists, and restarting the application is a very bad solution :)

Any idea how to solve this problem? Or maybe I'm doing something wrong?

Full Firebase Log:

 12-19 13:03:40.544 D/FirebaseAuth: Notifying listeners about user ( HbY7R8FPR6QwgstQd3wmypI2nwJ2 ). 12-19 13:03:40.546 D/FirebaseApp: Notifying auth state listeners. 12-19 13:03:40.546 D/FirebaseApp: Notified 1 auth state listeners. 12-19 13:03:40.546 D/RepoOperation: Auth token changed, triggering auth token refresh 12-19 13:03:40.602 D/FirebaseAuth: Notifying listeners about user ( HbY7R8FPR6QwgstQd3wmypI2nwJ2 ). 12-19 13:03:40.613 D/FirebaseApp: Notifying auth state listeners. 12-19 13:03:40.613 D/FirebaseApp: Notified 1 auth state listeners. 12-19 13:03:40.613 D/RepoOperation: Auth token changed, triggering auth token refresh 12-19 13:03:43.832 V/FA: Session started, time: 176462962 12-19 13:03:43.853 I/FA: Tag Manager is not found and thus will not be used 12-19 13:03:43.858 D/FA: Logging event (FE): _s, Bundle[{_o=auto, _sc=MainActivity, _si=3824046701607023337}] 12-19 13:03:43.885 V/FA: Using measurement service 12-19 13:03:43.885 V/FA: Connecting to remote service 12-19 13:03:43.909 D/FA: Connected to remote service 12-19 13:03:43.910 V/FA: Processing queued up service tasks: 1 12-19 13:03:44.139 W/PersistentConnection: pc_0 - Provided authentication credentials are invalid. This usually indicates your FirebaseApp instance was not initialized correctly. Make sure your google-services.json file has the correct firebase_url and api_key. You can re-download google-services.json from https://console.firebase.google.com/. 12-19 13:03:45.985 W/PersistentConnection: pc_0 - Provided authentication credentials are invalid. This usually indicates your FirebaseApp instance was not initialized correctly. Make sure your google-services.json file has the correct firebase_url and api_key. You can re-download google-services.json from https://console.firebase.google.com/. 12-19 13:03:48.945 V/FA: Inactivity, disconnecting from the service 

PS It seems that the problem only occurs on my Nexus 5 (Android 6.0.1). There is no such problem with Sony Xperia V (Android 4.3).

PPS I also tried to create debug and release apks and add the "Editor" role to all the accounts of auto-generated services in the Google cloud console - this does not help either.

+6
source share
2 answers

Finally, I found how to fix the problem thanks to Firebase support engineers! :)

The trick is to wait for FirebaseAuth to get from FirebaseAuth.AuthStateListener (instead of getting it directly from FirebaseAuth.getInstance ()) before accessing the Firebase Database when the application starts : it looks like Firebase needs some time to initialize / update user authentication .

So, the following code does not work correctly:

 // Main Activity protected void onCreate(...) { if (FirebaseAuth.getInstance().getCurrentUser() != null) { // accessing Firebase Database } } 

It should be like:

 protected void onCreate(...) { //... authStateListener = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (user != null) { // accessing Firebase Database } } }; FirebaseAuth.getInstance().addAuthStateListener(authStateListener); } 
+4
source

Alexander's answer did not help me.

It seems that clearing the entire Google Play service storage solution solves the problem Settings => Services => Google Play Services => Storage => MANAGEMENT STORAGE => CLEAR ALL DATA , this issue probably needs to be fixed by Firebase or Google.

+2
source

Source: https://habr.com/ru/post/1013284/


All Articles