Commit c3391b64 authored by wanglei's avatar wanglei

...gid

parent b35986a1
# appzxhy # appzxhy
用来快速搭建项目滴 用来快速搭建项目滴
\ No newline at end of file
# 商业化
## Application中
1.公共参数 uuid 和 gid
2.初始化广告
3.初始化Facebook
4.FCM上报topic和token
...@@ -6,11 +6,15 @@ import android.content.Context ...@@ -6,11 +6,15 @@ import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.text.TextUtils import android.text.TextUtils
import android.util.Log import android.util.Log
import com.base.appzxhy.ads.AdsMgr
import com.base.appzxhy.bean.ConstObject.appLanguageCountrySp import com.base.appzxhy.bean.ConstObject.appLanguageCountrySp
import com.base.appzxhy.bean.ConstObject.appLanguageSp import com.base.appzxhy.bean.ConstObject.appLanguageSp
import com.base.appzxhy.fcm.FCMManager
import com.base.appzxhy.utils.ActivityManagerUtils import com.base.appzxhy.utils.ActivityManagerUtils
import com.base.appzxhy.utils.AppPreferences import com.base.appzxhy.utils.AppPreferences
import com.base.appzxhy.utils.LogEx import com.base.appzxhy.utils.LogEx
import com.facebook.FacebookSdk
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import com.hjq.language.MultiLanguages import com.hjq.language.MultiLanguages
import com.hjq.language.OnLanguageListener import com.hjq.language.OnLanguageListener
import java.util.Locale import java.util.Locale
...@@ -44,6 +48,7 @@ class MyApplication : Application() { ...@@ -44,6 +48,7 @@ class MyApplication : Application() {
super.onCreate() super.onCreate()
appContext = this appContext = this
initUUid() initUUid()
initGid()
initApp() initApp()
// 初始化语种切换框架 // 初始化语种切换框架
...@@ -66,6 +71,14 @@ class MyApplication : Application() { ...@@ -66,6 +71,14 @@ class MyApplication : Application() {
}) })
} }
private fun initGid() {
Thread {
val info: AdvertisingIdClient.Info = AdvertisingIdClient.getAdvertisingIdInfo(applicationContext)
val advertisingId = info.id
AppPreferences.getInstance().put("gid", advertisingId)
}.start()
}
override fun attachBaseContext(base: Context?) { override fun attachBaseContext(base: Context?) {
super.attachBaseContext(MultiLanguages.attach(base)) super.attachBaseContext(MultiLanguages.attach(base))
} }
...@@ -82,14 +95,14 @@ class MyApplication : Application() { ...@@ -82,14 +95,14 @@ class MyApplication : Application() {
private fun initApp() { private fun initApp() {
//初始化广告相关业务 //初始化广告相关业务
// com.base.appzxhy.ads.AdsMgr.init(appContext) AdsMgr.init(appContext)
// FacebookSdk.sdkInitialize(applicationContext) FacebookSdk.sdkInitialize(applicationContext)
// val token = AppPreferences.getInstance().getString("token", "") // val token = AppPreferences.getInstance().getString("token", "")
// val topic = AppConfig.packageName + "_push" val topic = GlobalConfig.PACKAGE_NAME + "_push"
// LogEx.logDebug(TAG, "topic=${topic} token=$token") // LogEx.logDebug(TAG, "topic=${topic} token=$token")
// FCMManager.initFirebase(this) FCMManager.initFirebase(this)
// FCMManager.subscribeToTopic(topic) FCMManager.subscribeToTopic(topic)
// initConfig() // initConfig()
// //
......
package com.base.appzxhy.fcm
import android.content.Context
import android.util.Log
import com.base.appzxhy.helper.EventUtils.event
import com.base.appzxhy.utils.AppPreferences
import com.base.appzxhy.utils.LogEx
import com.google.android.gms.tasks.OnCompleteListener
import com.google.android.gms.tasks.Task
import com.google.firebase.FirebaseApp
import com.google.firebase.messaging.FirebaseMessaging
import org.json.JSONObject
object FCMManager {
fun initFirebase(context: Context?) {
FirebaseApp.initializeApp(context!!)
LogEx.logDebug("initFirebase", "initFirebase")
getToken()
}
fun subscribeToTopic(topic: String) {
Log.e("FCMUtil", "subscribeToTopic")
FirebaseMessaging.getInstance().subscribeToTopic(topic)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
getToken()
Log.e("FCMUtil", "isSuccessful")
event("FCM_Topic_$topic", null, null)
} else {
event("FCM_Error", "subscribeToTopic task.isSuccessful=${task.isSuccessful} ", null)
}
}
}
fun unsubscribeFromTopic(topic: String?) {
FirebaseMessaging.getInstance().unsubscribeFromTopic(topic!!)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
} else {
}
}
}
/**
* 有vpn有google商店才能获取
*/
private fun getToken() {
FirebaseMessaging.getInstance().token
.addOnCompleteListener(object : OnCompleteListener<String> {
override fun onComplete(task: Task<String>) {
LogEx.logDebug("FCM", "onComplete ${task.isSuccessful}")
if (!task.isSuccessful) {
Log.e("FCM", "Fetching FCM registration token failed", task.exception)
return
}
// Get new FCM registration token
val token: String = task.result
LogEx.logDebug("FCM", "token=$token")
val json = JSONObject()
json.put("token", token)
event("fcm_message_received", ext = json)
AppPreferences.getInstance().put("token", token)
// Handle new token
LogEx.logDebug("FCM", "FCM Registration Token: $token")
}
})
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment