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

Using Firebase in Android Apps

Using Firebase in Android Apps

Explore curated features and services of Firebase from Authentication, Realtime Database, Cloud Firestore, Cloud Storage, etc. This session will highlight the de facto features of Firebase used to start building a full-fledged working Android app.

strongforce1

January 28, 2023
Tweet

More Decks by strongforce1

Other Decks in Programming

Transcript

  1. History of myself • BS CS Specialized in Software Engineering;

    FEU Institute of Technology • Currently working for a British financial software • Freelance Android Developer, 2019-present • Open-source contributor (Android, Google, Firebase, Tinder, Telegram, Signal, etc.) • I love building things around Android and the JVM
  2. • What is Firebase? • Common challenges in Android development

    • Integrating Firebase in your existing Android app • Authentication – Login user using Email, Facebook, Google • Realtime Database – chat app • Cloud Storage – image upload Agenda
  3. Evolution of Firebase 2011 2012 2014 2016 2017 Envolve Online

    chat functionality Realtime Database Introduce Cloud Firestore 2016 Acquired by Auth, Hosting born Founded by James Taplin and Andrew Lee
  4. • Manage your users in a simple and secure way.

    • Offers multiple methods to authenticate, including email and password, third-party providers like Google, Facebook, etc.
  5. • It's an efficient, low-latency solution for mobile apps that

    require synced states across clients in real-time. • Offline support capability
  6. • Store and sync data between users and devices -

    at global scale - using a cloud-hosted, NoSQL database. • Powerful queries • Offline support capability
  7. “It solves all the common mobile App Development challenges” Why

    Firebase is popular among mobile developers?
  8. // See documentation at https://firebase.google.com/docs/auth/android/password-auth fun signInFirebaseWithEmailPassword(email: String, password: String)

    { Firebase.auth.signInWithEmailAndPassword(email, password) .addOnCompleteListener { if (it.isSuccessful) { navigateToMainScreen() } else { // display error message } } }
  9. // See documentation at https://developers.google.com/identity/sign-in/android/sign-in // Configure sign-in to request

    the user's ID, email address, and basic // profile. ID and basic profile are included in DEFAULT_SIGN_IN. private val signInOptions: GoogleSignInOptions by lazy { GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.default_web_client_id)) .requestEmail() .build() } private val googleSignInContract = registerForActivityResult( ActivityResultContracts.StartActivityForResult() ) { result -> if (result.resultCode != RESULT_OK) { Logger.e("GoogleSignInContract: failed") return@registerForActivityResult } val signInTask = GoogleSignIn.getSignedInAccountFromIntent(result.data) try { val googleSignInAccount = signInTask.getResult(ApiException::class.java) signInFirebaseWithGoogleAccount(googleSignInAccount) } catch (e: ApiException) { // consume error message and display to the user. } }
  10. fun signInFirebaseWithGoogleAccount(googleSignIn: GoogleSignInAccount) { val credential = GoogleAuthProvider.getCredential(googleSignIn.idToken, null) Firebase.auth.signInWithCredential(credential).addOnCompleteListener

    { if (it.isSuccessful) { navigateToMainScreen() } else { // display error message } } } // invoke Google sign in button to authenticate with Firebase Auth. binding.signInWithGoogleBtn.setOnClickListener { val signInClient = GoogleSignIn.getClient(requireContext(), signInOptions) googleSignInContract.launch(signInClient.signInIntent) } // See documentation at https://firebase.google.com/docs/auth/android/google-signin
  11. // See documentation at https://developers.facebook.com/docs/facebook-login/android // CallbackManager manages the callbacks

    into the FacebookSdk from // an Activity's or Fragment's onActivityResult() method. private val callbackManager = CallbackManager.Factory.create() // Since we are not going to use Facebook’s default Login UI Button, // therefore, we will manually configure our LoginManager logic to our // custom LoginButton UI. private val loginManager by lazy { LoginManager.getInstance().apply { setLoginBehavior(LoginBehavior.DIALOG_ONLY) setAuthType(ServerProtocol.DIALOG_REREQUEST_AUTH_TYPE) registerCallback(callbackManager, facebookCallback) … } } // A callback listener for retrieving Facebook login result private val facebookCallback = object : FacebookCallback<LoginResult> { override fun onSuccess(result: LoginResult) { signInFirebaseWithFacebookAccessToken(result.accessToken) } // comment out for abbrev. }
  12. // Request permission for accessing user email and public profile.

    val facebookPermissions = listOf("email", "public_profile") // invoke Facebook login button to authenticate with Firebase Auth. binding.signInWithFacebookBtn.setOnClickListener { facebookLoginContract.launch(facebookPermissions) } fun signInFirebaseWithFacebookAccessToken(accessToken: AccessToken) { val credential = FacebookAuthProvider.getCredential(accessToken.token) Firebase.auth.signInWithCredential(credential) .addOnCompleteListener { if (it.isSuccessful) { navigateToMainScreen() } else { // display error message } } } // See documentation at https://firebase.google.com/docs/auth/android/facebook-login