From 9082f15b631e7e1f23772da05a91383ba753fb43 Mon Sep 17 00:00:00 2001 From: devttl Date: Mon, 20 Oct 2025 22:56:43 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E7=9F=A5=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/taskttl/core/alarm/AlarmReceiver.kt | 37 +------------------ ...d.kt => AppNotificationManager.android.kt} | 34 +++++++++++------ .../core/notification/NotificationWorker.kt | 3 +- ...onManager.kt => AppNotificationManager.kt} | 4 +- .../kotlin/com/taskttl/data/di/KoinModels.kt | 1 - ...r.ios.kt => AppNotificationManager.ios.kt} | 2 +- 6 files changed, 29 insertions(+), 52 deletions(-) rename composeApp/src/androidMain/kotlin/com/taskttl/core/notification/{NotificationManager.android.kt => AppNotificationManager.android.kt} (86%) rename composeApp/src/commonMain/kotlin/com/taskttl/core/notification/{NotificationManager.kt => AppNotificationManager.kt} (82%) rename composeApp/src/iosMain/kotlin/com/taskttl/core/notification/{NotificationManager.ios.kt => AppNotificationManager.ios.kt} (97%) diff --git a/composeApp/src/androidMain/kotlin/com/taskttl/core/alarm/AlarmReceiver.kt b/composeApp/src/androidMain/kotlin/com/taskttl/core/alarm/AlarmReceiver.kt index df39995..6059a59 100644 --- a/composeApp/src/androidMain/kotlin/com/taskttl/core/alarm/AlarmReceiver.kt +++ b/composeApp/src/androidMain/kotlin/com/taskttl/core/alarm/AlarmReceiver.kt @@ -3,7 +3,7 @@ package com.taskttl.core.alarm import android.content.BroadcastReceiver import android.content.Context import android.content.Intent -import com.taskttl.core.notification.NotificationManager +import com.taskttl.core.notification.AppNotificationManager import com.taskttl.core.notification.NotificationPayload import com.taskttl.core.notification.NotificationRepeatType import kotlinx.coroutines.CoroutineScope @@ -20,7 +20,7 @@ class AlarmReceiver : BroadcastReceiver() { // 使用协程调用统一通知方法 CoroutineScope(Dispatchers.Main).launch { - NotificationManager.scheduleNotification( + AppNotificationManager.scheduleNotification( NotificationPayload( id = id, title = title, @@ -30,38 +30,5 @@ class AlarmReceiver : BroadcastReceiver() { ) ) } - - - - // val id = intent.getStringExtra("id") ?: return - // val title = intent.getStringExtra("title") ?: "TaskTTL" - // val message = intent.getStringExtra("message") ?: "" - // - // val channelId = "taskttl_channel" - // - // // 确保通道存在 - // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - // val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager - // if (nm.getNotificationChannel(channelId) == null) { - // val channel = android.app.NotificationChannel( - // channelId, - // "TaskTTL Notifications", - // android.app.NotificationManager.IMPORTANCE_HIGH - // ) - // channel.description = "TaskTTL 提醒通知" - // nm.createNotificationChannel(channel) - // } - // } - // - // // 构建通知 - // val builder = NotificationCompat.Builder(context, channelId) - // .setContentTitle(title) - // .setContentText(message) - // .setSmallIcon(R.mipmap.ic_launcher) - // .setAutoCancel(true) - // .setPriority(NotificationCompat.PRIORITY_HIGH) - // - // val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager - // nm.notify(id.hashCode(), builder.build()) } } diff --git a/composeApp/src/androidMain/kotlin/com/taskttl/core/notification/NotificationManager.android.kt b/composeApp/src/androidMain/kotlin/com/taskttl/core/notification/AppNotificationManager.android.kt similarity index 86% rename from composeApp/src/androidMain/kotlin/com/taskttl/core/notification/NotificationManager.android.kt rename to composeApp/src/androidMain/kotlin/com/taskttl/core/notification/AppNotificationManager.android.kt index c410363..43ac289 100644 --- a/composeApp/src/androidMain/kotlin/com/taskttl/core/notification/NotificationManager.android.kt +++ b/composeApp/src/androidMain/kotlin/com/taskttl/core/notification/AppNotificationManager.android.kt @@ -3,6 +3,7 @@ package com.taskttl.core.notification import android.Manifest import android.app.Activity import android.app.AlarmManager +import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent @@ -12,7 +13,11 @@ import android.os.Build import androidx.core.app.ActivityCompat import androidx.core.app.NotificationCompat import androidx.core.content.ContextCompat -import androidx.work.* +import androidx.work.ExistingPeriodicWorkPolicy +import androidx.work.OneTimeWorkRequestBuilder +import androidx.work.PeriodicWorkRequestBuilder +import androidx.work.WorkManager +import androidx.work.workDataOf import com.taskttl.MainApplication import com.taskttl.R import com.taskttl.core.alarm.AlarmReceiver @@ -20,7 +25,7 @@ import com.taskttl.core.utils.LogUtils import java.util.concurrent.TimeUnit import kotlin.math.max -actual object NotificationManager { +actual object AppNotificationManager { private val channelId = "taskttl_channel" @@ -35,13 +40,14 @@ actual object NotificationManager { private fun setupNotificationChannel() { val activity = MainApplication.currentActivity() ?: return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - val nm = activity.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager + val nm = + activity.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager if (nm.getNotificationChannel(channelId) == null) { val channel = android.app.NotificationChannel( channelId, "TaskTTL Notifications", - android.app.NotificationManager.IMPORTANCE_HIGH - ).apply { description = "TaskTTL 提醒通知" } + NotificationManager.IMPORTANCE_HIGH + ) nm.createNotificationChannel(channel) LogUtils.d("DevTTL_NotificationTest", "Notification channel created") } @@ -137,10 +143,11 @@ actual object NotificationManager { else -> 24 * 60 * 60 * 1000L } - val request = PeriodicWorkRequestBuilder(interval, TimeUnit.MILLISECONDS) - .setInputData(data) - .addTag(payload.id) - .build() + val request = + PeriodicWorkRequestBuilder(interval, TimeUnit.MILLISECONDS) + .setInputData(data) + .addTag(payload.id) + .build() workManager.enqueueUniquePeriodicWork( payload.id, @@ -153,7 +160,8 @@ actual object NotificationManager { /** 立即显示通知 */ private fun showImmediateNotification(payload: NotificationPayload) { val activity = getActivity() - val nm = activity.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager + val nm = + activity.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notification = NotificationCompat.Builder(activity, channelId) .setContentTitle(payload.title) @@ -184,14 +192,16 @@ actual object NotificationManager { ) alarmManager.cancel(pendingIntent) - val nm = activity.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager + val nm = + activity.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager nm.cancel(id.hashCode()) } actual suspend fun cancelAll() { val activity = getActivity() WorkManager.getInstance(activity).cancelAllWork() - val nm = activity.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager + val nm = + activity.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager nm.cancelAll() } } diff --git a/composeApp/src/androidMain/kotlin/com/taskttl/core/notification/NotificationWorker.kt b/composeApp/src/androidMain/kotlin/com/taskttl/core/notification/NotificationWorker.kt index 64f1cdf..3f2a59a 100644 --- a/composeApp/src/androidMain/kotlin/com/taskttl/core/notification/NotificationWorker.kt +++ b/composeApp/src/androidMain/kotlin/com/taskttl/core/notification/NotificationWorker.kt @@ -1,5 +1,6 @@ package com.taskttl.core.notification +import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent @@ -48,7 +49,7 @@ class NotificationWorker( .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) .setPriority(NotificationCompat.PRIORITY_HIGH) - val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager + val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager nm.notify(id.hashCode(), builder.build()) } } diff --git a/composeApp/src/commonMain/kotlin/com/taskttl/core/notification/NotificationManager.kt b/composeApp/src/commonMain/kotlin/com/taskttl/core/notification/AppNotificationManager.kt similarity index 82% rename from composeApp/src/commonMain/kotlin/com/taskttl/core/notification/NotificationManager.kt rename to composeApp/src/commonMain/kotlin/com/taskttl/core/notification/AppNotificationManager.kt index 324c9cd..8244c31 100644 --- a/composeApp/src/commonMain/kotlin/com/taskttl/core/notification/NotificationManager.kt +++ b/composeApp/src/commonMain/kotlin/com/taskttl/core/notification/AppNotificationManager.kt @@ -4,9 +4,9 @@ package com.taskttl.core.notification * 通知管理器 * @author admin * @date 2025/10/16 - * @constructor 创建[NotificationManager] + * @constructor 创建[AppNotificationManager] */ -expect object NotificationManager { +expect object AppNotificationManager { /** * 日程通知 * @param [payload] 有效载荷 diff --git a/composeApp/src/commonMain/kotlin/com/taskttl/data/di/KoinModels.kt b/composeApp/src/commonMain/kotlin/com/taskttl/data/di/KoinModels.kt index cf2647a..35fc2ab 100644 --- a/composeApp/src/commonMain/kotlin/com/taskttl/data/di/KoinModels.kt +++ b/composeApp/src/commonMain/kotlin/com/taskttl/data/di/KoinModels.kt @@ -1,6 +1,5 @@ package com.taskttl.data.di -import com.taskttl.core.notification.NotificationManager import com.taskttl.data.repository.OnboardingRepository import com.taskttl.data.repository.SettingsRepository import com.taskttl.data.repository.impl.OnboardingRepositoryImpl diff --git a/composeApp/src/iosMain/kotlin/com/taskttl/core/notification/NotificationManager.ios.kt b/composeApp/src/iosMain/kotlin/com/taskttl/core/notification/AppNotificationManager.ios.kt similarity index 97% rename from composeApp/src/iosMain/kotlin/com/taskttl/core/notification/NotificationManager.ios.kt rename to composeApp/src/iosMain/kotlin/com/taskttl/core/notification/AppNotificationManager.ios.kt index 7271b29..95aa80e 100644 --- a/composeApp/src/iosMain/kotlin/com/taskttl/core/notification/NotificationManager.ios.kt +++ b/composeApp/src/iosMain/kotlin/com/taskttl/core/notification/AppNotificationManager.ios.kt @@ -2,7 +2,7 @@ package com.taskttl.core.notification import platform.UserNotifications.* -actual class NotificationManager { +actual object AppNotificationManager { actual suspend fun scheduleNotification(payload: NotificationPayload) { val content = UNMutableNotificationContent().apply { title = payload.title