1
votes

I experience some errors while trying to push data into my firebase realtime database. I checked a couple of tutorials and according to all of them i am doing nothing wrong. Unfortunately I can not update the database from my android studio emulator device.

I activated debug mode and stepped through every single process and saw that my code actually works because the database responses with the child id when pushing to it.

package com.example.fbconnect;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {
    Button btnsaves;
    Member member;
    DatabaseReference myRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnsaves=(Button)findViewById(R.id.button);
        btnsaves.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                myRef = FirebaseDatabase.getInstance().getReference("Member");
                String id = myRef.push().getKey();
                member = new Member("MSmith", 12, id);
                myRef.child(id).setValue(member);
                Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

When debugging to this line

String id = myRef.push().getKey();

I see, that the database answered with a code. But it will not set the new value or update the value of existing database Childs.

After activating the debug mode I recognised some rare lines in android studio that may lead to the problem:

E/FirebaseInstanceId: Topic sync or token retrieval failed on hard failure exceptions: AUTHENTICATION_FAILED. Won't retry the operation.

I am definitely sure that I did not activate any authentication in my firebase. Normally everybody should be able to read and write (I am aware of this but I am desperate to get this database connection working).

Can you please help me?

2
Did you change the authentication of Firebase database in the firebase console? - mayur
no its still on default. I tried activating the anonymous sign in method but that did not help either... - kantejn
Go to firebase console > Database > Firebase Realtime Database > Rules tab Change read and write to true. - mayur
I'm experiencing the same, it works on my physical device but not on the emulator. But it seems to store the data in some local cache since it can retrieve what I've set in the emulator but that's also gone after I close the app. - riezebosch
For me the problems with the emulator were solved by uninstalling the app from the device and a fresh deploy. - riezebosch

2 Answers

1
votes

Go to firebase console > Database > Firebase Realtime Database > Rules tab Change read and write to true. Also check if your device is connected to the internet.

0
votes

By Default Rule in Firebase is for Authorized requests

you can change read and write to True

BUT IT IS NOT SECURE

any one with your Firebase Url Can Access And Change Data

enter image description here