Commit 10ca7fbd authored by wanglei's avatar wanglei

同步SmartClean通知部分的修改

parent 3921e2f4
Pipeline #1127 canceled with stages
...@@ -166,7 +166,13 @@ ...@@ -166,7 +166,13 @@
</intent-filter> </intent-filter>
</receiver> </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 <service
android:name=".notificationclean.AppNotificationListenerService" android:name=".notificationclean.AppNotificationListenerService"
......
...@@ -131,7 +131,7 @@ object NotificationHelper { ...@@ -131,7 +131,7 @@ object NotificationHelper {
* @param extra 额外参数 * @param extra 额外参数
*/ */
@SuppressLint("RemoteViewLayout") @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) val remoteViews = RemoteViews(packageName, R.layout.notification_common_notify)
when (actionId) { when (actionId) {
ID_JUNK_CLEAN_PUSH -> { ID_JUNK_CLEAN_PUSH -> {
...@@ -281,7 +281,7 @@ object NotificationHelper { ...@@ -281,7 +281,7 @@ object NotificationHelper {
val requestCode = Random.nextInt(0, 1000) val requestCode = Random.nextInt(0, 1000)
val pendingIntent = PendingIntent.getActivity(applicationContext, requestCode, intent, FLAG_UPDATE_CURRENT or FLAG_MUTABLE) 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() { ...@@ -31,10 +31,10 @@ class NotificationTimerTask() : TimerTask() {
var extra: Int? = null var extra: Int? = null
if (id == ID_PHONE_ACCELERATE) { if (id == ID_PHONE_ACCELERATE) {
extra = RamMemoryEx.getMemoryUsage(BaseApplication.context).toInt() 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 { ...@@ -40,7 +40,7 @@ object NotificationUtils {
remoteViews: RemoteViews, remoteViews: RemoteViews,
closeViewId: Int, closeViewId: Int,
pendingIntent: PendingIntent, pendingIntent: PendingIntent,
delay: Long pushStayTime: Long
) { ) {
if (closeViewId != 0) { if (closeViewId != 0) {
val closeIntent = Intent(context, CloseNotificationReceiver::class.java).apply { val closeIntent = Intent(context, CloseNotificationReceiver::class.java).apply {
...@@ -50,7 +50,7 @@ object NotificationUtils { ...@@ -50,7 +50,7 @@ object NotificationUtils {
PendingIntent.getBroadcast(context, 0, closeIntent, PendingIntent.FLAG_MUTABLE) PendingIntent.getBroadcast(context, 0, closeIntent, PendingIntent.FLAG_MUTABLE)
remoteViews.setOnClickPendingIntent(closeViewId, closePendingIntent) remoteViews.setOnClickPendingIntent(closeViewId, closePendingIntent)
} }
val builder = createNotificationBuilder(context, remoteViews, pendingIntent, delay) val builder = createNotificationBuilder(context, remoteViews, pendingIntent, pushStayTime)
sendNotification(context, notificationId, builder) sendNotification(context, notificationId, builder)
} }
...@@ -58,7 +58,7 @@ object NotificationUtils { ...@@ -58,7 +58,7 @@ object NotificationUtils {
context: Context, context: Context,
remoteViews: RemoteViews, remoteViews: RemoteViews,
pendingIntent: PendingIntent, pendingIntent: PendingIntent,
delay: Long pushStayTime: Long
): NotificationCompat.Builder { ): NotificationCompat.Builder {
val notificationManager = val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
...@@ -90,7 +90,7 @@ object NotificationUtils { ...@@ -90,7 +90,7 @@ object NotificationUtils {
.setStyle(NotificationCompat.BigTextStyle()) .setStyle(NotificationCompat.BigTextStyle())
.setFullScreenIntent(pendingIntent, true) .setFullScreenIntent(pendingIntent, true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setTimeoutAfter(delay) .setTimeoutAfter(pushStayTime)
.setSound(null) .setSound(null)
.setLights(0, 0, 0) .setLights(0, 0, 0)
.setVibrate(null) .setVibrate(null)
...@@ -187,7 +187,7 @@ object NotificationUtils { ...@@ -187,7 +187,7 @@ object NotificationUtils {
return notification return notification
} }
fun sendTimerPush(s: Long) { fun sendTimerPush(pushStayTime: Long) {
Log.d(TAG, "sendTimerPush") Log.d(TAG, "sendTimerPush")
val id = NotificationHelper.getPresentPushId() val id = NotificationHelper.getPresentPushId()
val isPush = PushStrategy.isPush(id) val isPush = PushStrategy.isPush(id)
...@@ -199,13 +199,13 @@ object NotificationUtils { ...@@ -199,13 +199,13 @@ object NotificationUtils {
if (id == ID_PHONE_ACCELERATE) { if (id == ID_PHONE_ACCELERATE) {
extra = RamMemoryEx.getMemoryUsage(BaseApplication.context).toInt() 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 { fun isNotificationExist(context: Context, notificationId: Int): Boolean {
// 获取 NotificationManagerCompat 实例 // 获取 NotificationManagerCompat 实例
val notificationManager = NotificationManagerCompat.from(context!!) val notificationManager = NotificationManagerCompat.from(context)
// 获取当前活动的通知列表 // 获取当前活动的通知列表
val notifications: MutableList<StatusBarNotification> = notificationManager.activeNotifications val notifications: MutableList<StatusBarNotification> = notificationManager.activeNotifications
......
package com.test.basd.onecleanmaster.display.fcm 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.content.Intent
import android.os.Build import android.os.Build
import android.util.Log import android.util.Log
...@@ -13,20 +17,20 @@ import com.test.basd.onecleanmaster.helps.EventHelper ...@@ -13,20 +17,20 @@ import com.test.basd.onecleanmaster.helps.EventHelper
class MyFirebaseMessagingService : FirebaseMessagingService() { class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) { override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.d(TAG, "onMessageReceived: " + remoteMessage.from) Log.d(TAG, "onMessageReceived: " + remoteMessage.from)
val isBoolean = NotificationUtils.isNotificationExist(this, 1) val pushStayTime = remoteMessage.data["push_stay_time"]?.toLongOrNull() ?: 0
val s = remoteMessage?.data?.get("push_stay_time")?.toLongOrNull() ?: 0 EventHelper.event("fcm_message_received",pushStayTime.toString())
EventHelper.event("fcm_message_received", s.toString()) NotificationUtils.sendTimerPush(pushStayTime)
NotificationUtils.sendTimerPush(s) initJob()
if (isBoolean) { }
Log.d(TAG, "常驻通知栏存在");
return private fun initJob() {
} val jobScheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
val intent = Intent(this, NotificationService::class.java) val job = JobInfo.Builder(0, ComponentName(this, NotificationJobService::class.java))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { .setMinimumLatency(0L)
startForegroundService(intent) .setOverrideDeadline(0L)
} else { .setPersisted(true)
startService(intent) .build()
} jobScheduler.schedule(job)
} }
override fun onNewToken(token: String) { override fun onNewToken(token: String) {
...@@ -38,3 +42,4 @@ class MyFirebaseMessagingService : FirebaseMessagingService() { ...@@ -38,3 +42,4 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
private const val TAG = "MyFirebaseMsgService" 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