fix: 通知自检卡片拼接bug(通道状态被吞);新增测试通知与通道修复

This commit is contained in:
cbb
2026-10-12 01:56:01 +08:00
parent 9fd2287396
commit 893e613562
3 changed files with 76 additions and 31 deletions
+2 -2
View File
@@ -13,8 +13,8 @@ android {
applicationId = "com.smbfast.app" applicationId = "com.smbfast.app"
minSdk = 26 minSdk = 26
targetSdk = 35 targetSdk = 35
versionCode = 25 versionCode = 26
versionName = "1.21.0" versionName = "1.21.1"
} }
signingConfigs { signingConfigs {
@@ -106,10 +106,7 @@ class TransferService : Service() {
private fun createChannel() { private fun createChannel() {
if (Build.VERSION.SDK_INT >= 26) { if (Build.VERSION.SDK_INT >= 26) {
val ch = NotificationChannel(CHANNEL_ID, "传输进度", NotificationManager.IMPORTANCE_LOW) createChannelStatic(this)
ch.description = "SMB 后台传输进行中"
ch.setShowBadge(false)
getSystemService(NotificationManager::class.java).createNotificationChannel(ch)
} }
} }
@@ -323,6 +320,43 @@ class TransferService : Service() {
/** 岛参数模式:极简(仅 baseInfo/ticker,无岛结构,白名单实验二) */ /** 岛参数模式:极简(仅 baseInfo/ticker,无岛结构,白名单实验二) */
const val MODE_MINIMAL = "minimal" 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) { fun start(ctx: Context) {
ContextCompat.startForegroundService(ctx, Intent(ctx, TransferService::class.java)) ContextCompat.startForegroundService(ctx, Intent(ctx, TransferService::class.java))
@@ -316,11 +316,13 @@ private fun currentVersionCode(): Long {
} }
} }
/** 通知系统自检:应用通知权限/通道被关是"通知和灵动岛全都不显示"的最常见原因 */ /** 通知系统自检:通道被系统屏蔽是"通知和灵动岛全都不显示"的常见原因(应用权限显示正常也未必可见) */
@Composable @Composable
private fun NotificationDiagCard() { private fun NotificationDiagCard() {
val ctx = LocalContext.current val ctx = LocalContext.current
val (enabled, hasChannel, importance) = remember { var refresh by remember { mutableStateOf(0) }
var msg by remember { mutableStateOf<String?>(null) }
val (enabled, hasChannel, importance) = remember(refresh) {
val nm = androidx.core.app.NotificationManagerCompat.from(ctx) val nm = androidx.core.app.NotificationManagerCompat.from(ctx)
val ch = nm.getNotificationChannel("smbfast_transfers") val ch = nm.getNotificationChannel("smbfast_transfers")
Triple(nm.areNotificationsEnabled(), ch != null, ch?.importance ?: -1) Triple(nm.areNotificationsEnabled(), ch != null, ch?.importance ?: -1)
@@ -328,7 +330,7 @@ private fun NotificationDiagCard() {
val impText = when { val impText = when {
!hasChannel -> "未创建" !hasChannel -> "未创建"
importance == -1 -> "未创建" importance == -1 -> "未创建"
importance == 0 -> "已屏蔽(不显示)" importance == 0 -> "已被屏蔽(通知完全不显示!)"
importance == 1 -> "MIN(通知栏不显示)" importance == 1 -> "MIN(通知栏不显示)"
importance == 2 -> "LOW(静默显示在通知栏,无横幅)" importance == 2 -> "LOW(静默显示在通知栏,无横幅)"
importance == 3 -> "DEFAULT" importance == 3 -> "DEFAULT"
@@ -338,11 +340,28 @@ private fun NotificationDiagCard() {
icon = Icons.Default.Notifications, icon = Icons.Default.Notifications,
brush = Neon.File, brush = Neon.File,
title = "通知系统自检", title = "通知系统自检",
subtitle = "系统通知权限:" + if (enabled) "已授予" else "已被关闭!通知与灵动岛都不会显示" + subtitle = "系统通知权限:" + (if (enabled) "已授予" else "已被关闭!通知与灵动岛都不会显示") +
"\n通知通道「传输进度」:" + impText, "\n通知通道「传输进度」:" + impText,
) { ) {
if (!enabled) {
Spacer(Modifier.height(8.dp)) 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("去开启通知权限") { MiniChip("去开启通知权限") {
runCatching { runCatching {
ctx.startActivity( ctx.startActivity(
@@ -353,19 +372,11 @@ private fun NotificationDiagCard() {
} }
} }
} }
if (hasChannel && importance == 0) { }
val m = msg
if (m != null) {
Spacer(Modifier.height(8.dp)) Spacer(Modifier.height(8.dp))
MiniChip("去修复通知通道") { Text(m, color = Neon.TextDim, fontSize = 11.sp, lineHeight = 15.sp)
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"),
)
}
}
} }
} }
} }