Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Database listeners not firing after a while while authenticated

I'm making an app and I've run into a problem with Firebase's Database and Authentication services.

After a while, while I am authenticated (using Firebase Auth) and using my app, my database ValueEventListeners don't seem to be called, even though there is data in the database.

How I'm adding 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)
            {

            }
        });

Stuff I've tried:

  • Checking the database rules (after a relog reading is fine - but simulated reads pass even while authenticated & unauthenticated)
  • keepSynced(true) on the DatabaseReferences
  • Adding the listeners in the Activity's onCreate instead of the Application's onCreate
  • Adding/removing/updating data in the database to trigger a sync
  • Rebooting

Any help would be much appreciated.

like image 222
Ilan Avatar asked Sep 17 '17 14:09

Ilan


2 Answers

So, apparently the issue was that an API called "Token Service" was not enabled in my Google APIs dashboard.
Thanks to a helpful email from Firebase Support (thanks guys!), I've turned on debug 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.

So by enabling the API, it seems to have fixed the error!

like image 177
Ilan Avatar answered Oct 27 '22 11:10

Ilan


Create a reference to your child database, create a custom Chat class as per your requirement( essentially what you see in your firebase ). addChildEventListener should give you all the changes happening with 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) {

            }
        });
like image 37
Prashanth Kondedath Avatar answered Oct 27 '22 11:10

Prashanth Kondedath