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

Using Java to interact with Firebase in Android

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

Magda Miu

November 01, 2018
Tweet

More Decks by Magda Miu

Other Decks in Programming

Transcript

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

    the events from Cloud Firestore 3. Receive notifications 4. Detect and solve issues
  2. //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'
  3. //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); } }
  4. //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 { //… } } });
  5. 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
  6. Firebase Cloud Firestore v/s Firebase Realtime Database • Different data

    structure • Better querying on FS • Scalability • Pricing • Security • Write batch
  7. 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()); } } });
  8. Firebase Cloud Messaging • Send messages to any device •

    Advanced message targeting • Customized notification content • No coding required for sending notifications
  9. public class FirebaseImplementation extends FirebaseMessagingService { @Override public void onNewToken(String

    s) { super.onNewToken(s); } @Override public void onMessageReceived(RemoteMessage message) { sendMyNotification(message.getNotification().getBody()); } ...
  10. 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 +