Firebase database listeners do not start after authentication

I am creating an application and I have a problem with Firebase Database and Authentication services.

After a while, when I am authenticated (using Firebase Auth) and using my application, my ValueEventListeners database does not seem to be called, although there is data in the database.

How do I add my listeners:

FirebaseDatabase .getInstance() .getReference() .child("my_child") .addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Log.d("App", "onDataChange"); } @Override public void onCancelled(DatabaseError databaseError) { } }); 

The material I tried:

  • Checking the rules of the database (after the correct reading), but simulated readings pass even with authentication and are not authenticated).
  • keepSynced (true) on DatabaseReferences
  • Adding listeners to Activity onCreate instead of onCreate
  • Add / delete / update data in the database to start synchronization
  • Rebooting

Any help would be greatly appreciated.

+5
source share
2 answers

So, apparently, the problem was that the API, called the Token Service, was not included in my Google API Toolbar.
Thanks for the helpful email from Firebase Support (thanks guys!), I turned on debugging logging by calling FirebaseDatabase.getInstance().setLogLevel(Logger.Level.DEBUG);

Lo and behold: D/PersistentConnection: pc_0 - Error fetching token: An internal error has occurred. [ Token Service API has not been used in project <project-id> before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/securetoken.googleapis.com/overview?project=<project-id> then retry. D/PersistentConnection: pc_0 - Error fetching token: An internal error has occurred. [ Token Service API has not been used in project <project-id> before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/securetoken.googleapis.com/overview?project=<project-id> then retry.

Thus, by turning on the API, he seems to have fixed the error!

+3
source

Create a link to your child database, create your own chat class according to your requirement (basically what you see in your firebase). addChildEventListener should give you all the changes that happen to your my_child. Hope this helps you.

 mFirebaseRef = new Firebase("https://yourapp.firebaseio.com/").child("my_child"); /** * Firebase - Receives message */ mFirebaseRef.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { if (dataSnapshot != null && dataSnapshot.getValue() != null) { try{ Chat model = dataSnapshot.getValue(Chat.class); mChats.add(model); mRecyclerView.scrollToPosition(mChats.size() - 1); mAdapter.notifyItemInserted(mChats.size() - 1); } catch (Exception ex) { Log.e(TAG, ex.getMessage()); } } } @Override public void onChildChanged(DataSnapshot dataSnapshot, String s) { } @Override public void onChildRemoved(DataSnapshot dataSnapshot) { } @Override public void onChildMoved(DataSnapshot dataSnapshot, String s) { } @Override public void onCancelled(FirebaseError firebaseError) { } }); 
+1
source

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


All Articles