這里是FireBase官網(wǎng),使用需要連入外網(wǎng)。
FireBase能做哪些事?
接入firebase,就類似于接入了集成國外第三方登錄,友盟Push,友盟分析,阿里云存儲,云控制等一系列功能。
接入流程:
1.創(chuàng)建項目

2.添加android應(yīng)用

3.下載google-services配置文件,放入app根目錄下。

4.在項目級build.gradle中配置:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
classpath 'com.google.gms:google-services:4.3.10'
// Import the BoM for the Firebase platform
implementation(platform("com.google.firebase:firebase-bom:33.1.1"))
// Add the dependencies for the Crashlytics and Analytics libraries
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation("com.google.firebase:firebase-crashlytics")
implementation("com.google.firebase:firebase-analytics-ktx")
implementation 'com.google.firebase:firebase-messaging-ktx'
implementation 'com.google.android.gms:play-services-auth:20.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
在模塊下build.gradle中配置:
apply plugin: 'com.android.application'
// Add this line
apply plugin: 'com.google.gms.google-services'
dependencies {
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:30.1.0')
// Add the dependency for the Firebase SDK for Google Analytics
// When using the BoM, don't specify versions in Firebase dependencies
implementation 'com.google.firebase:firebase-analytics-ktx'
}
在settings.gradle中:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
maven { url 'https://jitpack.io' } //add line
}
}
最后,Sync Now一下,BUILD SUCCESSFUL。
1.app埋點:Analytics
應(yīng)用內(nèi)數(shù)據(jù)上報,幫助分析用戶在app內(nèi)的行為
埋點上報:
var firebaseAnalytics: FirebaseAnalytics = Firebase.analytics
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.APP_OPEN, Bundle().apply {
putString("appid", "1001")
putLong("time", System.currentTimeMillis())
})
2.云消息推送:Firebase Cloud Message
即:FCM,幫助app推送通知
獲取消息推送token,然后可以去上報自己服務(wù)器,要獲取成功token的前提是設(shè)備安裝了GMS服務(wù),以及外網(wǎng)連接。
private fun getMessageToken() {
FirebaseMessaging.getInstance().token.addOnCompleteListener {
if (!it.isSuccessful) {
Log.i("minfos", "獲取token失敗" + it.exception)
return@addOnCompleteListener
}
//國內(nèi)需要連接VPN方能獲取成功
val token = it.result
Toast.makeText(applicationContext, "獲取token" + token, Toast.LENGTH_LONG).show()
Log.i("minfos", "獲取到token:" + token)
}
}
3.身份驗證:Authentication
方便的實現(xiàn)google登錄,facebook登錄,twitter登錄,github登錄,郵箱登錄,電話登錄以及自定義驗證登錄
private fun signInClient() {
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Scope(""))
.requestIdToken("")
.requestProfile()
.requestServerAuthCode("", true)
.requestEmail()
.build();
// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(ctx, gso)
}
4.實時數(shù)據(jù)庫:Database和最新的Firestore
無需搭建服務(wù)器就能擁有一個實時的數(shù)據(jù)庫,可以用來保存自己想要保存的任何數(shù)據(jù)。
5.云倉庫:Cloud Storage
無需搭建服務(wù)器就能擁有一個云倉庫,可以用來保存文件,如圖片、音頻、視頻。不過免費版最多保存1個G的文件。
6.app崩潰報告:Firebase Crashlytics
自動記錄應(yīng)用內(nèi)崩潰信息,只需簡單的幾步,就可以將Firebase Crashlytics添加到安卓工程中,然后Firebase Crashlytics就會自動的收集應(yīng)用內(nèi)崩潰信息,包括錯誤類型,代碼定位等等,非常的方便實用
7.Firebase遠程配置:Remote Config
相當于在服務(wù)器上設(shè)置幾個key-value字段,我們在應(yīng)用內(nèi)可以請求這幾個字段,通過value值設(shè)置我們的app。
是的,這兩種方法一樣可以實現(xiàn)遠程配置。只是用Firebase遠程配置實現(xiàn)的話,對app的性能影響最低,實現(xiàn)起來也更優(yōu)雅。
8.A/B測試
通過Firebase遠程配置的A/B測試,幫助了解哪種配置用戶更喜歡。
9.動態(tài)鏈接:Dynamic Link
生成一個根據(jù)不同的場景響應(yīng)不同行為的鏈接。
10.邀請:Firebase invites
邀請好友,基于Firebase動態(tài)鏈接。使用邀請功能讓用戶邀請好友下載或打開app更加的方便。
11.AdWords
幫助投放app,就是給錢讓谷歌給你打廣告。
可繼續(xù)接入的功能
1.第三方登錄

2.實時概覽

3.日志搜集

4.用戶分析

5.事件打點

6.消息推送

首次發(fā)送消息,需要驗證FCM注冊令牌,使用該方法可獲取令牌值:
if (!task.isSuccessful) {
Log.i("minfo", "Fetching FCM registration token failed", task.exception)
return@OnCompleteListener
}
// Get new FCM registration token
val token = task.result
Log.i("minfo", token)
})
消息推送成功:

7.云控配置
