Browse Source

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

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

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

@ -850,13 +850,21 @@ private var companionManager: BleCompanionManager? = null
// Android 8.0+ 需要创建通知渠道 // Android 8.0+ 需要创建通知渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val existingChannel = manager.getNotificationChannel(CHANNEL_ID)
if (existingChannel == null) {
val channel = NotificationChannel( val channel = NotificationChannel(
CHANNEL_ID, CHANNEL_ID,
"语音助手代理服务", "Voice assistant agent",
NotificationManager.IMPORTANCE_LOW 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) manager.createNotificationChannel(channel)
} }
}
// 点击通知打开主界面 // 点击通知打开主界面
val intent = packageManager.getLaunchIntentForPackage(packageName)?.apply { val intent = packageManager.getLaunchIntentForPackage(packageName)?.apply {
@ -873,11 +881,18 @@ private var companionManager: BleCompanionManager? = null
null null
} }
val appIconResId = applicationInfo.icon.takeIf { it != 0 } ?: android.R.drawable.ic_dialog_info
// 构建通知 // 构建通知
val builder = NotificationCompat.Builder(this, CHANNEL_ID) val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Voitrans") .setContentTitle("Voitrans")
.setContentText("语音助手代理服务运行中") .setContentText("Voice assistant agent is running")
.setSmallIcon(android.R.drawable.ic_dialog_info) .setSmallIcon(appIconResId)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setShowWhen(false)
// 设置点击意图 // 设置点击意图
pendingIntent?.let { builder.setContentIntent(it) } pendingIntent?.let { builder.setContentIntent(it) }

Loading…
Cancel
Save