Browse Source

fix(通知): 优化BLE媒体服务的通知配置

修复通知渠道重复创建的问题并完善通知设置
使用应用图标作为通知图标
添加通知的持续显示和静默设置
newdev_shunjiawei
lxm 9 months ago
parent
commit
d3eb78a91b
  1. 35
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/BleMediaService.kt

35
local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/BleMediaService.kt

@ -413,7 +413,7 @@ private var companionManager: BleCompanionManager? = null
// 设置服务状态
isRunning.set(true)
startForeground(NOTIFICATION_ID, createNotification())
// 初始化BLE伴侣设备管理器 - 使用单例模式获取实例
// 初始化BLE伴侣设备管理器 - 使用单例模式获取实例
companionManager = BleCompanionManager.getInstance(this)
// 初始化蓝牙监听器
initializeBluetoothListener()
@ -850,12 +850,20 @@ private var companionManager: BleCompanionManager? = null
// Android 8.0+ 需要创建通知渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
CHANNEL_ID,
"语音助手代理服务",
NotificationManager.IMPORTANCE_LOW
)
manager.createNotificationChannel(channel)
val existingChannel = manager.getNotificationChannel(CHANNEL_ID)
if (existingChannel == null) {
val channel = NotificationChannel(
CHANNEL_ID,
"Voice assistant agent",
NotificationManager.IMPORTANCE_LOW
)
channel.description = "Voice assistant agent"
channel.setShowBadge(false)
channel.enableLights(false)
channel.enableVibration(false)
channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
manager.createNotificationChannel(channel)
}
}
// 点击通知打开主界面
@ -873,15 +881,22 @@ private var companionManager: BleCompanionManager? = null
null
}
val appIconResId = applicationInfo.icon.takeIf { it != 0 } ?: android.R.drawable.ic_dialog_info
// 构建通知
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Voitrans")
.setContentText("语音助手代理服务运行中")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentText("Voice assistant agent is running")
.setSmallIcon(appIconResId)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setShowWhen(false)
// 设置点击意图
pendingIntent?.let { builder.setContentIntent(it) }
return builder.build()
}
}
}

Loading…
Cancel
Save