Share
Embed
Quickly build your app alone with Firebase- Android Makers 2018
Creating a personal Android app requires us to solve a lot of common challenges. Sometimes some of these challenges require so much time that we start to forget about the product we are building. Firebase can act as a toolbox to give functionality like analytics, databases, messaging and crash reporting so we can move quickly and focus on our users.
Renaud MATHIEU
April 24, 2018
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
81 slides
-
You might need ‣ Feedback system ‣ Authentication ‣ Social
Integration ‣ Analytics ‣ Remote control ‣ Notifications ‣ Device compatibility ‣ Permissions ‣ Tests ‣ Architecture ‣ Kotlin -
You might need ‣ Feedback system ‣ Authentication ‣ Social
Integration ‣ Analytics ‣ Remote control ‣ Notifications ‣ Device compatibility ‣ Permissions ‣ Tests ‣ Architecture ‣ Kotlin -
Firebase Products Cloud Firestore Cloud Functions Authentication Cloud Storage Crashlytics
Analytics Remote Config Feedback Infra Messaging -
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.1' implementation 'com.google.firebase:firebase-core:15.0.0' implementation 'com.google.firebase:firebase-config:15.0.0' implementation 'com.google.firebase:firebase-messaging:15.0.0' implementation 'com.google.firebase:firebase-firestore:15.0.0'
implementation 'com.google.firebase:firebase-storage:15.0.0' implementation 'com.google.firebase:firebase-auth:15.0.0' build.gradle Firebase Products -
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.1' implementation 'com.google.firebase:firebase-core:15.0.0' implementation 'com.google.firebase:firebase-config:15.0.0' implementation 'com.google.firebase:firebase-messaging:15.0.0' implementation 'com.google.firebase:firebase-firestore:15.0.0'
implementation 'com.google.firebase:firebase-storage:15.0.0' implementation ‘com.google.firebase:firebase-auth:15.0.0' build.gradle Firebase Products -
Core abstract class BaseFragment : Fragment() { lateinit var firebaseAnalytics:
FirebaseAnalytics override fun onAttach(context: Context?) { super.onAttach(context) firebaseAnalytics = FirebaseAnalytics.getInstance(this) } } 2/4 -
Core // Analytics firebaseAnalytics.logEvent(FirebaseAnalytics.Event.ECOMMERCE_PURCHASE, Bundle().apply { putString(FirebaseAnalytics.Param.CURRENCY, receiptModel.currency) putDouble(FirebaseAnalytics.Param.PRICE, receiptModel.amount
/ 100.0) putString(FirebaseAnalytics.Param.TRANSACTION_ID, receiptModel.id) putInt(FirebaseAnalytics.Param.QUANTITY, receiptModel.items.sumBy { it.quantity }) }) 3/4 -
Crashlytics <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application> <meta-data android:name="firebase_crashlytics_collection_enabled" android:value="false"
/> </application> </manifest> debug/AndroidManifest.xml -
Crashlytics class CrashlyticsTree(val applicationTag: String) : Timber.Tree() { override fun
log(priority: Int, tag: String?, message: String, t: Throwable?) { val crashlyticsTag = tag ?: applicationTag // Log the message to Crashlytics, so we can see it in crash reports Crashlytics.log(priority, crashlyticsTag, message); // Log the exception in Crashlytics if we have one. if (t != null) { Crashlytics.logException(t) } // If this is an error or a warning, log it as a exception so we see it in Crashlytics. if (priority > Log.WARN) { Crashlytics.logException(Throwable(message)) } } } -
Crashlytics object LogManager { fun init(tag: String) { Timber.plant(Timber.DebugTree()) }
fun setUserId(id: String) { // No-op in Debug } } -
Crashlytics object LogManager { fun init(tag: String) { Timber.plant(CrashlyticsTree(tag)) }
fun setUserId(id: String) { Crashlytics.setUserIdentifier(id) } } -
Crashlytics object LogManager { fun init(tag: String) { Timber.plant(Timber.DebugTree()) }
fun setUserId(id: String) { // No-op in Debug } } object LogManager { fun init(tag: String) { Timber.plant(CrashlyticsTree(tag)) } fun setUserId(id: String) { Crashlytics.setUserIdentifier(id) } } private fun initLogManager() { if (!BuildConfig.DEBUG) { Fabric.with(this, Crashlytics()) } LogManager.init(getString(R.string.app_name)) } -
Remote Config @Singleton @Component( modules = arrayOf( AndroidSupportInjectionModule::class, ApplicationModule::class, ActivityModule::class,
FirebaseModule::class ) ) interface ApplicationComponent : AndroidInjector<NeosApplication> { @Component.Builder interface Builder { fun build(): ApplicationComponent @BindsInstance fun application(application: Application): Builder } } -
Remote Config @Module class FirebaseModule { @Singleton @Provides fun provideFirebaseRemoteConfig():
FirebaseRemoteConfig = FirebaseRemoteConfig.getInstance() } -
Remote Config abstract class BaseActivity : AppCompatActivity(), HasSupportFragmentInjector { companion
object { const val FIREBASE_REMOTE_CONFIG_CACHE_EXPIRATION: Long = 3600 } @Inject lateinit var firebaseRemoteConfig: FirebaseRemoteConfig override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) super.onCreate(savedInstanceState) initAndFetchFirebaseRemoteConfig() } private fun initAndFetchFirebaseRemoteConfig() { firebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults) firebaseRemoteConfig.fetch(FIREBASE_REMOTE_CONFIG_CACHE_EXPIRATION).addOnCompleteListener { if (it.isSuccessful) { firebaseRemoteConfig.activateFetched() } else { Timber.w("Firebase Remote Config Fetch Failed.") } } } } -
Remote Config abstract class BaseActivity : AppCompatActivity(), HasSupportFragmentInjector { companion
object { const val FIREBASE_REMOTE_CONFIG_CACHE_EXPIRATION: Long = 3600 } @Inject lateinit var firebaseRemoteConfig: FirebaseRemoteConfig override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) super.onCreate(savedInstanceState) initAndFetchFirebaseRemoteConfig() } private fun initAndFetchFirebaseRemoteConfig() { firebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults) firebaseRemoteConfig.fetch(FIREBASE_REMOTE_CONFIG_CACHE_EXPIRATION).addOnCompleteListener { if (it.isSuccessful) { firebaseRemoteConfig.activateFetched() } else { Timber.w("Firebase Remote Config Fetch Failed.") } } } } -
Remote Config abstract class BaseActivity : AppCompatActivity(), HasSupportFragmentInjector { companion
object { const val FIREBASE_REMOTE_CONFIG_CACHE_EXPIRATION: Long = 3600 } @Inject lateinit var firebaseRemoteConfig: FirebaseRemoteConfig override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) super.onCreate(savedInstanceState) initAndFetchFirebaseRemoteConfig() } private fun initAndFetchFirebaseRemoteConfig() { firebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults) firebaseRemoteConfig.fetch(FIREBASE_REMOTE_CONFIG_CACHE_EXPIRATION).addOnCompleteListener { if (it.isSuccessful) { firebaseRemoteConfig.activateFetched() } else { Timber.w("Firebase Remote Config Fetch Failed.") } } } } -
Remote Config abstract class BaseFragment : Fragment(), HasSupportFragmentInjector { @Inject
lateinit var childFragmentInjector: DispatchingAndroidInjector<Fragment> @Inject lateinit var firebaseRemoteConfig: FirebaseRemoteConfig lateinit var firebaseAnalytics: FirebaseAnalytics override fun onAttach(context: Context?) { AndroidSupportInjection.inject(this) super.onAttach(context) firebaseAnalytics = FirebaseAnalytics.getInstance(context) } } -
Remote Config class ProfileFragment @Inject constructor() : BaseFragment() { val
URL_SHARING: String by lazy { firebaseRemoteConfig.getString(REMOTE_CONFIG_SHARING_URL) } private fun shareNeosApp() { val intent = Intent().apply { action = Intent.ACTION_SEND putExtra(Intent.EXTRA_TEXT, URL_SHARING) type = "text/plain" } startActivity(Intent.createChooser( intent, resources.getText(R.string.profile_share))) } } -
Remote Config class ProfileFragment @Inject constructor() : BaseFragment() { val
URL_SHARING: String by lazy { firebaseRemoteConfig.getString(REMOTE_CONFIG_SHARING_URL) } private fun shareNeosApp() { val intent = Intent().apply { action = Intent.ACTION_SEND putExtra(Intent.EXTRA_TEXT, URL_SHARING) type = "text/plain" } startActivity(Intent.createChooser( intent, resources.getText(R.string.profile_share))) } } -
Remote Config class ProfileFragment @Inject constructor() : BaseFragment() { val
URL_SHARING: String by lazy { firebaseRemoteConfig.getString(REMOTE_CONFIG_SHARING_URL) } private fun shareNeosApp() { val intent = Intent().apply { action = Intent.ACTION_SEND putExtra(Intent.EXTRA_TEXT, URL_SHARING) type = "text/plain" } startActivity(Intent.createChooser( intent, resources.getText(R.string.profile_share))) } } -
Messaging As of April 10, 2018, Google has deprecated GCM.
The GCM server and client APIs are deprecated and will be removed as soon as April 11, 2019. -
Authentication ‣ Easily add sign-in to your Android app with
FirebaseUI ‣ Get started with the Firebase SDK -
Firestore BÊTA @Module class FirebaseModule { @Singleton @Provides fun provideFirebaseRemoteConfig():
FirebaseRemoteConfig = FirebaseRemoteConfig.getInstance() @Singleton @Provides fun provideFirebaseAuth(): FirebaseAuth = FirebaseAuth.getInstance() } -
class HomeActivity : BaseActivity() { @Inject lateinit var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) super.onCreate(savedInstanceState) setContentView(R.layout.activity_home) if (auth.currentUser != null) { // Set user properties or welcome back? } else { startActivityForResult( // Get an instance of AuthUI based on the default app AuthUI.getInstance().createSignInIntentBuilder().build(), RC_SIGN_IN ) } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == RC_SIGN_IN) { val response = IdpResponse.fromResultIntent(data) // Successfully signed in if (resultCode == Activity.RESULT_OK) { Timber.w("Successfully signed in") } else { // Sign in failed if (response == null) { // User pressed back button return } if (response.error!!.errorCode == ErrorCodes.NO_NETWORK) { // Handle this case return } Timber.e(response.error) } } } -
class HomeActivity : BaseActivity() { @Inject lateinit var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) super.onCreate(savedInstanceState) setContentView(R.layout.activity_home) if (auth.currentUser != null) { // Set user properties or welcome back? } else { startActivityForResult( // Get an instance of AuthUI based on the default app AuthUI.getInstance().createSignInIntentBuilder().build(), RC_SIGN_IN ) } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == RC_SIGN_IN) { val response = IdpResponse.fromResultIntent(data) // Successfully signed in if (resultCode == Activity.RESULT_OK) { Timber.w("Successfully signed in") } else { // Sign in failed if (response == null) { // User pressed back button return } if (response.error!!.errorCode == ErrorCodes.NO_NETWORK) { // Handle this case return } Timber.e(response.error) } } } -
class HomeActivity : BaseActivity() { @Inject lateinit var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) super.onCreate(savedInstanceState) setContentView(R.layout.activity_home) if (auth.currentUser != null) { // Set user properties or welcome back? } else { startActivityForResult( // Get an instance of AuthUI based on the default app AuthUI.getInstance().createSignInIntentBuilder().build(), RC_SIGN_IN ) } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == RC_SIGN_IN) { val response = IdpResponse.fromResultIntent(data) // Successfully signed in if (resultCode == Activity.RESULT_OK) { Timber.w("Successfully signed in") } else { // Sign in failed if (response == null) { // User pressed back button return } if (response.error!!.errorCode == ErrorCodes.NO_NETWORK) { // Handle this case return } Timber.e(response.error) } } } -
signOutButton.setOnClickListener { v -> AuthUI.getInstance() .signOut(this) .addOnCompleteListener { Timber.d("User is
now signed out") } } Authentication -
Authentication ‣ Migrate from Firebase to your back end #
npm install -g firebase-tools # firebase login # firebase init # firebase list # firebase auth:export myexport.csv --format=csv -
Firestore BETA @Module class FirebaseModule { @Singleton @Provides fun provideFirebaseRemoteConfig():
FirebaseRemoteConfig = FirebaseRemoteConfig.getInstance() @Singleton @Provides fun provideFirebaseAuth(): FirebaseAuth = FirebaseAuth.getInstance() @Singleton @Provides fun provideFirebaseFirestore(): FirebaseFirestore = FirebaseFirestore.getInstance() } -
Firestore BETA @Singleton @Component( modules = arrayOf( AndroidSupportInjectionModule::class, ApplicationModule::class, ActivityModule::class,
RepositoryModule::class, FirebaseModule::class ) ) interface ApplicationComponent : AndroidInjector<NeosApplication> { @Component.Builder interface Builder { fun build(): ApplicationComponent @BindsInstance fun application(application: Application): Builder } } -
Firestore BETA @Module class RepositoryModule { @Singleton @Provides fun provideDataRepository(firestore:
FirebaseFirestore): DataRepository { return DataRepository(firestore) } } -
Firestore BETA class DataRepository(val firestore: FirebaseFirestore) { /** * STORES
*/ fun getStores(): Single<List<StoreDomain>> { return Single.create { firestore.collection("stores") .get() .addOnSuccessListener { query -> it.onSuccess(query.toObjects(StoreDomain::class.java)) } .addOnFailureListener { exception -> it.onError(exception) } } } } -
Firestore BETA ‣ Structuring Security Rules service cloud.firestore { match
/databases/{database}/documents { // Match any document in the 'stores' collection match /stores/{store} { allow read: if <condition>; allow write: if <condition>; } } } -
Firestore BETA ‣ Structuring Security Rules // Allow read/write access
on all documents to any user signed in to the application service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if request.auth.uid != null; } } } -
Firestore BETA ‣ Structuring Security Rules service cloud.firestore { match
/databases/{database}/documents { // A read rule can be divided into get and list rules match /cities/{city} { // Applies to single document read requests allow get: if <condition>; // Applies to queries and collection read requests allow list: if <condition>; } // A write rule can be divided into create, update, and delete rules match /cities/{city} { // Applies to writes to nonexistent documents allow create: if <condition>; // Applies to writes to existing documents allow update: if <condition>; // Applies to delete operations allow delete: if <condition>; } } } -
@Module class FirebaseModule { @Singleton @Provides fun provideFirebaseRemoteConfig(): FirebaseRemoteConfig =
FirebaseRemoteConfig.getInstance() @Singleton @Provides fun provideFirebaseAuth(): FirebaseAuth = FirebaseAuth.getInstance() @Singleton @Provides fun provideFirebaseFirestore(): FirebaseFirestore = FirebaseFirestore.getInstance() @Singleton @Provides fun provideFirebaseStorage(): FirebaseStorage = FirebaseStorage.getInstance() } Cloud Storage -
Cloud Storage @Module class RepositoryModule { @Singleton @Provides fun provideDataRepository(
firestore: FirebaseFirestore, firebaseStorage: FirebaseStorage ): DataRepository { return DataRepository(firestore, firebaseStorage) } } -
Cloud Storage class DataRepository( val firestore: FirebaseFirestore, val storage: FirebaseStorage
) { fun getStoreLogoURL() { storage.reference.child("stores/logos/gep_rive_gauche.png") .getDownloadUrl() .addOnSuccessListener { // Emit Single? Download to a file? } .addOnFailureListener { Timber.e(it) } } } -
Cloud Storage ‣ Using FirebaseUI to download and display images
using Glide @GlideModule class MyAppGlideModule : AppGlideModule() { fun registerComponents(context: Context, glide: Glide, registry: Registry) { // Register FirebaseImageLoader to handle StorageReference registry.append( StorageReference::class.java, InputStream::class.java, FirebaseImageLoader.Factory() ) } } -
Cloud Storage ‣ Structuring Security Rules // Allow read/write access
on all documents to any user signed in to the application service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write: if request.auth.uid != null; } } } -
Cloud Functions BÊTA Scenarios ‣ Notify users when something interesting
happens. ‣ Perform Realtime Database sanitization and maintenance. ‣ Execute intensive tasks in the cloud instead of in your app. ‣ Integrate with third-party services and APIs.