Slide 1

Slide 1 text

Firebase Essentials For Android ¬ GDG WonderCoders, May 2016

Slide 2

Slide 2 text

Erik Jhordan González Reyes Android developer "Strong believer in software craftsmanship, SOLID, clean code and testing."

Slide 3

Slide 3 text

Talk Schedule ● Firebase Guide for android ● Android + Firebase Sample ● Answers & Questions

Slide 4

Slide 4 text

Installation & Setup

Slide 5

Slide 5 text

dependencies { compile 'com.firebase:firebase-client-android:2.5.2+' } Firebase

Slide 6

Slide 6 text

Firebase android { ... packagingOptions { exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE-FIREBASE.txt' exclude 'META-INF/NOTICE' } } If you are getting a build error complaining about duplicate files you can choose to exclude those files.

Slide 7

Slide 7 text

Firebase Add Internet Permission

Slide 8

Slide 8 text

@Override public void onCreate() { super.onCreate(); Firebase.setAndroidContext(this); // other setup code } Firebase setup firebase on android

Slide 9

Slide 9 text

Understanding Data Firebase

Slide 10

Slide 10 text

Firebase All Firebase database data is stored as JSON objects. { "users": { "mchen": { "friends": { "brinchen": true }, "name": "Mary Chen", // our child node appears in the existing JSON tree "widgets": { "one": true, "three": true } }, "brinchen": { ... }, "hmadi": { ... } } }

Slide 11

Slide 11 text

Firebase These types correspond to the types available in JSON. ●String ●Boolean ●Long ●Double ●Map ●List

Slide 12

Slide 12 text

Firebase Creating a Firebase database Reference Firebase rootRef = new Firebase("https://docs-examples.firebaseio.com/web/data");

Slide 13

Slide 13 text

Saving Data Firebase

Slide 14

Slide 14 text

Firebase Ways to Save Data four methods for writing data to the Firebase database. ● setValue() ● updateChildren() ● push() ● runTransaction()

Slide 15

Slide 15 text

Firebase setValue() Saves data to the specified Firebase reference, replacing any existing data at that path. Example -Firebase Reference -Object -save data using setValue()

Slide 16

Slide 16 text

Firebase Firebase ref = new Firebase("https://docs-examples.firebaseio.com/android/saving-data/fireblog"); Firebase Reference

Slide 17

Slide 17 text

Firebase public class User { private int birthYear; private String fullName; public User() {} public User(String fullName, int birthYear) { this.fullName = fullName; this.birthYear = birthYear; } public long getBirthYear() { return birthYear; } public String getFullName() { return fullName; } } Creating an Object

Slide 18

Slide 18 text

Firebase Firebase alanRef = ref.child("users").child("alanisawesome"); User alan = new User("Alan Turing", 1912); alanRef.setValue(alan); Saving Data

Slide 19

Slide 19 text

Firebase { "users": { "alanisawesome": { "birthYear": "1912", "fullName": "Alan Turing" } } } Result

Slide 20

Slide 20 text

Firebase updateChildren() If you want to write to specific children of a node at the same time without overwriting other child nodes. Firebase alanRef = usersRef.child("alanisawesome"); Map nickname = new HashMap(); nickname.put("nickname", "Alan The Machine"); alanRef.updateChildren(nickname);

Slide 21

Slide 21 text

Firebase Result { "users": { "alanisawesome": { "birthYear": "1912", "fullName": "Alan Turing", "nickname": "Alan The Machine", } } }

Slide 22

Slide 22 text

Firebase Using multi-path updates updateChildren() can now update values at multiple locations in your Firebase database at the same time. Map nicknames = new HashMap(); nickname.put("alanisawesome/nickname", "Alan The Machine"); nickname.put("gracehop/nickname", "Amazing Grace"); usersRef.updateChildren(nicknames);

Slide 23

Slide 23 text

Firebase Result "users": { "alanisawesome": { "date_of_birth": "June 23, 1912", "full_name": "Alan Turing", "nickname": "Alan The Machine" }, "gracehop": { "date_of_birth": "December 9, 1906", "full_name": "Grace Hopper", "nickname": "Amazing Grace" } } }

Slide 24

Slide 24 text

Firebase Adding a Completion Callback ref.setValue("I'm writing data", new Firebase.CompletionListener() { @Override public void onComplete(FirebaseError firebaseError, Firebase firebase) { if (firebaseError != null) { System.out.println("Data could not be saved. " + firebaseError. getMessage()); } else { System.out.println("Data saved successfully."); } } });

Slide 25

Slide 25 text

Firebase push() Generates a unique ID every time a new child is added to the specified Firebase reference. Firebase postRef = ref.child("posts"); Map post1 = new HashMap(); post1.put("author", "gracehop"); post1.put("title", "Announcing COBOL, a New Programming Language"); postRef.push().setValue(post1); Map post2 = new HashMap(); post2.put("author", "alanisawesome"); post2.put("title", "The Turing Machine"); postRef.push().setValue(post2);

Slide 26

Slide 26 text

Firebase Result { "posts": { "-JRHTHaIs-jNPLXOQivY": { "author": "gracehop", "title": "Announcing COBOL,Programming Language" }, "-JRHTHaKuITFIhnj02kE": { "author": "alanisawesome", "title": "The Turing Machine" } } }

Slide 27

Slide 27 text

Retrieving Data Firebase

Slide 28

Slide 28 text

Firebase Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts"); // Attach an listener to read the data at our posts reference 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()); } });

Slide 29

Slide 29 text

Firebase Read event types public class BlogPost { private String author; private String title; public BlogPost() { // empty default constructor, necessary for Firebase to be able to deserialize blog posts } public String getAuthor() { return author; } public String getTitle() { return title; } }

Slide 30

Slide 30 text

Firebase Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts"); // Attach an listener to read the data at our posts reference ref.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { System.out.println("There are " + snapshot.getChildrenCount() + " blog posts"); for (DataSnapshot postSnapshot: snapshot.getChildren()) { BlogPost post = postSnapshot.getValue(BlogPost.class); System.out.println(post.getAuthor() + " - " + post.getTitle()); } } @Override public void onCancelled(FirebaseError firebaseError) { System.out.println("The read failed: " + firebaseError.getMessage()); } });

Slide 31

Slide 31 text

Structuring Data Firebase

Slide 32

Slide 32 text

Firebase Because we can nest data up to 32 levels deep, it's tempting to think that this should be the default structure. However, when we fetch data at a location in our database, we also retrieve all of its child nodes. Therefore, nesting data must be done with careful consideration for how the data will be read later. Use Nested Data Sparingly

Slide 33

Slide 33 text

Understanding Security Firebase

Slide 34

Slide 34 text

Firebase ● Authentication ● Authorization ● Data Validation Overview

Slide 35

Slide 35 text

User Authentication Firebase

Slide 36

Slide 36 text

Firebase Firebase makes authentication easy. It can integrate with your existing login server, or authenticate users with only client-side code. It has built-in functionality for email & password, and third-party providers such as Facebook, Twitter, GitHub, and Google. Authentication

Slide 37

Slide 37 text

Offline Capabilities Firebase

Slide 38

Slide 38 text

Firebase Firebase apps automatically handle temporary network interruptions for you. Cached data will still be available while offline and your writes will be resent when network connectivity is recovered. Enabling disk persistence allows our app to also keep all of its state even after an app restart. We can enable disk persistence with just one line of code. Firebase.getDefaultConfig().setPersistenceEnabled(true); Disk Persistence

Slide 39

Slide 39 text

Firebase Tools Firebase

Slide 40

Slide 40 text

Firebase ● FirebaseUI (UI Bindings) https://github.com/firebase/firebaseui-android ● GeoFire (Geo Location) https://github.com/firebase/geofire-java Libraries

Slide 41

Slide 41 text

Firebase ● Office Mover https://github.com/firebase/office-mover-5000 ● Chat https://github.com/firebase/AndroidChat ● Drawing https://github.com/firebase/AndroidDrawing ● SF Vehicles https://github.com/firebase/geofire-java/tree/master/examples/SFVehicles ● Login Demo https://github.com/firebase/firebase-login-demo-android Examples

Slide 42

Slide 42 text

#Show me code Firebase

Slide 43

Slide 43 text

Firebase https://github.com/erikcaffrey/Firebase-chat

Slide 44

Slide 44 text

Answers & Questions

Slide 45

Slide 45 text

Further Reading ● My Personal Blog https://erikcaffrey.github.io ● Firebase Documentation https://www.firebase.com/docs/android/ ● Udacity - Firebase Essentials For Android https://www.udacity.com/course/firebase-essentials-for-android--ud009 ● Github - Firebase https://github.com/firebase

Slide 46

Slide 46 text

Erik Jhordan González Reyes Android Developer +Erik Jhordan Rey Caffrey @ErikJhordan_Rey erikcaffrey erikcaffrey.github.io

Slide 47

Slide 47 text

firebase.child(“ThankYou”).setValue(“GDGWonderCoders”);