27

I want to create an android app, where I, as an author, upload my college notes and anyone can download it. I read that firebase can help me with this. Can anyone please explain what is firebase and how to use it with my reference? Thank you!

David East
  • 31,526
  • 6
  • 67
  • 82
Nirup Iyer
  • 1,215
  • 2
  • 14
  • 20

3 Answers3

25

Update: Since Google I/O 2016 there have been some major updates to Firebase. Below is information related to the legacy service.

Firebase team member here.

tl;dr - Read this Quickstart, watch this video. Use FirebaseUI.

Firebase is a platform for mobile and web apps.

There's three main services to Firebase:

  • Realtime database
  • Authentication
  • Static Hosting

Setup

For writing an Android app you need to download the Android SDK. If you have Android Studio 1.4 you can setup Firebase by going to File > Project Structure > Cloud. Then click the Firebase checkbox.

Saving and Retrieving data

Every Firebase app has a name, and that is used to in a URL to access your database. Data is stored in Firebase in JSON. Each piece has a URL mapped to its location. To get or save data to that location you create a Firebase reference.

// Create a reference to the Firebase database
Firebase ref = new Firebase("https:<MY-FIREBASE-APP>.firebaseio.com/data");
// Save Data
ref.setValue("Hello"); 
// Sync data
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        System.out.println(snapshot.getValue());
    }
    @Override
    public void onCancelled(FirebaseError firebaseError) {
        System.out.println("The read failed: " + firebaseError.getMessage());
    }
});

FirebaseUI

The Firebase SDK is good at saving and retrieving data, but it is agnostic of Android SDK components like ListAdapters. For that you can use the FirebaseUI library.

FirebaseUI allows you to quickly connect common UI elements to the Firebase database for data storage. Below is an example of using FirebaseUI with a FirebaseListAdapter.

mAdapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class, android.R.layout.two_line_list_item, ref) {
    @Override
    protected void populateView(View view, ChatMessage chatMessage) {
        ((TextView)view.findViewById(android.R.id.text1)).setText(chatMessage.getName());
        ((TextView)view.findViewById(android.R.id.text2)).setText(chatMessage.getMessage());

    }
};
messagesView.setListAdapter(mAdapter);

That's just the gist of everything. The documentation of Firebase is pretty comprehensive (and human readable if I do so myself).

Community
  • 1
  • 1
David East
  • 31,526
  • 6
  • 67
  • 82
11

Firebase is a NoSQL type database that makes use of sockets, which allows the client to receive information live - without having to make GET requests to the server.

This requires that when you set things up you 'subscribe' the client to the database/collection.

In terms of how you could use it in an application, that depends on the technologies you wish to use in your stack. The firebase website has documentation advising you on how to do this.

Also, if you are asking solely on the basis of wishing to have the functionality you have described; the Meteor framework comes with a sockets based, NoSQL, backend database which is very easy to implement; there is a tutorial here; https://www.meteor.com/tutorials/blaze/creating-an-app

Barris
  • 969
  • 13
  • 29
8

Its been a while since you asked the question and Firebase has added new documentation. Here is what it says,

Prerequisites

  • Android's version should be 2.3 or newer with Google Play services 9.2.1 or newer
  • Android Studio 1.5 or higher.
  • An Android Studio project and its package name (The package name can be found from ApplicationManifest.xml).

Add Firebase to your application

  • Create an account on Firebase.
  • Create a project in Firebase console.
  • Click Firebase for Android app and follow the instructions.
  • When prompted, enter app's package name.
  • Download google-services.json file.
  • Place google-services.json file in your app's module folder. Typically /app.

Add the SDK

If you would like to integrate Firebase libraries into your projects, you need to perform few basic tasks to prepare your Android SDK project.

  • First, add rules to your root-level build.gradle file, to include the google-services plugin:

    buildscript { // ... dependencies { // ... classpath 'com.google.gms:google-services:3.0.0' } }

Then, in your module Gradle file (usually the app/build.gradle), add the apply plugin line at the bottom of the file to enable the Gradle plugin:

apply plugin: 'com.android.application'

android {
  // ...
}

dependencies {
  // ...
  compile 'com.google.firebase:firebase-core:9.2.1'
}
// ADD THIS AT THE BOTTOM

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

You should also add the dependencies for the Firebase SDKs you wish to use - we recommend starting with the firebase-core, which include Firebase Analytics, but see below for the full list.

Sagar D
  • 2,588
  • 1
  • 18
  • 31
  • `You should also add the dependencies for the Firebase SDKs you wish to use - we recommend starting with the firebase-core, which include Firebase Analytics, but see below for the full list.` This is from the Google site.... What does that even mean? How do we add and what to add exactly? – Si8 Oct 30 '16 at 23:18
  • He has Copied it from https://firebase.google.com/docs/android/setup prefer the link it has rest of the content – Menuka Ishan Dec 19 '16 at 04:21