Vous êtes sur la page 1sur 4

8/20/2016

Set up Firebase Realtime Database for Android | Firebase

Set up Firebase Realtime Database for Android


Connect your app to Firebase
1. Install the Firebase SDK(https:// rebase.google.com/docs/android/setup).
2. In the Firebase console (://firebase.google.com/console/), add your app to your
Firebase project.

Add the Realtime Database to your app


Add the dependency for Firebase Realtime Database to your app-level build.gradle le:
compile 'com.google.firebase:firebase-database:9.4.0'

Con gure Firebase Database Rules


The Realtime Database provides a declarative rules language that allows you to de ne how your data should be structured, how
it should be indexed, and when your data can be read from and written to. By default, read and write access to your database is
restricted so only authenticated users can read or write data. To get started without setting up Authentication
(https:// rebase.google.com/docs/auth/), you can con gure your rules for public access
(https:// rebase.google.com/docs/database/security/quickstart#sample-rules). This does make your database open to anyone,
even people not using your app, so be sure to restrict your database again when you set up authentication.

https://rebase.google.com/docs/database/android/start/

1/4

8/20/2016

Set up Firebase Realtime Database for Android | Firebase

Write to your database


Retrieve an instance of your database using getInstance() and reference the location you want to write to.
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");

You can save a range of data types to the database this way, including Java objects. When you save an object the responses
from any getters will be saved as children of this location.

Read from your database


To make your app data update in realtime, you should add a ValueEventListener
(https://firebase.google.com/docs/reference/android/com/google/firebase/database/ValueEventListener) to

the reference you just created.


The onDataChange() method in this class is triggered once when the listener is attached and again every time the data changes,
including the children.
// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.

https://rebase.google.com/docs/database/android/start/

2/4

8/20/2016

Set up Firebase Realtime Database for Android | Firebase

String value = dataSnapshot.getValue(String.class);


Log.d(TAG, "Value is: " + value);
}





});

@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}

Optional: Con gure ProGuard


When using Firebase Realtime Database in your app along with ProGuard you need to consider how your model objects will be
serialized and deserialized after obfuscation. If you use DataSnapshot.getValue(Class) or
DatabaseReference.setValue(Object) to read and write data you will need to add rules to the proguard-rules.pro le:
# Add this global rule
-keepattributes Signature
# This rule will properly ProGuard all the model classes in
# the package com.yourcompany.models. Modify to fit the structure
# of your app.
-keepclassmembers class com.yourcompany.models.** {
*;
}

Prepare for Launch


https://rebase.google.com/docs/database/android/start/

3/4

8/20/2016

Set up Firebase Realtime Database for Android | Firebase

Before launching your app, we recommend walking through our launch checklist
(https:// rebase.google.com/support/guides/launch-checklist) to make sure your app is ready to go!

Next Steps
See your new database in the Firebase console (://firebase.google.com/console/).
Learn more about how to structure data and navigate the JSON tree in our documentation
(https:// rebase.google.com/docs/database/android/structure-data).
If you are upgrading from a 2.X version of the Firebase SDK, please read our upgrade guide for Android
(https:// rebase.google.com/support/guides/ rebase-android).
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License
(http://creativecommons.org/licenses/by/3.0/), and code samples are licensed under the Apache 2.0 License(http://www.apache.org/licenses/LICENSE-2.0).
For details, see our Site Policies(https:// rebase.google.com/site-policies). Java is a registered trademark of Oracle and/or its af liates.
Last updated August 15, 2016.

https://rebase.google.com/docs/database/android/start/

4/4

Vous aimerez peut-être aussi