Commit a3745b91 authored by wanglei's avatar wanglei

...通知

parent e8ade6bb
......@@ -209,14 +209,14 @@ class MyApplication : Application() {
private fun initConfig(config: String? = AppPreferences.getInstance().getString("config", "")) {
// kotlin.runCatching {
// val configBean = Gson().fromJson(config, ConfigBean::class.java)
// val configBean = Gson().fromJson(config, com.base.scanqr.bean.config.ConfigBean::class.java)
//
// val jsonObject = JSONObject()
// jsonObject.put("ut", configBean.ut)
// EventUtils.event("user_type", ext = jsonObject)
//
// //配置
// ConfigBean.configBean = configBean
// com.base.scanqr.bean.config.ConfigBean.configBean = configBean
//
// //广告
// com.base.scanqr.ads.AdsMgr.adsConfigBean = configBean.adConfigBean
......
......@@ -7,7 +7,6 @@ import androidx.annotation.LayoutRes
import com.applovin.sdk.AppLovinMediationProvider
import com.applovin.sdk.AppLovinSdk
import com.applovin.sdk.AppLovinSdkInitializationConfiguration
import com.base.appzxhy.bean.config.ConfigBean.Companion.configBean
import com.base.scanqr.BuildConfig
import com.base.scanqr.GlobalConfig
import com.base.scanqr.ads.admob.AdBannerMgr
......@@ -20,6 +19,7 @@ import com.base.scanqr.ads.applovin.MaxInsertMgr
import com.base.scanqr.ads.applovin.MaxNativeMgr
import com.base.scanqr.ads.applovin.MaxOpenMgr
import com.base.scanqr.bean.config.AdConfigBean
import com.base.scanqr.bean.config.ConfigBean.Companion.configBean
import com.base.scanqr.helper.EventUtils
import com.base.scanqr.utils.AppPreferences
import com.base.scanqr.utils.LogEx
......@@ -240,7 +240,7 @@ object AdsMgr {
}
}
fun isNativeShow() = (isAdmobInit && !configBean.isInBlackList) && LimitUtils.isAdShow(AdsType.NATIVE, null)
fun isNativeShow() = LimitUtils.isAdShow(AdsType.NATIVE, null)
/**
* 展示banner广告
......
package com.base.scanqr.bean
import android.content.Context
class NotificationSendBean(
val context: Context,
val actionId: String = "",
val where: String = "",
val canSend: () -> Boolean = { true },//是否可以发送
var sendSuccess: (() -> Unit)? = null,//发送成功回调
var valueMap: HashMap<String, Any> = hashMapOf()//发送通知可能携带的参数
) {
companion object {
const val POPUP_WHERE_TIMBER = "Timer"
const val POPUP_WHERE_LOCK = "Lock"
const val POPUP_WHERE_FCM = "fcm"
const val POPUP_WHERE_ALARM = "Alarm"
const val POPUP_WHERE_BATTERY = "battery"
const val POPUP_WHERE_PACKAGE = "package"
const val POPUP_WHERE_WORK_MANAGER = "workManager"
const val POPUP_WHERE_FILE_JOB = "FileJob"
}
}
\ No newline at end of file
......@@ -9,4 +9,11 @@ class AdConfigBean(
var openAdLoading: Int = 15,
var functionBackShowAd: Boolean = true,
var functionInShowAd: Boolean = true,
)
\ No newline at end of file
) {
companion object {
/**
* 广告配置项目
*/
var adsConfigBean: AdConfigBean = AdConfigBean()
}
}
\ No newline at end of file
package com.base.appzxhy.bean.config
import com.base.scanqr.bean.config.AdConfigBean
import com.base.scanqr.bean.config.PopupConfigBean
package com.base.scanqr.bean.config
/**
* 后台配置
......
......@@ -33,4 +33,9 @@ class PopupConfigBean(
var packageS: Boolean = true,
var popupPackageCount: Int = 20,
var popupPackageInterval: Int = 1
)
\ No newline at end of file
) {
companion object {
var popupConfigBean: PopupConfigBean = PopupConfigBean()
}
}
\ No newline at end of file
package com.base.scanqr.fcm.notification
import android.os.Handler
import android.os.HandlerThread
import com.base.scanqr.MyApplication
import com.base.scanqr.bean.NotificationSendBean
import com.base.scanqr.bean.config.PopupConfigBean.Companion.popupConfigBean
/**
* 悬停通知
*/
object NotificationHoverUtils {
private val TAG = "NotificationHoverUtils"
private var handlerThread: HandlerThread? = null
private var handler: Handler? = null
/**
* 发送悬停通知
*/
fun sendHoverNotification(sendBean: NotificationSendBean) {
val hoverStatus = popupConfigBean.popupHoverStatus
val hoverCount = popupConfigBean.popupHoverCount
val hoverDelay = popupConfigBean.popupHoverDelay.toLong()
if (!hoverStatus) return
if (handlerThread == null) {
handlerThread = HandlerThread("NotificationHandlerThread")
handlerThread?.start()
}
// 创建 Handler
if (handler == null) {
handlerThread?.let {
handler = Handler(it.getLooper())
}
}
for (i in 1..hoverCount) {
val time = i * hoverDelay
handler?.postDelayed(Runnable {
// LogEx.logDebug(TAG, "handler ${MyApplication.PAUSED_VALUE}")
if (MyApplication.PAUSED_VALUE == 1) {
handler?.removeCallbacksAndMessages(null)
} else {
NotificationUiUtil.setNotification(sendBean)
}
}, time)
}
}
fun stopNotificationHandler() {
// 停止 HandlerThread
if (handler != null) {
handler?.removeCallbacksAndMessages(null)
}
if (handlerThread != null) {
handlerThread?.quit()
handlerThread = null
}
handler = null
}
}
\ No newline at end of file
package com.base.scanqr.fcm.notification
import android.content.Context
import com.base.scanqr.BuildConfig
import com.base.scanqr.bean.NotificationSendBean
import com.base.scanqr.bean.NotificationSendBean.Companion.POPUP_WHERE_TIMBER
import com.base.scanqr.bean.config.PopupConfigBean.Companion.popupConfigBean
import com.base.scanqr.helper.EventUtils
import com.base.scanqr.utils.AppPreferences
import com.base.scanqr.utils.KotlinExt.currentDate
import com.base.scanqr.utils.LogEx
import com.base.scanqr.utils.ToastUtils.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.async
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.util.Calendar
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.atomic.AtomicBoolean
/**
* 发送通知的管理类,方便测试限制条件
*/
object NotificationManager {
private val TAG = "NotificationManager"
private var sendBeanBlockingQueue = ArrayBlockingQueue<NotificationSendBean>(10)
fun submitSendBean(bean: NotificationSendBean) {
try {
sendBeanBlockingQueue.put(bean)
} catch (e: Exception) {
EventUtils.event("Notification_Error", "submitSendBean Exception")
LogEx.logDebug("canSendNotification", "submitSendBean Exception")
}
}
private var isStartSendQueue = AtomicBoolean(false)
private var isUnLimit: Boolean = if (BuildConfig.DEBUG) false else false
fun startNotificationQueue() {
if (isStartSendQueue.get()) return
isStartSendQueue.set(true)
MainScope().launch(Dispatchers.IO) {
while (isActive) {
val bean = sendBeanBlockingQueue.take()
//测试哪些位置触发
if (BuildConfig.DEBUG) {
if (!testWhere.contains(bean.where)) continue
}
LogEx.logDebug(TAG, "sendNotificationIfCan where=${bean.where}")
EventUtils.event("Notification_Popup_Start", "where=${bean.where}")
if (!isUnLimit) {
//总的限制条件
if (!canSendNotification(bean.context)) continue
//当条推送是否可以推送
if (!bean.canSend.invoke()) continue
}
async(Dispatchers.Main) {
NotificationUiUtil.setNotification(bean)
}.await()
//上报通知
EventUtils.event("Notification_Popup", "where=${bean.where} actionId=${bean.actionId}")
//当天次数加一
dayPopupCount += 1
//推送时间
lastPopupTime = System.currentTimeMillis()
//这条推送回调
bean.sendSuccess?.invoke()
async(Dispatchers.Main) {
//发送悬停
NotificationHoverUtils.sendHoverNotification(bean)
}.await()
}
isStartSendQueue.set(false)
}
}
//当天推送次数
private var dayPopupCount = 0
get() {
return AppPreferences.getInstance().getInt("dayPopupCount_${currentDate()}", field)
}
set(value) {
field = value
AppPreferences.getInstance().put("dayPopupCount_${currentDate()}", value, true)
}
//上次推送时间
private var lastPopupTime = 0L
get() {
return AppPreferences.getInstance().getLong("lastPopupTime", field)
}
set(value) {
field = value
AppPreferences.getInstance().put("lastPopupTime", value, true)
}
/**
* 总的限制条件
*/
private fun canSendNotification(context: Context): Boolean {
//是否开启推送
if (!popupConfigBean.popupStatus) {
EventUtils.event("Notification_Error", "status=${popupConfigBean.popupStatus}")
LogEx.logDebug("canSendNotification", "status")
if (BuildConfig.DEBUG) {
MainScope().launch(Dispatchers.Main) {
context.toast("配置关闭推送")
}
}
return false
}
//当天推送次数
val count = popupConfigBean.popupCount
if (dayPopupCount > count) {
LogEx.logDebug("canSendNotification", "count")
EventUtils.event("Notification_Error", "dayPopupCount=$dayPopupCount count=$count ")
return false
}
//判断是否在时间区域
val start = popupConfigBean.popupStart
val end = popupConfigBean.popupEnd
val calendar = Calendar.getInstance()
val currentHour = calendar.get(Calendar.HOUR_OF_DAY)
if (currentHour !in start until end) {
LogEx.logDebug("canSendNotification", "start-end currentHour=$currentHour start=$start end=$end")
EventUtils.event("Notification_Error", "start=$start end=$end currentHour=$currentHour")
return false
}
//总时间间隔,单位分钟
val interval = popupConfigBean.popupInterval
val passedTime = System.currentTimeMillis() - lastPopupTime
if (passedTime < interval * 60 * 1000L) {
EventUtils.event("Notification_Error", "interval=$interval passedTime=$passedTime")
LogEx.logDebug("canSendNotification", "interval=$interval passedTime=$passedTime")
return false
}
return true
}
/**
* 只测某些类型
*/
private var testWhere = listOf(
POPUP_WHERE_TIMBER
)
}
\ No newline at end of file
package com.base.scanqr.fcm.notification
import com.base.scanqr.bean.NotificationSendBean
/**
* 发送通知UI部分
*/
object NotificationUiUtil {
fun setNotification(sendBean: NotificationSendBean) {
}
}
\ No newline at end of file
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