user15226396
user15226396

Reputation: 109

Firebase realtime database is not connecting

Iam trying to use firebase real-time database. I have set rules to true and in my project added all required dependencies and also internet permission. My mobile is very well connected to the wifi.

When I tried to use write the data then it's not working. So I checked the status as per documentation and it's showing dB is not connected to my app.

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) {
            Log.d(TAG, "connected");
        } else {
            Log.d(TAG, "not connected");
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Log.w(TAG, "Listener was cancelled");
    }
});

Am I doing anything wrong hwre?

Appreciate help.

PD

Upvotes: 0

Views: 611

Answers (2)

user15226396
user15226396

Reputation: 109

The issue is solved by recreating database and connecting again to the app. Thank you for the answers.

Upvotes: 1

anonymous
anonymous

Reputation: 228

You should add the Query

DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference();
Query query = connectedRef.child("info/connected");
query.addValueEventListener(new ValueEventListener() { 
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        boolean connected = snapshot.getValue(Boolean.class);
        if (connected) {
        Log.d(TAG, "connected");
        } else {
        Log.d(TAG, "not connected");
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Log.w(TAG, "Listener was cancelled");
    }
});

Upvotes: 1

Related Questions