Firebase-Conectivity-with-Android-Studio

What is Firebase?

Firebase is a backend platform for building web, Android and iOS applications. It offers real-time database, authentication services, storage solutions, and various other tools and services to help developers build high-quality apps faster. Firebase is developed by Google and is available as a cloud service, with support for multiple programming languages.

Why we use Firebase?

There are several reasons why developers choose to use Firebase:

What are the alternatives of firebase?

Some alternatives to Firebase include:

Some essential code to add to connect to firebase from anroid studio

To connect your Android app to Firebase, you can follow these steps:

buildscript {

    dependencies {
        // Add the dependency for the Google services Gradle plugin
        classpath 'com.google.gms:google-services:4.3.15'
    }
}

plugins {
    //below is alredy there
    id 'com.android.application'
    // this to be added
    // Add the Google services Gradle plugin
    id 'com.google.gms.google-services'

}
dependencies{

    implementation 'com.google.firebase:firebase-database:20.0.4'

}

M-1

import com.google.firebase.FirebaseApp;

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize Firebase
    FirebaseApp.initializeApp(this);
  }
}

M-2

The code FirebaseFirestore firestore; declares a variable firestore of type FirebaseFirestore. It is used to interact with the Firebase Firestore database.

The Firebase Firestore library is a NoSQL document-based database provided by Firebase. It allows you to store and retrieve data in real-time, as well as perform complex queries and transactions on the data.

The firestore variable can be used to perform various operations such as reading data from the database, writing data to the database, updating data, and deleting data.

   FirebaseFirestore firestore;

The code import com.google.firebase.firestore.FirebaseFirestore; is an import statement in Java. It is used to include the FirebaseFirestore class from the com.google.firebase.firestore package into the current Java file.

import com.google.firebase.firestore.FirebaseFirestore;
import  java.util.Map;

below code is used to store data in the Firebase Firestore database.


        // Code For Firebase Data Storage

        firestore = FirebaseFirestore.getInstance();
        Map<String, Object> users = new HashMap<>();
        users.put("firstName", "Kumar");
        users.put("lastName", "Abhinav");
        users.put("year", "3rd Year IT");

        users.put("firstName2","H.S");
        users.put("lastName2","Rai");
        users.put("post", "HOD CIVIL");

        firestore.collection("users").add(users).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
            @Override
            public void onSuccess(DocumentReference documentReference) {
                Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_LONG).show();
            }
        });

Time Required : 1 hours 20 minutes