From fbc6f089c1a3ebfcda59ecb2bfc4bc77a9de5d2c Mon Sep 17 00:00:00 2001 From: XuqmGroup Date: Mon, 13 Jul 2026 16:50:55 +0800 Subject: [PATCH] fix(push): create notification channels from profiles --- .../push/PushNotificationChannelManager.kt | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/sdk-push/src/main/java/com/xuqm/sdk/push/PushNotificationChannelManager.kt b/sdk-push/src/main/java/com/xuqm/sdk/push/PushNotificationChannelManager.kt index 8eeffd4..1775523 100644 --- a/sdk-push/src/main/java/com/xuqm/sdk/push/PushNotificationChannelManager.kt +++ b/sdk-push/src/main/java/com/xuqm/sdk/push/PushNotificationChannelManager.kt @@ -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() + val routeToChannel = mutableMapOf() 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() }