fix(push): create notification channels from profiles

这个提交包含在:
XuqmGroup 2026-07-13 16:50:55 +08:00
父节点 999e21bcf2
当前提交 fbc6f089c1

查看文件

@ -15,11 +15,13 @@ internal object PushNotificationChannelManager {
fun apply(context: Context, pushConfig: JsonObject?) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || pushConfig == null) return
val channels = pushConfig.getAsJsonArray("channels") ?: return
val channelByKey = mutableMapOf<String, String>()
val routeToChannel = mutableMapOf<String, String>()
val manager = context.getSystemService(NotificationManager::class.java)
channels.mapNotNull { it.takeIf { item -> item.isJsonObject }?.asJsonObject }
pushConfig.getAsJsonArray("channels")
?.mapNotNull { it.takeIf { item -> item.isJsonObject }?.asJsonObject }
.orEmpty()
.forEach { channel ->
val key = channel.text("key").ifBlank { return@forEach }
val baseChannelId = channel.text("channelId").ifBlank { key }
@ -42,6 +44,34 @@ internal object PushNotificationChannelManager {
Log.d(TAG, "Notification channel ready key=$key id=$effectiveChannelId")
}
// Current tenant PUSH schema stores notification channels directly in profiles.
// Vendor push APIs send this exact channelId, so do not append a version suffix.
pushConfig.getAsJsonArray("profiles")
?.mapNotNull { it.takeIf { item -> item.isJsonObject }?.asJsonObject }
.orEmpty()
.filter { it.bool("enabled", true) }
.forEach { profile ->
val channelId = profile.text("channelId").ifBlank { return@forEach }
val routeType = profile.text("routeType").ifBlank { "DEFAULT" }
val profileKey = profile.text("profileKey").ifBlank { routeType }
val notificationChannel = NotificationChannel(
channelId,
routeType,
profile.importance(),
).apply {
description = profile.text("remark")
enableVibration(profile.bool("vibration", true))
setShowBadge(profile.bool("badge", true))
if (!profile.bool("sound", true)) {
setSound(null, null)
}
}
manager.createNotificationChannel(notificationChannel)
channelByKey[profileKey] = channelId
routeToChannel[routeType] = channelId
Log.d(TAG, "Profile notification channel ready route=$routeType id=$channelId")
}
val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
prefs.edit().apply {
channelByKey.forEach { (key, channelId) -> putString("channel_$key", channelId) }
@ -52,6 +82,9 @@ internal object PushNotificationChannelManager {
val channelId = channelByKey[channelKey] ?: return@forEach
putString(KEY_PREFIX_ROUTE + type, channelId)
}
routeToChannel.forEach { (routeType, channelId) ->
putString(KEY_PREFIX_ROUTE + routeType, channelId)
}
}.apply()
}