'Firebase Android App - not updating realtime database
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?
Solution 1:[1]
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.
Solution 2:[2]
If anyone reads this and gets this error,
Firebase Realtime Database connection killed: Different Region
Make sure your google-services.json
file has the right firebase_url
.
If it doesn't work right away,
- Clean Project
- Invalidate Caches
After restart, it should be working if that was the case.
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Jeremy Caney |
Solution 3 | Elsunhoty |