0

I have tried to insert a simple message on firebase database using firebase documentation and firebase tool both but that is not updating.

I have also add picture of my firebase console after launching app.

Please help me to find bug!!!

MainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("message");
        myRef.setValue("Hello, World!");
    }
}

Gradle App

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}


android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.firebase_realtimedata001"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    implementation 'com.google.firebase:firebase-database:20.0.2'
    implementation platform('com.google.firebase:firebase-bom:28.4.1')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-database'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Console Image

enter image description here

2
  • Make sure you have configured read and write rules in Realtime Database console Commented Sep 24, 2021 at 9:33
  • What is the location of your database, US central? Please respond with @AlexMamo Commented Sep 24, 2021 at 10:04

1 Answer 1

1

Your code is working perfectly fine. Hello, World! message is added to database when I click add button

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate ( Bundle savedInstanceState ) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        Button addMessage = findViewById ( R.id.button );
        addMessage.setOnClickListener ( v -> addMessage () );
    }

    public void addMessage () {
        DatabaseReference myRef = FirebaseDatabase.getInstance ().getReference ( "message" );
        myRef.setValue ( "Hello, World!" ).addOnCompleteListener ( new OnCompleteListener<Void> () {
            @Override
            public void onComplete ( @NonNull Task<Void> task ) {
                Log.d ( "INFORMATION", "Added" );
            }
        } );
    }
}

After clicking addMessage button Realtime Database shows the message as follows

Realtime Database Data

Make sure you are having rules as shown below

Realtime Database Rules

Sign up to request clarification or add additional context in comments.

2 Comments

@niranjan1997 - I tried same but problem is same.
@AlexMamo - I choose singapur location for database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.