diff --git a/notification/src/main/AndroidManifest.xml b/notification/src/main/AndroidManifest.xml index 61c9f54..db10d8c 100644 --- a/notification/src/main/AndroidManifest.xml +++ b/notification/src/main/AndroidManifest.xml @@ -6,11 +6,6 @@ - - - - - @@ -56,8 +51,7 @@ + android:exported="false" /> diff --git a/notification/src/main/java/com/remax/notification/service/NotificationKeepAliveService.kt b/notification/src/main/java/com/remax/notification/service/NotificationKeepAliveService.kt index e92fcfa..2fe54db 100644 --- a/notification/src/main/java/com/remax/notification/service/NotificationKeepAliveService.kt +++ b/notification/src/main/java/com/remax/notification/service/NotificationKeepAliveService.kt @@ -1,18 +1,14 @@ package com.remax.notification.service -import android.app.Notification import android.app.Service import android.content.Context import android.content.Intent -import android.os.Build import android.os.Handler import android.os.IBinder import android.os.Looper import com.remax.base.ext.KvLongDelegate -import com.remax.base.ext.canSendNotification import com.remax.base.report.DataReportManager import com.remax.notification.check.NotificationCheckController -import com.remax.notification.controller.NotificationTriggerController import com.remax.notification.controller.NotificationTriggerController.triggerFixTimeNotification import com.remax.notification.timing.NotificationTimingController import com.remax.notification.utils.NotiLogger @@ -25,8 +21,7 @@ import com.remax.notification.utils.NotiLogger class NotificationKeepAliveService : Service() { companion object { - private val NOTIFICATION_ID = NotificationTriggerController.getResidentNotificationId() - + // 默认15分钟 = 900秒 private const val DEFAULT_INTERVAL_SECONDS = 900L @@ -66,15 +61,11 @@ class NotificationKeepAliveService : Service() { } try { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - context.startForegroundService(intent) - } else { - context.startService(intent) - } - NotiLogger.d("启动保活服务,间隔: ${intervalSeconds}秒") + context.startService(intent) + NotiLogger.d("启动服务,间隔: ${intervalSeconds}秒") } catch (e: Exception) { DataReportManager.reportData("Notific_Show_Fail",mapOf("reason" to "alive_service_${e.message}")) - NotiLogger.e("启动保活服务失败", e) + NotiLogger.e("启动服务失败", e) } } @@ -128,15 +119,15 @@ class NotificationKeepAliveService : Service() { ACTION_START_SERVICE -> { intervalSeconds = intent.getLongExtra(EXTRA_INTERVAL_SECONDS, defaultIntervalSeconds) - startForegroundService() + startRunning() } ACTION_STOP_SERVICE -> { - stopForegroundService() + stopRunning() } ACTION_UPDATE_NOTIFICATION -> { - updateForegroundNotification() + updateRunningNotification() } } return START_STICKY // 服务被杀死后自动重启 @@ -146,48 +137,34 @@ class NotificationKeepAliveService : Service() { override fun onDestroy() { super.onDestroy() - stopForegroundService() + stopRunning() NotiLogger.d("保活服务销毁") } /** - * 启动前台服务 + * 启动服务 */ - private fun startForegroundService() { + private fun startRunning() { if (isServiceRunning) { - NotiLogger.d("保活服务已在运行中,刷新通知,间隔: ${intervalSeconds}秒") + NotiLogger.d("服务已在运行中,刷新通知,间隔: ${intervalSeconds}秒") // 服务已运行,只刷新通知 - updateForegroundNotification() + updateRunningNotification() triggerFixTimeNotification() return } - // 检查通知权限 - val hasNotificationPermission = canSendNotification() - NotiLogger.d("通知权限状态: $hasNotificationPermission") - - if (!hasNotificationPermission) { - NotiLogger.w("没有通知权限,前台服务通知可能不会显示") - } - isServiceRunning = true - // 创建前台通知 - val notification = createForegroundNotification() - - // 启动前台服务 - startForeground(NOTIFICATION_ID, notification) - // 启动定时任务 startKeepAliveTask() - NotiLogger.d("保活服务启动成功,间隔: ${intervalSeconds}秒") + NotiLogger.d("服务启动成功,间隔: ${intervalSeconds}秒") } /** - * 停止前台服务 + * 停止服务 */ - private fun stopForegroundService() { + private fun stopRunning() { if (!isServiceRunning) { return } @@ -197,16 +174,9 @@ class NotificationKeepAliveService : Service() { // 停止定时任务 stopKeepAliveTask() - // 停止前台服务 - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - stopForeground(STOP_FOREGROUND_REMOVE) - } else { - @Suppress("DEPRECATION") - stopForeground(true) - } stopSelf() - NotiLogger.d("保活服务停止") + NotiLogger.d("服务停止") } /** @@ -224,7 +194,7 @@ class NotificationKeepAliveService : Service() { try { NotiLogger.d("执行保活任务") DataReportManager.reportData("Notific_Pull", mapOf("topic" to "timer")) - updateForegroundNotification() + updateRunningNotification() triggerFixTimeNotification() // 尝试触发保活通知 NotificationTimingController.getInstance().triggerNotificationIfAllowed( @@ -259,34 +229,10 @@ class NotificationKeepAliveService : Service() { } /** - * 更新前台服务通知 + * 更新服务通知 */ - private fun updateForegroundNotification() { - if (!isServiceRunning) { - NotiLogger.d("前台服务未运行,无法更新通知") - return - } - - if (!canSendNotification()) { - NotiLogger.d("无通知权限,忽略本次更新通知") - return - } - - try { - val newNotification = createForegroundNotification() - startForeground(NOTIFICATION_ID, newNotification) - NotiLogger.d("前台服务通知已更新") - } catch (e: Exception) { - NotiLogger.e("更新前台服务通知失败", e) - } - } - - /** - * 创建前台通知 - */ - private fun createForegroundNotification(): Notification { - // 使用NotificationTriggerController提供的构建函数 - return NotificationTriggerController.buildResidentNotification(this) + private fun updateRunningNotification() { + // 不再需要更新前台通知 } } \ No newline at end of file