firebase 接入和部分方法

This commit is contained in:
renhaoting 2026-01-04 18:18:31 +08:00
parent a722d84f06
commit 1123dc2c9e
5 changed files with 135 additions and 7 deletions

View File

@ -1,6 +1,8 @@
plugins { plugins {
alias(libs.plugins.androidLibrary) alias(libs.plugins.androidLibrary)
alias(libs.plugins.kotlinAndroid) alias(libs.plugins.kotlinAndroid)
id("com.google.gms.google-services")
id("com.google.firebase.crashlytics")
} }
android { android {
@ -54,4 +56,13 @@ dependencies {
api("com.google.android.gms:play-services-ads-identifier:18.2.0") api("com.google.android.gms:play-services-ads-identifier:18.2.0")
implementation("cn.thinkingdata.android:ThinkingAnalyticsSDK:3.0.2") implementation("cn.thinkingdata.android:ThinkingAnalyticsSDK:3.0.2")
// 导入 Firebase BoMBill of Materials统一管理版本
implementation(platform("com.google.firebase:firebase-bom:33.16.0"))
// 添加你需要的 Firebase 产品依赖(无需指定版本号)
implementation("com.google.firebase:firebase-analytics")
implementation("com.google.firebase:firebase-crashlytics")
implementation("com.google.firebase:firebase-config")
} }

View File

@ -3,5 +3,6 @@
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
</manifest> </manifest>

View File

@ -1,11 +1,14 @@
package com.gamedog.statisticreporter.firbase package com.gamedog.statisticreporter.firbase
import android.content.Context
import android.os.Bundle
import com.ama.core.architecture.BaseApp import com.ama.core.architecture.BaseApp
import com.google.firebase.analytics.FirebaseAnalytics
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
import java.util.concurrent.ConcurrentHashMap
class FireBaseManager private constructor() { class FireBaseManager private constructor() {
@ -25,22 +28,125 @@ class FireBaseManager private constructor() {
private val mBgScope = CoroutineScope(SupervisorJob() + Dispatchers.Default) private val mBgScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
private val mMainScope = CoroutineScope(SupervisorJob() + Dispatchers.Main) private val mMainScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
private lateinit var mFbAnalytics: FirebaseAnalytics
private val mDefaultParams = ConcurrentHashMap<String, Any>()
fun init(fireAppId: String) {
init {
if (!::mFbAnalytics.isInitialized) {
mFbAnalytics = FirebaseAnalytics.getInstance(mAppContext)
}
}
fun reportEvent(eventName: String, parameters: Map<String, Any>? = null) {
if (!::mFbAnalytics.isInitialized) {
throw IllegalStateException("FirebaseAnalyticsUtil not initialized. Call init() first.")
}
val bundle = Bundle().apply {
// 默认参数添加
mDefaultParams.forEach { (key, value) ->
when (value) {
is String -> putString(key, value)
is Int -> putInt(key, value)
is Long -> putLong(key, value)
is Double -> putDouble(key, value)
is Float -> putFloat(key, value)
is Boolean -> putBoolean(key, value)
}
}
// 本次事件特定参数添加
parameters?.forEach { (key, value) ->
when (value) {
is String -> putString(key, value)
is Int -> putInt(key, value)
is Long -> putLong(key, value)
is Double -> putDouble(key, value)
is Float -> putFloat(key, value)
is Boolean -> putBoolean(key, value)
else -> putString(key, value.toString())
}
}
}
mFbAnalytics.logEvent(eventName, bundle)
} }
//----------------------- PRIVATE ------------------------// /**
* 记录推荐事件使用 Firebase 预设事件类型
* @param eventType 事件类型 FirebaseAnalytics.Event.SELECT_CONTENT
* @param parameters 事件参数
*/
fun reportRecommendedEvent(eventType: String, parameters: Bundle) {
if (!::mFbAnalytics.isInitialized) {
throw IllegalStateException("FirebaseAnalyticsUtil not initialized. Call init() first.")
}
mFbAnalytics.logEvent(eventType, parameters)
}
/**
* 设置用户属性
* @param propertyName 属性名称
* @param propertyValue 属性值
*/
fun setUserProperty(propertyName: String, propertyValue: String) {
if (!::mFbAnalytics.isInitialized) return
mFbAnalytics.setUserProperty(propertyName, propertyValue)
}
/**
* 设置用户ID
* @param userId 用户唯一标识
*/
fun setUserId(userId: String) {
if (!::mFbAnalytics.isInitialized) return
mFbAnalytics.setUserId(userId)
}
/**
* 设置当前屏幕
* @param screenName 屏幕名称
* @param screenClass 屏幕类名可选
*/
fun setCurrentScreen(context: Context, screenName: String, screenClass: String? = null) {
if (!::mFbAnalytics.isInitialized) return
val bundle = Bundle().apply {
putString(FirebaseAnalytics.Param.SCREEN_NAME, screenName)
screenClass?.let { putString(FirebaseAnalytics.Param.SCREEN_CLASS, it) }
}
mFbAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle)
}
/**
* 设置默认事件参数将自动添加到每个事件中
* @param parameters 默认参数映射
*/
fun setDefaultEventParameters(parameters: Map<String, Any>) {
mDefaultParams.clear()
mDefaultParams.putAll(parameters)
}
/**
* 添加单个默认事件参数
*/
fun addDefaultEventParameter(key: String, value: Any) {
mDefaultParams[key] = value
}
/**
* 清除所有默认事件参数
*/
fun clearDefaultEventParameters() {
mDefaultParams.clear()
}
} }

View File

@ -21,6 +21,9 @@ plugins {
alias(libs.plugins.moduleGraph) apply true // Plugin applied to allow module graph generation alias(libs.plugins.moduleGraph) apply true // Plugin applied to allow module graph generation
alias(libs.plugins.kotlin.kapt) alias(libs.plugins.kotlin.kapt)
id("com.google.gms.google-services") version "4.4.3" apply false
id("com.google.firebase.crashlytics") version "2.9.9" apply false
} }

View File

@ -28,6 +28,10 @@ swiperefreshlayout = "1.1.0"
material = "1.12.0" material = "1.12.0"
protobuf = "4.29.3" protobuf = "4.29.3"
kotlinxSerializationJson = "1.8.1" kotlinxSerializationJson = "1.8.1"
google-services = "4.4.3"
crashlytics-gradle = "2.9.9"
#================================Other库相关=================================== #================================Other库相关===================================
baseRecyclerViewAdapterHelper = "4.1.6" baseRecyclerViewAdapterHelper = "4.1.6"
magicIndicator = "1.7.0" magicIndicator = "1.7.0"
@ -65,6 +69,7 @@ firebaseBom = "34.1.0"
lifecycleProcess = "2.8.3" lifecycleProcess = "2.8.3"
[libraries] [libraries]
#================================Test相关=================================== #================================Test相关===================================
junit = { group = "junit", name = "junit", version.ref = "junit" } junit = { group = "junit", name = "junit", version.ref = "junit" }
@ -150,7 +155,6 @@ firebase-crashlytics = { group = "com.google.firebase", name = "firebase-crashly
[plugins] [plugins]
# android # android
androidApplication = { id = "com.android.application", version.ref = "agp" } androidApplication = { id = "com.android.application", version.ref = "agp" }
@ -177,3 +181,6 @@ versions = { id = "com.github.ben-manes.versions", version.ref = "check-versions
moduleGraph = { id = "com.jraska.module.graph.assertion", version.ref = "moduleGraph" } moduleGraph = { id = "com.jraska.module.graph.assertion", version.ref = "moduleGraph" }
kotlin-parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin" } kotlin-parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin" }
room = { id = "androidx.room", version.ref = "roomRuntime" } room = { id = "androidx.room", version.ref = "roomRuntime" }
google-services = { id = "com.google.gms.google-services", version.ref = "google-services" }
firebase-crashlytics = { id = "com.google.firebase.crashlytics", version.ref = "crashlytics-gradle" }