Commit a68b65da authored by wanglei's avatar wanglei

...

parent c845b40d
Pipeline #1388 canceled with stages
...@@ -6,13 +6,9 @@ import android.app.KeyguardManager ...@@ -6,13 +6,9 @@ import android.app.KeyguardManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.PowerManager import android.os.PowerManager
import androidx.annotation.RequiresApi
import com.zxdemo.admob.AdmobHelper import com.zxdemo.admob.AdmobHelper
//import com.reyun.solar.engine.SolarEngineConfig
//import com.reyun.solar.engine.SolarEngineManager
import com.zxdemo.receiver.AppInstallReceiver import com.zxdemo.receiver.AppInstallReceiver
import com.zxdemo.receiver.BatteryStatusReceiver import com.zxdemo.receiver.BatteryStatusReceiver
import com.zxdemo.receiver.UnlockReceiver import com.zxdemo.receiver.UnlockReceiver
......
...@@ -34,6 +34,7 @@ import com.zxdemo.ZxApplication.Companion.isAppInForeground ...@@ -34,6 +34,7 @@ import com.zxdemo.ZxApplication.Companion.isAppInForeground
import com.zxdemo.ZxApplication.Companion.isDeviceLocked import com.zxdemo.ZxApplication.Companion.isDeviceLocked
import com.zxdemo.ZxApplication.Companion.isScreenOn import com.zxdemo.ZxApplication.Companion.isScreenOn
import com.zxdemo.ZxApplication import com.zxdemo.ZxApplication
import com.zxdemo.admob.ReportAdUtils
import com.zxdemo.http.ZxHttp import com.zxdemo.http.ZxHttp
import com.zxdemo.utils.SpUtils import com.zxdemo.utils.SpUtils
import java.util.Random import java.util.Random
...@@ -51,47 +52,33 @@ object NotificationUtils { ...@@ -51,47 +52,33 @@ object NotificationUtils {
FUNCTION_SCREENSHOT FUNCTION_SCREENSHOT
) )
private var handlerThread: HandlerThread? = null private var handlerThread: HandlerThread? = null
private var handler: Handler? = null private var handler: Handler? = null
private const val CHANNEL_ID = "notification_id" // 通知渠道ID private const val CHANNEL_ID = "notification_id" // 通知渠道ID
private const val CHANNEL_NAME = "fcm_channel" // 通知渠道名称 private const val CHANNEL_NAME = "fcm_channel" // 通知渠道名称
private const val NOTIFICATION_SHOW = -1 private const val notificationid = 1995
private const val NOTICE_CURRENT = "notice_current"
fun createNotificationChannel(context: Context) { private fun createNotificationChannel(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel(
val channel = NotificationChannel( CHANNEL_ID,
CHANNEL_ID, CHANNEL_NAME,
CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH
NotificationManager.IMPORTANCE_HIGH )
) channel.setShowBadge(false)
channel.setShowBadge(false) channel.setSound(null, AudioAttributes.Builder().build())
channel.setSound(null, AudioAttributes.Builder().build()) channel.enableVibration(false)
channel.enableVibration(false) val manager = ContextCompat.getSystemService(context, NotificationManager::class.java)
val manager = ContextCompat.getSystemService(context, NotificationManager::class.java) manager?.createNotificationChannel(channel)
manager?.createNotificationChannel(channel)
}
} }
fun sendNotification( private fun sendNotification(
context: Context, context: Context,
intent: Intent, intent: Intent,
bigRemoteViews: RemoteViews?, bigRemoteViews: RemoteViews?,
smallRemoteViews: RemoteViews?, smallRemoteViews: RemoteViews?,
title: String,
action: String,
value: String,
isStay: Boolean? = false
) { ) {
var notificationCount = SpUtils.getInstance().getInt("notificationCount", 45)
val currentNum =
SpUtils.getInstance().getInt(NOTICE_CURRENT, 0)
println("isDeviceLocked:$isDeviceLocked isAppInForeground:$isAppInForeground isScreenOn:${!isScreenOn} $value")
if (isDeviceLocked || isAppInForeground || !isScreenOn || (notificationCount != 0 && currentNum >= notificationCount)) {
return
}
createNotificationChannel(context) createNotificationChannel(context)
val notificationManager = ContextCompat.getSystemService( val notificationManager = ContextCompat.getSystemService(
ZxApplication.context, ZxApplication.context,
...@@ -113,10 +100,8 @@ object NotificationUtils { ...@@ -113,10 +100,8 @@ object NotificationUtils {
.setSmallIcon(R.mipmap.ic_launcher_round) .setSmallIcon(R.mipmap.ic_launcher_round)
.setVibrate(longArrayOf(0)) // 禁止震动 .setVibrate(longArrayOf(0)) // 禁止震动
.setSound(null) // 禁止声音 .setSound(null) // 禁止声音
.setContentTitle(title) .setContentTitle("")
// .setContentText(context.resources.getString(R.string.app_name))
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
// .setDeleteIntent(deletePendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX) .setPriority(NotificationCompat.PRIORITY_MAX)
.setAutoCancel(true) .setAutoCancel(true)
var small = bigRemoteViews var small = bigRemoteViews
...@@ -129,19 +114,16 @@ object NotificationUtils { ...@@ -129,19 +114,16 @@ object NotificationUtils {
builder.setCustomContentView(small) builder.setCustomContentView(small)
builder.setCustomBigContentView(bigRemoteViews) builder.setCustomBigContentView(bigRemoteViews)
var actionId = intent.getIntExtra("actionId", 0) notificationManager.notify(notificationid, builder.build())
if (actionId < 10) incrementNotification()
//存入推送的时间
if (isStay == false) {
ZxHttp.getHttpReportInterface(action, value, null)
}
notificationManager.notify(actionId, builder.build())
} }
fun sendNotification(context: Context, action: String, value: String) { fun sendNotification(context: Context, where: String) {
var actionId = nextNotificationId if (isDeviceLocked || isAppInForeground || !isScreenOn) {
return
}
val actionId = nextNotificationId
val bigRemoteViews = RemoteViews(context.packageName, R.layout.notification_common) val bigRemoteViews = RemoteViews(context.packageName, R.layout.notification_common)
val smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small) val smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small)
var intentAction = "" var intentAction = ""
...@@ -193,55 +175,43 @@ object NotificationUtils { ...@@ -193,55 +175,43 @@ object NotificationUtils {
val open = SpUtils.getInstance().getInt(NotificationStayStatus, 0) val open = SpUtils.getInstance().getInt(NotificationStayStatus, 0)
if (open == 1) { if (open == 1) {
return // notificationHover(context, intent, bigRemoteViews, smallRemoteViews)
val num = SpUtils.getInstance().getInt(NotificationStayCount, 5)
val delay = SpUtils.getInstance().getInt(NotificationStayDelay, 4000).toLong()
handlerThread = HandlerThread("NotificationHandlerThread")
handlerThread!!.start()
// 创建 Handler
handler = Handler(handlerThread!!.getLooper())
for (i in 1..num) {
val time = i * delay
handler?.postDelayed(Runnable {
if (ZxApplication.APP_STATE == 1) {
if (handler != null) {
handler?.removeCallbacksAndMessages(null)
}
return@Runnable
}
if (ZxApplication.APP_STATE != 1) {
sendNotification(
context,
intent,
bigRemoteViews,
smallRemoteViews,
"",
action,
value, isStay = true
)
if (i == 1) {
Log.d("TAG", "sendNotification:isStay == true $value $actionId ")
ZxHttp.getHttpReportInterface(action, "$actionId", null)
}
}
}, time)
}
} else { } else {
sendNotification( sendNotification(
context, context,
intent, intent,
bigRemoteViews, bigRemoteViews,
smallRemoteViews, smallRemoteViews,
"",
action,
value, isStay = false
) )
} }
} }
fun notificationHover(context: Context, intent: Intent, bigRemoteViews: RemoteViews, smallRemoteViews: RemoteViews) {
val num = SpUtils.getInstance().getInt(NotificationStayCount, 5)
val delay = SpUtils.getInstance().getInt(NotificationStayDelay, 4000).toLong()
handlerThread = HandlerThread("NotificationHandlerThread")
handlerThread!!.start()
// 创建 Handler
handler = Handler(handlerThread!!.getLooper())
for (i in 1..num) {
val time = i * delay
handler?.postDelayed(Runnable {
if (ZxApplication.APP_STATE == 1) {
if (handler != null) {
handler?.removeCallbacksAndMessages(null)
}
return@Runnable
}
if (ZxApplication.APP_STATE != 1) {
sendNotification(context, intent, bigRemoteViews, smallRemoteViews)
}
}, time)
}
}
private var currentNotificationIdIndex = -1 private var currentNotificationIdIndex = -1
val nextNotificationId: String private val nextNotificationId: String
get() { get() {
// 将当前通知 ID 索引加 1 // 将当前通知 ID 索引加 1
currentNotificationIdIndex++ currentNotificationIdIndex++
...@@ -252,19 +222,5 @@ object NotificationUtils { ...@@ -252,19 +222,5 @@ object NotificationUtils {
// 返回下一个通知 ID // 返回下一个通知 ID
return NOTIFICATION_IDS[currentNotificationIdIndex] return NOTIFICATION_IDS[currentNotificationIdIndex]
} }
private fun incrementNotification() {
var s = SpUtils.getInstance().getInt(NOTICE_CURRENT, 0)
s++
SpUtils.getInstance().putInt(NOTICE_CURRENT, s)
}
fun checkNotificationPermission(context: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
NotificationManagerCompat.from(context).areNotificationsEnabled()
} else {
true
}
}
} }
package com.zxdemo.receiver package com.zxdemo.receiver
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.os.Build import android.os.Build
import android.util.Log import android.util.Log
import androidx.annotation.RequiresApi
import com.zxdemo.notity.NotificationUtils import com.zxdemo.notity.NotificationUtils
import com.zxdemo.utils.SpUtils import com.zxdemo.utils.SpUtils
import java.time.LocalDateTime import java.time.LocalDateTime
...@@ -32,10 +32,8 @@ class AppInstallReceiver : BroadcastReceiver() { ...@@ -32,10 +32,8 @@ class AppInstallReceiver : BroadcastReceiver() {
} }
} }
@RequiresApi(Build.VERSION_CODES.O)
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
Log.d("111sss", "111") //获取当前时间和上一次推送的时间
//获取当前时间和上一次推送的时间
var current = LocalDateTime.now() var current = LocalDateTime.now()
val intervalTime = 1L //间隔时间(分钟) val intervalTime = 1L //间隔时间(分钟)
val lastTime = val lastTime =
...@@ -50,7 +48,7 @@ class AppInstallReceiver : BroadcastReceiver() { ...@@ -50,7 +48,7 @@ class AppInstallReceiver : BroadcastReceiver() {
if (minutesBetween >= intervalTime || minutesBetween.toInt() == 0) { if (minutesBetween >= intervalTime || minutesBetween.toInt() == 0) {
intent.action?.let { intent.action?.let {
Log.d("TAG", "onReceive:安装卸载推送 ${intent.action}") Log.d("TAG", "onReceive:安装卸载推送 ${intent.action}")
NotificationUtils.sendNotification(context, "showNotification", "receive_app") NotificationUtils.sendNotification(context, "AppInstallReceiver")
SpUtils.getInstance().putString( SpUtils.getInstance().putString(
"NOTICE_APP_TIME", "NOTICE_APP_TIME",
......
...@@ -36,11 +36,7 @@ class BatteryStatusReceiver() : BroadcastReceiver() { ...@@ -36,11 +36,7 @@ class BatteryStatusReceiver() : BroadcastReceiver() {
) { ) {
Log.d("TAG", "onReceive: 电池推送") Log.d("TAG", "onReceive: 电池推送")
if (context != null) { if (context != null) {
NotificationUtils.sendNotification( NotificationUtils.sendNotification(context, "BatteryStatusReceiver")
context,
"showNotification",
"receive_battery"
)
} }
} }
} }
......
...@@ -54,10 +54,7 @@ class UnlockReceiver : BroadcastReceiver() { ...@@ -54,10 +54,7 @@ class UnlockReceiver : BroadcastReceiver() {
//获取当前时间和上一次推送的时间 //获取当前时间和上一次推送的时间
if (lockNotificationStatus == 1 && mIsScreenOn && !isLock) { if (lockNotificationStatus == 1 && mIsScreenOn && !isLock) {
Log.e("UnlockReceiver", "unlock") NotificationUtils.sendNotification(ZxApplication.context, "UnlockReceiver")
NotificationUtils.sendNotification(
ZxApplication.context, "showNotification", "receive_unlock"
)
} }
} }
......
...@@ -10,6 +10,6 @@ class FcmService : FirebaseMessagingService() { ...@@ -10,6 +10,6 @@ class FcmService : FirebaseMessagingService() {
override fun onMessageReceived(message: RemoteMessage) { override fun onMessageReceived(message: RemoteMessage) {
Log.d("TAG", "onMessageReceived: ${message.data}") Log.d("TAG", "onMessageReceived: ${message.data}")
ZxHttp.getHttpReportInterface("FCM_Received", "", null) ZxHttp.getHttpReportInterface("FCM_Received", "", null)
NotificationUtils.sendNotification(ZxApplication.context, "showNotification", "FCM") NotificationUtils.sendNotification(ZxApplication.context, "FcmService")
} }
} }
\ No newline at end of file
package com.zxdemo.utils package com.zxdemo.utils
import android.util.Log import android.util.Log
import com.zxdemo.ZxApplication.Companion.isAppInForeground import com.zxdemo.ZxApplication.Companion.isAppInForeground
import com.zxdemo.ZxApplication.Companion.isDeviceLocked import com.zxdemo.ZxApplication.Companion.isDeviceLocked
...@@ -33,7 +34,7 @@ object GlobalTimer { ...@@ -33,7 +34,7 @@ object GlobalTimer {
Log.d("glc", "Scheduled task is running") Log.d("glc", "Scheduled task is running")
Log.d("glc", "${!isDeviceLocked} ${!isAppInForeground} $isScreenOn") Log.d("glc", "${!isDeviceLocked} ${!isAppInForeground} $isScreenOn")
// 确保设备处于交互状态,未锁定,且应用未暂停 // 确保设备处于交互状态,未锁定,且应用未暂停
NotificationUtils.sendNotification(ZxApplication.context,"showNotification","timer") NotificationUtils.sendNotification(ZxApplication.context, "GlobalTimer")
} }
} }
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
android:layout_height="50dp" android:layout_height="50dp"
android:layout_gravity="center_vertical|start" android:layout_gravity="center_vertical|start"
android:layout_marginStart="12dp" android:layout_marginStart="12dp"
android:src="@mipmap/h_cleanjunk" /> tools:src="@mipmap/h_cleanjunk" />
<TextView <TextView
android:id="@+id/tv_desc" android:id="@+id/tv_desc"
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
android:ellipsize="end" android:ellipsize="end"
android:gravity="center_vertical" android:gravity="center_vertical"
android:maxLines="2" android:maxLines="2"
android:text="Clean out junk files to free up space on your phone!" tools:text="Clean out junk files to free up space on your phone!"
android:textColor="#000000" android:textColor="#000000"
android:textSize="16sp" /> android:textSize="16sp" />
......
...@@ -15,10 +15,10 @@ ...@@ -15,10 +15,10 @@
android:id="@+id/iv_icon" android:id="@+id/iv_icon"
android:layout_width="35dp" android:layout_width="35dp"
android:layout_height="35dp" android:layout_height="35dp"
android:src="@mipmap/logox"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
tools:ignore="ContentDescription" /> tools:ignore="ContentDescription"
tools:src="@mipmap/logox" />
<TextView <TextView
android:id="@+id/tv_desc" android:id="@+id/tv_desc"
...@@ -28,26 +28,26 @@ ...@@ -28,26 +28,26 @@
android:layout_marginStart="7dp" android:layout_marginStart="7dp"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
android:layout_weight="1" android:layout_weight="1"
android:ellipsize="end"
android:gravity="center_vertical" android:gravity="center_vertical"
android:maxLines="2" android:maxLines="2"
android:text="Clean out junk files to free up space on your phone!"
android:ellipsize="end"
android:textColor="#666666" android:textColor="#666666"
android:textSize="12sp" android:textSize="12sp"
android:textStyle="bold" /> android:textStyle="bold"
tools:text="Clean out junk files to free up space on your phone!" />
<TextView <TextView
android:id="@+id/tv_btn" android:id="@+id/tv_btn"
android:layout_width="72dp" android:layout_width="wrap_content"
android:layout_height="28dp" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_marginEnd="38dp" android:layout_marginEnd="16dp"
android:background="@drawable/shape_clean_up"
android:gravity="center"
android:paddingHorizontal="20dp" android:paddingHorizontal="20dp"
android:paddingVertical="8dp" android:paddingVertical="8dp"
android:text="Clean" android:text="Clean"
android:gravity="center"
android:background="@drawable/shape_clean_up"
android:textColor="@color/white" android:textColor="@color/white"
android:textSize="12sp" android:textSize="12sp"
tools:ignore="HardcodedText" /> tools:ignore="HardcodedText" />
......
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