Slide 1

Slide 1 text

Using Firebase in Android Apps Aljan Porquillo @forceporquillo Software Engineer, Finastra

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

• 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

Slide 4

Slide 4 text

What is Firebase?

Slide 5

Slide 5 text

Google’s mobile application development platform that helps developers build, improve, and grow apps. - Firebase Website

Slide 6

Slide 6 text

Suite of tools that enable developers to develop high quality mobile apps

Slide 7

Slide 7 text

build, improve, grow

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

• 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.

Slide 10

Slide 10 text

• It's an efficient, low-latency solution for mobile apps that require synced states across clients in real-time. • Offline support capability

Slide 11

Slide 11 text

• Store and sync data between users and devices - at global scale - using a cloud-hosted, NoSQL database. • Powerful queries • Offline support capability

Slide 12

Slide 12 text

• Simplify your web hosting with tools made specifically for modern web apps.

Slide 13

Slide 13 text

Backed by Google Cloud Platform Infrastructure

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Java/Kotlin Objective-C/Swift Native platforms Client SDKs

Slide 16

Slide 16 text

Supported platforms Cross-platform Client SDKs

Slide 17

Slide 17 text

“It solves all the common mobile App Development challenges” Why Firebase is popular among mobile developers?

Slide 18

Slide 18 text

The Challenges in Android Development

Slide 19

Slide 19 text

Designed by https://www.behance.net/jeaaanius

Slide 20

Slide 20 text

Authentication • Login via email, Facebook, Google. • To store and identify current user state.

Slide 21

Slide 21 text

Persistence Data Storage • Store current state of Medicine intake count • Upload documents/images

Slide 22

Slide 22 text

How?

Slide 23

Slide 23 text

Authentication? Data persistence (offline/online sync)? Improve and Monitor? Grow and Scale? How to handle

Slide 24

Slide 24 text

Traditional Android Development

Slide 25

Slide 25 text

Android Development with Firebase

Slide 26

Slide 26 text

Getting Started

Slide 27

Slide 27 text

// 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 } } }

Slide 28

Slide 28 text

// 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. } }

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

// 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 { override fun onSuccess(result: LoginResult) { signInFirebaseWithFacebookAccessToken(result.accessToken) } // comment out for abbrev. }

Slide 31

Slide 31 text

// 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