Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Firebase with Android

Firebase with Android

Firebase with Android

Fumihiko Shiroyama

March 08, 2016
Tweet

More Decks by Fumihiko Shiroyama

Other Decks in Programming

Transcript

  1. $VTUPN"QQMJDBUJPO public class MyApplication extends Application {
 @Override
 public void

    onCreate() {
 super.onCreate();
 Firebase.setAndroidContext(this);
 }
 }
  2. 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; } }
  3. /P42-+40/5SFF { "users": { "mchen": { "friends": { "brinchen": true

    }, "name": "Mary Chen", // our child node appears in the existing JSON tree "widgets": { "one": true, "three": true } }, "brinchen": { ... }, "hmadi": { ... } } }
  4. /P42-+40/5SFF { "users": { "mchen": { "friends": { "brinchen": true

    }, "name": "Mary Chen", // our child node appears in the existing JSON tree "widgets": { "one": true, "three": true } }, "brinchen": { ... }, "hmadi": { ... } } }
  5. /P42-+40/5SFF { "users": { "mchen": { "friends": { "brinchen": true

    }, "name": "Mary Chen", // our child node appears in the existing JSON tree "widgets": { "one": true, "three": true } }, "brinchen": { ... }, "hmadi": { ... } } } VTFSTNDIFOXJEHFUT
  6. /P42-+40/5SFF { "users": { "mchen": { "friends": { "brinchen": true

    }, "name": "Mary Chen", // our child node appears in the existing JSON tree "widgets": { "one": true, "three": true } }, "brinchen": { ... }, "hmadi": { ... } } } VTFSTNDIFOXJEHFUT 6OJRVF*EFOUJpFS
  7. Firebase firebaseRef = new Firebase("https://shiroyama-app-01.firebaseio.com/");
 firebaseRef.child("message").addValueEventListener(new ValueEventListener() {
 @Override
 public

    void onDataChange(DataSnapshot dataSnapshot) {
 Toast.makeText( MainActivity.this, dataSnapshot.getValue(String.class), Toast.LENGTH_SHORT ).show();
 }
 
 @Override
 public void onCancelled(FirebaseError firebaseError) {
 
 }
 });
  8. Firebase firebaseRef = new Firebase("https://shiroyama-app-01.firebaseio.com/");
 firebaseRef.child("message").addValueEventListener(new ValueEventListener() {
 @Override
 public

    void onDataChange(DataSnapshot dataSnapshot) {
 Toast.makeText( MainActivity.this, dataSnapshot.getValue(String.class), Toast.LENGTH_SHORT ).show();
 }
 
 @Override
 public void onCancelled(FirebaseError firebaseError) {
 
 }
 });
  9. Firebase firebaseRef = new Firebase("https://shiroyama-app-01.firebaseio.com/");
 firebaseRef.child("message").addValueEventListener(new ValueEventListener() {
 @Override
 public

    void onDataChange(DataSnapshot dataSnapshot) {
 Toast.makeText( MainActivity.this, dataSnapshot.getValue(String.class), Toast.LENGTH_SHORT ).show();
 }
 
 @Override
 public void onCancelled(FirebaseError firebaseError) {
 
 }
 });
  10. Firebase firebaseRef = new Firebase("https://shiroyama-app-01.firebaseio.com/");
 firebaseRef.child("message").addValueEventListener(new ValueEventListener() {
 @Override
 public

    void onDataChange(DataSnapshot dataSnapshot) {
 Toast.makeText( MainActivity.this, dataSnapshot.getValue(String.class), Toast.LENGTH_SHORT ).show();
 }
 
 @Override
 public void onCancelled(FirebaseError firebaseError) {
 
 }
 });
  11. Firebase firebaseRef = new Firebase("https://shiroyama-app-01.firebaseio.com/");
 firebaseRef.child("message").addValueEventListener(new ValueEventListener() {
 @Override
 public

    void onDataChange(DataSnapshot dataSnapshot) {
 Toast.makeText( MainActivity.this, dataSnapshot.getValue(String.class), Toast.LENGTH_SHORT ).show();
 }
 
 @Override
 public void onCancelled(FirebaseError firebaseError) {
 
 }
 });
  12. // Get a reference to our posts 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()); } });
  13. // Get a reference to our posts 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()); } });
  14. // Get a reference to our posts 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()); } });
  15. // Get a reference to our posts Firebase ref =

    new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts"); ref.addChildEventListener(new ChildEventListener() { // Retrieve new posts as they are added to the database @Override public void onChildAdded(DataSnapshot snapshot, String previousChildKey) { BlogPost newPost = snapshot.getValue(BlogPost.class); System.out.println("Author: " + newPost.getAuthor()); System.out.println("Title: " + newPost.getTitle()); } //... ChildEventListener also defines onChildChanged, onChildRemoved, // onChildMoved and onCanceled, covered in later sections. });
  16. @Override public void onChildChanged(DataSnapshot snapshot, String previousChildKey) { String title

    = (String) snapshot.child("title").getValue(); System.out.println("The updated post title is " + title); }
  17. @Override public void onChildRemoved(DataSnapshot snapshot) { String title = (String)

    snapshot.child("title").getValue(); System.out.println("The blog post titled " + title + " has been deleted"); }
  18. @Override public void onChildMoved(DataSnapshot snapshot, String previousChildKey) { String title

    = (String) snapshot.child("title").getValue(); System.out.println("The updated post title is " + title); }
  19. ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { //

    do some stuff once } @Override public void onCancelled(FirebaseError firebaseError) { } });
  20. ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { //

    do some stuff once } @Override public void onCancelled(FirebaseError firebaseError) { } });
  21. { "lambeosaurus": { "height" : 2.1, "length" : 12.5, "weight":

    5000 }, "stegosaurus": { "height" : 4, "length" : 9, "weight" : 2500 } }
  22. public class DinosaurFacts { long height; double length; long weight;

    public DinosaurFacts() { // empty default constructor // necessary for Firebase to be able to deserialize blog posts } public long getHeight() { return height; } public double getLength() { return length; } public long getWeight() { return weight; } }
  23. PSEFS#Z,FZ Query queryRef = ref.orderByKey(); { "lambeosaurus": { "height" :

    2.1, "length" : 12.5, "weight": 5000 }, "stegosaurus": { "height" : 4, "length" : 9, "weight" : 2500 } }
  24. PSEFS#Z7BMVF Query queryRef = ref.orderByValue(); { "scores": { "bruhathkayosaurus" :

    55, "lambeosaurus" : 21, "linhenykus" : 80, "pterodactyl" : 93, "stegosaurus" : 5, "triceratops" : 22 } }
  25. Query queryRef = ref.orderByChild("height").limitToFirst(2); Query queryRef = scoresRef.orderByValue().limitToLast(3); Query queryRef

    = ref.orderByChild("height").startAt(3); Query queryRef = ref.orderByKey().endAt("pterodactyl"); Query queryRef = ref.orderByChild("height").equalTo(25);
  26. { "rules": { "users": { "$user_id": { ".write": "$user_id ===

    auth.uid" } } } } UBMLTBCPVUUIJTMBUFS
  27. Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/"); ref.authWithCustomToken(AUTH_TOKEN, new Firebase.AuthResultHandler() { @Override

    public void onAuthenticationError(FirebaseError error) { System.err.println("Login Failed! " + error.getMessage()); } @Override public void onAuthenticated(AuthData authData) { System.out.println("Login Succeeded!"); } });
  28. Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/"); ref.authWithCustomToken(AUTH_TOKEN, new Firebase.AuthResultHandler() { @Override

    public void onAuthenticationError(FirebaseError error) { System.err.println("Login Failed! " + error.getMessage()); } @Override public void onAuthenticated(AuthData authData) { System.out.println("Login Succeeded!"); } });
  29. Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/"); ref.authWithCustomToken(AUTH_TOKEN, new Firebase.AuthResultHandler() { @Override

    public void onAuthenticationError(FirebaseError error) { System.err.println("Login Failed! " + error.getMessage()); } @Override public void onAuthenticated(AuthData authData) { System.out.println("Login Succeeded!"); } }); HJWFOGSPNZPVSTFSWFS
  30. Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/"); ref.authWithCustomToken(AUTH_TOKEN, new Firebase.AuthResultHandler() { @Override

    public void onAuthenticationError(FirebaseError error) { System.err.println("Login Failed! " + error.getMessage()); } @Override public void onAuthenticated(AuthData authData) { System.out.println("Login Succeeded!"); } });