From 893e613562bb9da3c1214788c505fcf07ce6469d Mon Sep 17 00:00:00 2001 From: cbb <2625274797@qq.com> Date: Mon, 12 Oct 2026 01:56:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=80=9A=E7=9F=A5=E8=87=AA=E6=A3=80?= =?UTF-8?q?=E5=8D=A1=E7=89=87=E6=8B=BC=E6=8E=A5bug=EF=BC=88=E9=80=9A?= =?UTF-8?q?=E9=81=93=E7=8A=B6=E6=80=81=E8=A2=AB=E5=90=9E=EF=BC=89=EF=BC=9B?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=B5=8B=E8=AF=95=E9=80=9A=E7=9F=A5=E4=B8=8E?= =?UTF-8?q?=E9=80=9A=E9=81=93=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle.kts | 4 +- .../smbfast/app/transfer/TransferService.kt | 42 +++++++++++-- .../smbfast/app/ui/screens/SettingsScreen.kt | 61 +++++++++++-------- 3 files changed, 76 insertions(+), 31 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index a0a2e46..2629020 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -13,8 +13,8 @@ android { applicationId = "com.smbfast.app" minSdk = 26 targetSdk = 35 - versionCode = 25 - versionName = "1.21.0" + versionCode = 26 + versionName = "1.21.1" } signingConfigs { diff --git a/app/src/main/java/com/smbfast/app/transfer/TransferService.kt b/app/src/main/java/com/smbfast/app/transfer/TransferService.kt index fb607d7..9c3ac17 100644 --- a/app/src/main/java/com/smbfast/app/transfer/TransferService.kt +++ b/app/src/main/java/com/smbfast/app/transfer/TransferService.kt @@ -106,10 +106,7 @@ class TransferService : Service() { private fun createChannel() { if (Build.VERSION.SDK_INT >= 26) { - val ch = NotificationChannel(CHANNEL_ID, "传输进度", NotificationManager.IMPORTANCE_LOW) - ch.description = "SMB 后台传输进行中" - ch.setShowBadge(false) - getSystemService(NotificationManager::class.java).createNotificationChannel(ch) + createChannelStatic(this) } } @@ -323,6 +320,43 @@ class TransferService : Service() { /** 岛参数模式:极简(仅 baseInfo/ticker,无岛结构,白名单实验二) */ const val MODE_MINIMAL = "minimal" + private const val NOTIF_ID_TEST = 1002 + + private fun createChannelStatic(ctx: Context) { + val ch = NotificationChannel(CHANNEL_ID, "传输进度", NotificationManager.IMPORTANCE_LOW) + ch.description = "SMB 后台传输进行中" + ch.setShowBadge(false) + ctx.getSystemService(NotificationManager::class.java).createNotificationChannel(ch) + } + + /** + * 发一条独立 id 的测试通知:验证通道级可见性(应用权限"已授予"不代表通道没被系统屏蔽)。 + * 返回是否发送成功。 + */ + fun sendTestNotification(ctx: Context): Boolean { + if (Build.VERSION.SDK_INT >= 26) createChannelStatic(ctx) + val time = java.text.SimpleDateFormat("HH:mm:ss", java.util.Locale.CHINA) + .format(java.util.Date()) + val n = NotificationCompat.Builder(ctx, CHANNEL_ID) + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle("SMBFast 测试通知") + .setContentText("看到这条即通道可见 · $time") + .setOngoing(false) + .setAutoCancel(true) + .build() + return runCatching { + NotificationManagerCompat.from(ctx).notify(NOTIF_ID_TEST, n) + true + }.getOrDefault(false) + } + + /** 通道一旦被用户/系统屏蔽,应用无法改回;删除重建可恢复默认重要性 */ + fun repairChannel(ctx: Context) { + val nm = ctx.getSystemService(NotificationManager::class.java) + runCatching { nm.deleteNotificationChannel(CHANNEL_ID) } + if (Build.VERSION.SDK_INT >= 26) createChannelStatic(ctx) + } + /** 有任务入队时调用;须在应用处于前台时启动(前台服务启动限制) */ fun start(ctx: Context) { ContextCompat.startForegroundService(ctx, Intent(ctx, TransferService::class.java)) diff --git a/app/src/main/java/com/smbfast/app/ui/screens/SettingsScreen.kt b/app/src/main/java/com/smbfast/app/ui/screens/SettingsScreen.kt index 3c92fd5..b54a045 100644 --- a/app/src/main/java/com/smbfast/app/ui/screens/SettingsScreen.kt +++ b/app/src/main/java/com/smbfast/app/ui/screens/SettingsScreen.kt @@ -316,11 +316,13 @@ private fun currentVersionCode(): Long { } } -/** 通知系统自检:应用通知权限/通道被关是"通知和灵动岛全都不显示"的最常见原因 */ +/** 通知系统自检:通道被系统屏蔽是"通知和灵动岛全都不显示"的常见原因(应用权限显示正常也未必可见) */ @Composable private fun NotificationDiagCard() { val ctx = LocalContext.current - val (enabled, hasChannel, importance) = remember { + var refresh by remember { mutableStateOf(0) } + var msg by remember { mutableStateOf(null) } + val (enabled, hasChannel, importance) = remember(refresh) { val nm = androidx.core.app.NotificationManagerCompat.from(ctx) val ch = nm.getNotificationChannel("smbfast_transfers") Triple(nm.areNotificationsEnabled(), ch != null, ch?.importance ?: -1) @@ -328,7 +330,7 @@ private fun NotificationDiagCard() { val impText = when { !hasChannel -> "未创建" importance == -1 -> "未创建" - importance == 0 -> "已屏蔽(不显示)" + importance == 0 -> "已被屏蔽(通知完全不显示!)" importance == 1 -> "MIN(通知栏不显示)" importance == 2 -> "LOW(静默显示在通知栏,无横幅)" importance == 3 -> "DEFAULT" @@ -338,34 +340,43 @@ private fun NotificationDiagCard() { icon = Icons.Default.Notifications, brush = Neon.File, title = "通知系统自检", - subtitle = "系统通知权限:" + if (enabled) "已授予" else "已被关闭!通知与灵动岛都不会显示" + + subtitle = "系统通知权限:" + (if (enabled) "已授予" else "已被关闭!通知与灵动岛都不会显示") + "\n通知通道「传输进度」:" + impText, ) { - if (!enabled) { - Spacer(Modifier.height(8.dp)) - MiniChip("去开启通知权限") { - runCatching { - ctx.startActivity( - android.content.Intent( - android.provider.Settings.ACTION_APP_NOTIFICATION_SETTINGS, - ).putExtra(android.provider.Settings.EXTRA_APP_PACKAGE, ctx.packageName), - ) + Spacer(Modifier.height(8.dp)) + Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { + MiniChip("发测试通知") { + msg = runCatching { + com.smbfast.app.transfer.TransferService.sendTestNotification(ctx) + "已发出,看状态栏/通知栏是否出现(时间戳区分新旧)" + }.getOrElse { "发送失败:${it.message}" } + refresh++ + } + if (hasChannel && importance <= 1) { + MiniChip("修复通知通道") { + msg = runCatching { + com.smbfast.app.transfer.TransferService.repairChannel(ctx) + "通道已删除并按默认重建,请再点「发测试通知」验证" + }.getOrElse { "修复失败:${it.message}" } + refresh++ + } + } + if (!enabled) { + MiniChip("去开启通知权限") { + runCatching { + ctx.startActivity( + android.content.Intent( + android.provider.Settings.ACTION_APP_NOTIFICATION_SETTINGS, + ).putExtra(android.provider.Settings.EXTRA_APP_PACKAGE, ctx.packageName), + ) + } } } } - if (hasChannel && importance == 0) { + val m = msg + if (m != null) { Spacer(Modifier.height(8.dp)) - MiniChip("去修复通知通道") { - runCatching { - ctx.startActivity( - android.content.Intent( - android.provider.Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS, - ) - .putExtra(android.provider.Settings.EXTRA_APP_PACKAGE, ctx.packageName) - .putExtra(android.provider.Settings.EXTRA_CHANNEL_ID, "smbfast_transfers"), - ) - } - } + Text(m, color = Neon.TextDim, fontSize = 11.sp, lineHeight = 15.sp) } } }