2

Anyone looking at this the below is the correct answer and I had everything setup correctly. I still dont no what the issue is. I was loggin in with facebook, using that to create a firebaseuser object. and then sending the below as test data. I've found it to be an intermittent issue. Uninstalling the app from the device and redeploying often fixes the issue. very odd

Im actually struggling to get the example to work.

So basic.. set value....

FirebaseDatabase database = FirebaseDatabase.getInstance();
Log.d(TAG, "db ref: "+database.getReference());
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");

When I go into the database console I cant see the values I sent. My log is returning a reference to the db though. I've also set my rules up as public to be on the safe side.

Any ideas what I'm doing wrong?

aidanmack
  • 518
  • 1
  • 5
  • 16
  • 1
    Very likely your database only allows read/write by authenticated users and your app doesn't sign the user in. See http://stackoverflow.com/questions/37403747/firebase-permission-denied/37404116#37404116 – Frank van Puffelen May 24 '16 at 19:00
  • is there any child name 'message' ? – Farrukh Faizy May 24 '16 at 19:00
  • @FrankvanPuffelen umm my rules are set up as read/write... { "rules": { ".read": true, ".write": true } } – aidanmack May 24 '16 at 19:08
  • @MuhammadFarrukhFaizy do i need to do anything in the console to except a write? It currently just says the name of my db and then "null" – aidanmack May 24 '16 at 19:09
  • Hmmm... those rules look good. If you attach an [completion callback](https://firebase.google.com/docs/database/android/save-data#receive_a_completion_callback) to `setValue()` it will show you if (and why) the write is failing. `myRef.setValue(...).addOnFailureListener()` – Frank van Puffelen May 24 '16 at 19:17
  • If you need a working read/write working android please refer to this link https://github.com/frk93/FirebaseBasics Make sure to add the app to your database and overwrite the rules – Farrukh Faizy May 24 '16 at 19:18
  • [along with the other solutions provided, try to add proper latest version of dependencies in the build.gradle file.... this worked for me .](https://i.stack.imgur.com/9O28c.jpg) – Amar Kumar Mar 11 '19 at 08:29

3 Answers3

3

Actually you are referencing a child 'message' which is logically not there,

If you want to name you root of the database simply follow the steps

DatabaseReference myRef1 = FirebaseDatabase.getInstance().getReference(); //Getting root reference
myRef1.setValue("Hello, World!");

and if you want to give some value to your child in database, Make sure you have a child in your database

DatabaseReference myRef1 = FirebaseDatabase.getInstance().getReference(); //Getting root reference
DatabaseReference myRef = myRef1.child("message"); //Write your child reference if any
myRef.setValue("Hello, World!");

It will overwrite the value of your child to 'Hello World'

Go to Firebase console and create a project and give it a valid packageName which you will use. and download google-service.json file and add this file to app folder by changing your Android View to Project View.

Add these lines into Project:Gradle

dependencies {
    classpath 'com.google.gms:google-services:3.0.0'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

Add these into App:Gradle

dependencies {
    compile 'com.google.firebase:firebase-core:9.0.0'
    compile 'com.google.firebase:firebase-database:9.0.0'
}

// ADD THIS AT THE BOTTOM OF YOUR APP GRADLE

apply plugin: 'com.google.gms.google-services'

Note : Do overwrite the rules in your database . If you are not implementing login authentication.

{
  "rules": 
   {
    ".read": true,
    ".write": true,
   }
}

And add this line in Application or in onCreate of Activity

Firebase.setAndroidContext(this);
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
Farrukh Faizy
  • 1,203
  • 2
  • 18
  • 30
  • Thanks Muhammad... No idea whats going on to be honest. "myRef1.getDatabase().getReference()" returns the url to my database... so i can see it, just aint writing to it. Set up my rules as public as well. – aidanmack May 24 '16 at 19:25
  • The answer is perfect just make your what i describe here you must follow and please refer to this link also https://github.com/frk93/FirebaseBasics – Farrukh Faizy May 24 '16 at 19:27
  • I checked your example Muhammad, ive added the myRef.addValueEventListener(new ValueEventListener() { to my code and it does return the values sent. however nothing updates on the web firebase console. does yours? – aidanmack May 24 '16 at 19:59
  • Make sure you have required dependencies and overwrite rules in database in your firebase database console – Farrukh Faizy May 24 '16 at 20:09
  • You guys can refer to this gist as well for implementation: https://github.com/frk93/FirebaseBasics – Farrukh Faizy Dec 18 '18 at 18:54
0

seems, i found the answer. It helps me!

I have used before android studio version 1.5 And that's why (maybe not) but i could't upgrade required firebase libs to 9.0.2 All this time i have used 9.0.0

So firstly i upgrade my android studio to updated stable version 2.2 and then update version of dependencies to next ones:

compile 'com.google.firebase:firebase-core:9.0.2'
compile 'com.google.firebase:firebase-auth:9.0.2'
compile 'com.google.firebase:firebase-database:9.0.2'

And now all values are saved to database from android app.

So do two things: 1. update android studio to last stable version 2. update firebase required libs versions

Hope this answer will help

0

Try including the URL of the database.

String strURL="https://testfirebase-0123-default-rtdb.asia-southeast1.firebasedatabase.app/";
FirebaseDatabase database = FirebaseDatabase.getInstance(strURL);
  • I suggest to you an example link. Also, this doesn't seems to answer the question. Can you explain how this will fix the issue/why he is getting this ? – Elikill58 Oct 24 '21 at 07:59