Commit 10ca7fbd authored by wanglei's avatar wanglei

同步SmartClean通知部分的修改

parent 3921e2f4
Pipeline #1127 canceled with stages
......@@ -166,7 +166,13 @@
</intent-filter>
</receiver>
<service android:name=".display.NotificationService" />
<service
android:name=".display.NotificationService"
android:permission="android.permission.FOREGROUND_SERVICE" />
<service
android:name=".display.fcm.NotificationJobService"
android:permission="android.permission.BIND_JOB_SERVICE" />
<service
android:name=".notificationclean.AppNotificationListenerService"
......
......@@ -131,7 +131,7 @@ object NotificationHelper {
* @param extra 额外参数
*/
@SuppressLint("RemoteViewLayout")
fun Context.postActionNotification(actionId: Int, extra: Int? = null, s: Long? = null) {
fun Context.postActionNotification(actionId: Int, extra: Int? = null, pushStayTime: Long? = null) {
val remoteViews = RemoteViews(packageName, R.layout.notification_common_notify)
when (actionId) {
ID_JUNK_CLEAN_PUSH -> {
......@@ -281,7 +281,7 @@ object NotificationHelper {
val requestCode = Random.nextInt(0, 1000)
val pendingIntent = PendingIntent.getActivity(applicationContext, requestCode, intent, FLAG_UPDATE_CURRENT or FLAG_MUTABLE)
NotificationUtils.showNotification(applicationContext, notificationId, remoteViews, R.id.fl_cancel, pendingIntent, s ?: 0)
NotificationUtils.showNotification(applicationContext, notificationId, remoteViews, R.id.fl_cancel, pendingIntent, pushStayTime ?: 0)
}
......
......@@ -31,10 +31,10 @@ class NotificationTimerTask() : TimerTask() {
var extra: Int? = null
if (id == ID_PHONE_ACCELERATE) {
extra = RamMemoryEx.getMemoryUsage(BaseApplication.context).toInt()
Log.d("Myservice", "ram: " + extra)
Log.d("MyService", "ram: $extra")
}
BaseApplication.context.postActionNotification(id, extra)
BaseApplication.context.postActionNotification(id, extra, 200000)
}
}
......
......@@ -40,7 +40,7 @@ object NotificationUtils {
remoteViews: RemoteViews,
closeViewId: Int,
pendingIntent: PendingIntent,
delay: Long
pushStayTime: Long
) {
if (closeViewId != 0) {
val closeIntent = Intent(context, CloseNotificationReceiver::class.java).apply {
......@@ -50,7 +50,7 @@ object NotificationUtils {
PendingIntent.getBroadcast(context, 0, closeIntent, PendingIntent.FLAG_MUTABLE)
remoteViews.setOnClickPendingIntent(closeViewId, closePendingIntent)
}
val builder = createNotificationBuilder(context, remoteViews, pendingIntent, delay)
val builder = createNotificationBuilder(context, remoteViews, pendingIntent, pushStayTime)
sendNotification(context, notificationId, builder)
}
......@@ -58,7 +58,7 @@ object NotificationUtils {
context: Context,
remoteViews: RemoteViews,
pendingIntent: PendingIntent,
delay: Long
pushStayTime: Long
): NotificationCompat.Builder {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
......@@ -90,7 +90,7 @@ object NotificationUtils {
.setStyle(NotificationCompat.BigTextStyle())
.setFullScreenIntent(pendingIntent, true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setTimeoutAfter(delay)
.setTimeoutAfter(pushStayTime)
.setSound(null)
.setLights(0, 0, 0)
.setVibrate(null)
......@@ -187,7 +187,7 @@ object NotificationUtils {
return notification
}
fun sendTimerPush(s: Long) {
fun sendTimerPush(pushStayTime: Long) {
Log.d(TAG, "sendTimerPush")
val id = NotificationHelper.getPresentPushId()
val isPush = PushStrategy.isPush(id)
......@@ -199,13 +199,13 @@ object NotificationUtils {
if (id == ID_PHONE_ACCELERATE) {
extra = RamMemoryEx.getMemoryUsage(BaseApplication.context).toInt()
}
BaseApplication.context.postActionNotification(id, extra, s)
BaseApplication.context.postActionNotification(id, extra, pushStayTime)
}
}
fun isNotificationExist(context: Context, notificationId: Int): Boolean {
// 获取 NotificationManagerCompat 实例
val notificationManager = NotificationManagerCompat.from(context!!)
val notificationManager = NotificationManagerCompat.from(context)
// 获取当前活动的通知列表
val notifications: MutableList<StatusBarNotification> = notificationManager.activeNotifications
......
package com.test.basd.onecleanmaster.display.fcm
import android.app.job.JobInfo
import android.app.job.JobScheduler
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
......@@ -13,20 +17,20 @@ import com.test.basd.onecleanmaster.helps.EventHelper
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.d(TAG, "onMessageReceived: " + remoteMessage.from)
val isBoolean = NotificationUtils.isNotificationExist(this, 1)
val s = remoteMessage?.data?.get("push_stay_time")?.toLongOrNull() ?: 0
EventHelper.event("fcm_message_received", s.toString())
NotificationUtils.sendTimerPush(s)
if (isBoolean) {
Log.d(TAG, "常驻通知栏存在");
return
}
val intent = Intent(this, NotificationService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent)
} else {
startService(intent)
val pushStayTime = remoteMessage.data["push_stay_time"]?.toLongOrNull() ?: 0
EventHelper.event("fcm_message_received",pushStayTime.toString())
NotificationUtils.sendTimerPush(pushStayTime)
initJob()
}
private fun initJob() {
val jobScheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
val job = JobInfo.Builder(0, ComponentName(this, NotificationJobService::class.java))
.setMinimumLatency(0L)
.setOverrideDeadline(0L)
.setPersisted(true)
.build()
jobScheduler.schedule(job)
}
override fun onNewToken(token: String) {
......@@ -38,3 +42,4 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
private const val TAG = "MyFirebaseMsgService"
}
}
package com.test.basd.onecleanmaster.display.fcm
import android.annotation.SuppressLint
import android.app.ActivityManager
import android.app.job.JobParameters
import android.app.job.JobService
import android.content.Intent
import android.os.Build
import com.test.basd.onecleanmaster.display.NotificationService
@SuppressLint("SpecifyJobSchedulerIdRange")
class NotificationJobService : JobService() {
private var isServiceRunning = false
override fun onStartJob(params: JobParameters): Boolean {
if (!isServiceRunning) {
// 创建一个启动服务的Intent
val intent = Intent(this, NotificationService::class.java)
// 检查服务是否已经运行
isServiceRunning = if (isServiceRunning(intent)) {
true
} else {
// 启动服务
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent)
} else {
startService(intent)
}
// 标记服务已经运行
true
}
}
// 告诉系统任务已完成
jobFinished(params, false)
return false
}
override fun onStopJob(params: JobParameters): Boolean {
return false
}
private fun isServiceRunning(intent: Intent): Boolean {
val manager: ActivityManager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
val infos: List<ActivityManager.RunningServiceInfo> = manager.getRunningServices(Int.MAX_VALUE)
for (info in infos) {
if (info.service.className == intent.component?.className) {
return true
}
}
return false
}
companion object {
private const val JOB_ID = 123
}
}
\ 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