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

Firebase with Android

Firebase with Android

Firebase with Android

3cca191bf3064fd059ea2c3d6022afbd?s=128

Fumihiko Shiroyama

March 08, 2016
Tweet

Transcript

  1. 'JSFCBTFXJUI "OESPJE ($16(50,:0."3$)

  2. 'JSFCBTF

  3. 'JSFCBTF w 3FBMUJNF%BUBCBTF w "VUIFOUJDBUJPO"DDFTT$POUSPM w 4UBUJD8FC4JUF)PTUJOH

  4. 6EBDJUZ w 'JSFCBTF&TTFOUJBM'PS"OESPJE w IUUQTXXXVEBDJUZDPNDPVSTFpSFCBTF FTTFOUJBMTGPSBOESPJEVE

  5. (FUUJOH4UBSUFE

  6. XXXpSFCBTFDPN

  7. None
  8. None
  9. None
  10. dependencies { compile 'com.firebase:firebase-client-android:2.5.2+' }

  11. android { ... packagingOptions { exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE-FIREBASE.txt' exclude

    'META-INF/NOTICE' } }
  12. <uses-permission android:name="android.permission.INTERNET" />

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

    onCreate() {
 super.onCreate();
 Firebase.setAndroidContext(this);
 }
 }
  14. "OESPJE.BOJGFTUYNM <application
 android:name=".MyApplication"
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:supportsRtl="true"
 android:theme="@style/AppTheme">

  15. 8SJUJOH%BUB

  16. "OESPJE.BOJGFTUYNM Firebase firebaseRef = new Firebase("https://shiroyama-app-01.firebaseio.com/");
 firebaseRef.child("message").setValue("hello firebase!");

  17. "DUJWJUZ Firebase firebaseRef = new Firebase("https://shiroyama-app-01.firebaseio.com/");
 firebaseRef.child("message").setValue("hello firebase!"); 3FGFSFODF

  18. "DUJWJUZ Firebase firebaseRef = new Firebase("https://shiroyama-app-01.firebaseio.com/");
 firebaseRef.child("message").setValue("hello firebase!"); $IJME/PEF

  19. "DUJWJUZ Firebase firebaseRef = new Firebase("https://shiroyama-app-01.firebaseio.com/");
 firebaseRef.child("message").setValue("hello firebase!"); 8SJUJOH

  20. None
  21. /PU-JNJUFEUP 1SJNJUJWF5ZQFT

  22. 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; } }
  23. 6OEFSTUBUJOH %BUB4USVDUVSF

  24. /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": { ... } } }
  25. /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": { ... } } }
  26. /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
  27. /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
  28. 7BMJE5ZQFT

  29. w 4USJOH w #PPMFBO w -POH w %PVCMF w .BQ4USJOH

    0CKFDU w -JTU0CKFDU
  30. 3FUSJFWJOH%BUB

  31. 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) {
 
 }
 });
  32. 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) {
 
 }
 });
  33. 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) {
 
 }
 });
  34. 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) {
 
 }
 });
  35. 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) {
 
 }
 });
  36. 3FBE&WFOU5ZQFT

  37. w 7BMVF w $IJME"EEFE w $IJME$IBOHFE w $IJME3FNPWFE w $IJME.PWFE

  38. 7BMVF&WFOU w 7BMVF&WFOU-JTUFOFSPO%BUB$IBOHF w 3FUSJFWFXIPMFEBUBBUPODF w $BMMFEPODFBUJOJUJBMUJNF UIFOFWFSZUJNF EBUBDIBOHFT

  39. // 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()); } });
  40. // 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()); } });
  41. // 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()); } });
  42. $IJME"EEFE&WFOU w $IJME&WFOU-JTUFOFSPO$IJME"EEFE w 0ODFGPSFBDIFYJTUJOHDIJMEJOJUJBMMZ w &WFSZUJNFOFXEBUBJTBEEFE

  43. // 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. });
  44. $IJME$IBOHFE&WFOU w $IJME&WFOU-JTUFOFSPO$IJME$IBOHFE w "OZUJNFBDIJMEJTDIBOHFEJODMVEJOHJUT EFTDFOEBOUT

  45. @Override public void onChildChanged(DataSnapshot snapshot, String previousChildKey) { String title

    = (String) snapshot.child("title").getValue(); System.out.println("The updated post title is " + title); }
  46. $IJME3FNPWFE&WFOU w $IJME&WFOU-JTUFOFSPO$IJME3FNPWFE w "OZUJNFBDIJMEJTSFNPWFE

  47. @Override public void onChildRemoved(DataSnapshot snapshot) { String title = (String)

    snapshot.child("title").getValue(); System.out.println("The blog post titled " + title + " has been deleted"); }
  48. $IJME.PWFE&WFOU w $IJME&WFOU-JTUFOFSPO$IJME.PWFE w "OZUJNFBDIJMEJTNPWFE

  49. @Override public void onChildMoved(DataSnapshot snapshot, String previousChildKey) { String title

    = (String) snapshot.child("title").getValue(); System.out.println("The updated post title is " + title); }
  50. %FUBDIJOH$BMMCBDLT w SFGSFNPWF&WFOU-JTUFOFS PSJHJOBM-JTUFOFS  w BMMMJTUFOFSTNVTUCFSFNPWFEFYQMJDJUMZ w SFNPWJOHQBSFOUMJTUFOFSXJMM/05SFNPWF DIJMETMJTUFOFST

  51. 3FBEJOH%BUB0ODF

  52. ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { //

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

    do some stuff once } @Override public void onCancelled(FirebaseError firebaseError) { } });
  54. 2VFSZJOH%BUB

  55. { "lambeosaurus": { "height" : 2.1, "length" : 12.5, "weight":

    5000 }, "stegosaurus": { "height" : 4, "length" : 9, "weight" : 2500 } }
  56. 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; } }
  57. w PSEFS#Z$IJME  w PSEFS#Z,FZ  w PSEFS#Z7BMVF  w

    PSEFS#Z1SJPSJUZ
  58. PSEFS#Z$IJME Query queryRef = ref.orderByChild("height"); or Query queryRef = ref.orderByChild("dimensions/height");

  59. PSEFS#Z,FZ Query queryRef = ref.orderByKey(); { "lambeosaurus": { "height" :

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

    55, "lambeosaurus" : 21, "linhenykus" : 80, "pterodactyl" : 93, "stegosaurus" : 5, "triceratops" : 22 } }
  61. PSEFS#Z1SJPSJUZ Query queryRef = ref.orderByPriority();

  62. PSEFS#Z1SJPSJUZ Query queryRef = ref.orderByPriority(); BSCJUSBSZQSJPSJUZZPVTFU

  63. w MJNJU5P'JSTU  w MJNJU5P-BTU  w TUBSU"U  w

    FOE"U  w FRVBM5P
  64. 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);
  65. 4FDVSJUZ

  66. None
  67. { "rules": { "users": { "$user_id": { ".write": "$user_id ===

    auth.uid" } } } }
  68. { "rules": { "users": { "$user_id": { ".write": "$user_id ===

    auth.uid" } } } }
  69. { "rules": { "users": { "$user_id": { ".write": "$user_id ===

    auth.uid" } } } } UBMLTBCPVUUIJTMBUFS
  70. "VUIFOUJDBUJPO

  71. $VTUPN

  72. w IFMQFSMJCSBSJFTUPJOUFHSBUFXJUIZPVSPXO BVUIFOUJDBUJPOT w IUUQTXXXpSFCBTFDPNEPDTBOESPJE HVJEFMPHJODVTUPNIUNMTFDUJPOSFTUUPLFO IFMQFSMJCSBSJFT

  73. 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!"); } });
  74. 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!"); } });
  75. 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
  76. 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!"); } });
  77. w KBWBEPD w IUUQTXXXpSFCBTFDPNEPDTKBWBBQJ KBWBEPDDPNpSFCBTFDMJFOU"VUI%BUBIUNM w "VUI%BUBHFU6JE 6OJRVF(VBSBOUFFE

  78. 5JQT

  79. w 4XJQF3FGSFTITIPVMECFVTFEXJUI 7BMVF&WFOU PODF

  80. -JNJUBUJPOT

  81. w /P'JMF6QMPBEJOH"1*T w 4FSWFSTJEFCBUDIJOH w 4FSWFSTJEFTDIFEVMJOH w 4FSWFSTJEFFWFOUIPPL

  82. 2"

  83. 4BNQMFT

  84. w $IBU w IUUQTHJUIVCDPNpSFCBTF"OESPJE$IBU w %SBXJOH w IUUQTHJUIVCDPNpSFCBTF"OESPJE%SBXJOH

  85. 5)9