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

Using Java to interact with Firebase in Android

44a168e6578c2cc83aaf54a38458ade9?s=47 Magda Miu
November 01, 2018

Using Java to interact with Firebase in Android

Firebase is a platform used to build better apps, improve their quality and also to grow businesses. This talk will cover examples about how to create a new project in Firebase console, how to integrate Google sign-in in the Java code in Android, how to s

44a168e6578c2cc83aaf54a38458ade9?s=128

Magda Miu

November 01, 2018
Tweet

Transcript

  1. Using Java to interact with Firebase in Android Magda Miu

    @magdamiu
  2. What is Firebase? Build better apps Improve app quality Grow

    your business
  3. None
  4. Firebase works across platforms iOS Android Web C++ Unity

  5. Developer experience matters

  6. GDG Pitesti App 1. Authentication using Google Sign-In 2. Get

    the events from Cloud Firestore 3. Receive notifications 4. Detect and solve issues
  7. Firebase Console

  8. Create new project

  9. Connect your app to the Firebase project

  10. Connect your app to the Firebase project

  11. Connect your app to the Firebase project

  12. Connect your app to the Firebase project

  13. Connect your app to the Firebase project

  14. Agenda Firebase Authentication Firebase Cloud Firestore Firebase Cloud Messaging Firebase

    Crashlytics
  15. Agenda Firebase Authentication Firebase Cloud Firestore Firebase Cloud Messaging Firebase

    Crashlytics
  16. Firebase Authentication • Email & password • Google, Facebook, Twitter,

    and GitHub sign-in • Phone number
  17. //project level dependencies { classpath 'com.android.tools.build:gradle:3.2.1' classpath 'com.google.gms:google-services:4.0.1' ... }

    //app level implementation 'com.google.firebase:firebase-auth:16.0.5' implementation 'com.google.android.gms:play-services-auth:16.0.1' apply plugin: 'com.google.gms.google-services'
  18. GoogleSignInOptions gso = new GoogleSignInOptions .Builder(GoogleSignInOptions .DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.default_web_client_id)) .requestEmail() .build();

  19. GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso); FirebaseAuth mAuth = FirebaseAuth.getInstance();

  20. Intent signInIntent = mGoogleSignInClient.getSignInIntent(); startActivityForResult(signInIntent, RC_SIGN_IN);

  21. //onActivityResult if (requestCode == RC_SIGN_IN) { Task<GoogleSignInAccount> task = GoogleSignIn.

    getSignedInAccountFromIntent(data); try { GoogleSignInAccount account = task.getResult(ApiException.class); firebaseAuthWithGoogle(account); } catch (ApiException e) { Logging.show(TAG, "Google sign in failed", e); } }
  22. //firebaseAuthWithGoogle AuthCredential credential = GoogleAuthProvider. getCredential(acct.getIdToken(), null); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new

    OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { //… } else { //… } } });
  23. <uses-permission android:name="android.permission.INTERNET" />

  24. Agenda Firebase Authentication Firebase Cloud Firestore Firebase Cloud Messaging Firebase

    Crashlytics
  25. Firebase Cloud Firestore • NoSQL database built for global apps

    • Query and structure data the way you like • Build truly serverless apps • Sync data across devices, on or offline • Scale globally • Strong user-based security
  26. Firebase Cloud Firestore v/s Firebase Realtime Database • Different data

    structure • Better querying on FS • Scalability • Pricing • Security • Write batch
  27. FirebaseFirestore mFirebaseFirestore = FirebaseFirestore.getInstance();

  28. mFirebaseFirestore.collection(FirestorePath.EVENTS) .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task)

    { if (task.isSuccessful()) { for (QueryDocumentSnapshot document : task.getResult()) { mEvents.add(parseData(document)); } } else { Log.w(TAG, "Error getting documents.", task.getException()); } } });
  29. mFirebaseFirestore.collection(FirestorePath.EVENTS) .add(eventMap) .addOnSuccessListener(new OnSuccessListener<DocumentReference>() { @Override public void onSuccess(DocumentReference documentReference)

    { } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { } });
  30. Agenda Firebase Authentication Firebase Cloud Firestore Firebase Cloud Messaging Firebase

    Crashlytics
  31. Firebase Cloud Messaging • Send messages to any device •

    Advanced message targeting • Customized notification content • No coding required for sending notifications
  32. implementation 'com.google.firebase:firebase-messaging:17.3.4'

  33. public class FirebaseImplementation extends FirebaseMessagingService { @Override public void onNewToken(String

    s) { super.onNewToken(s); } @Override public void onMessageReceived(RemoteMessage message) { sendMyNotification(message.getNotification().getBody()); } ...
  34. <service android:name=".FirebaseImplementation"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>

  35. <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_menu_manage" /> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/colorAccent" /> <meta-data android:name="com.google.android.gms.version"

    android:value="@integer/google_play_services_version" />
  36. FirebaseMessaging.getInstance().setAutoInitEnabled(true); FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(MainAc tivity.this, new OnSuccessListener<InstanceIdResult>() { @Override public void onSuccess(InstanceIdResult

    instanceIdResult) { String newToken = instanceIdResult.getToken(); } });
  37. FirebaseMessaging.getInstance().subscribeToTopic("the_topic") .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task)

    { } });
  38. Agenda Firebase Authentication Firebase Cloud Firestore Firebase Cloud Messaging Firebase

    Crashlytics
  39. Firebase Crashlytics • Powerful, real time crash reporting • Quickly

    pinpoint the root cause of crashes • Understand what issue to tackle first • Never miss a critical crash +
  40. maven { url 'https://maven.fabric.io/public' } classpath 'io.fabric.tools:gradle:1.26.1' implementation 'com.crashlytics.sdk.android:crashlytics:2.9.5' implementation('com.crashlytics.sdk.android:crashlytics:2.9.5@aar')

    { transitive = true; } <meta-data android:name="io.fabric.ApiKey" android:value="the_value_etc" />
  41. Fabric.with(this, new Crashlytics()); Crashlytics.getInstance().crash(); // Force a crash

  42. Resources • https://firebase.google.com/ • https://codelabs.developers.g oogle.com/ • https://firebase.google.com/d ocs/guides/

  43. Thank you! Magda Miu @magdamiu