Files
rider-pro-android-app/app/src/main/java/com/aiosman/ravenow/utils/MessageParser.kt
AllenTom 8901792561 新增通知类消息展示及解析逻辑
- 新增 `OpenIMMessageType.kt`,用于统一管理 OpenIM 的消息类型常量,并提供判断是否为通知类型消息的辅助函数。
- 新增 `NotificationMessageHelper.kt`,用于根据不同的通知类型生成用户友好的提示文本。
- 新增 `NotificationMessageItem.kt` Composable 组件,用于在聊天界面中展示居中样式的通知消息。
- 在 `ChatItem` 实体中增加 `isNotification` 字段,以标识消息是否为通知类型。
- 更新 `MessageParser` 和 `ChatItem` 的转换逻辑,以正确解析和处理通知消息,确保其在会话列表和聊天界面中正确显示。
- 在 `GroupChatScreen`, `ChatScreen`, `ChatAiScreen` 中,根据 `isNotification` 字段调用新的 `NotificationMessageItem` 组件来渲染通知消息。
- 修正获取群组会话 ID 时可能存在的 `s` 前缀缺失问题。
2025-11-14 15:05:11 +08:00

97 lines
3.7 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.aiosman.ravenow.utils
import com.aiosman.ravenow.AppState
import com.aiosman.ravenow.entity.OpenIMMessageType
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import io.openim.android.sdk.models.Message
/**
* OpenIM 消息解析工具类
* 用于解析 ConversationInfo 中的 latestMsg JSON 字符串
*/
object MessageParser {
/**
* 解析最新消息的显示文本和发送者信息
* @param latestMsgJson 最新消息的JSON字符串
* @return Pair<displayText, isSelf> 显示文本和是否是自己发送的消息
*/
fun parseLatestMessage(latestMsgJson: String?): Pair<String, Boolean> {
var displayText = ""
var isSelf = false
try {
if (!latestMsgJson.isNullOrEmpty()) {
val gson = Gson()
val message = gson.fromJson(latestMsgJson, Message::class.java)
// 检查是否为通知类型消息
// 1. 检查消息类型是否为通知类型
// 2. 检查发送者ID是否为系统账户如 "imAdmin"、"administrator" 等)
val sendID = message.sendID ?: ""
val isSystemAccount = sendID == "imAdmin" || sendID == "administrator" || sendID.isEmpty()
val isNotificationType = OpenIMMessageType.isNotification(message.contentType)
val isNotification = isNotificationType || isSystemAccount
// 通知类型消息不显示"我:"前缀,设置 isSelf = false
if (isNotification) {
isSelf = false
} else {
// 判断是否是自己发送的消息
isSelf = sendID == AppState.profile?.trtcUserId
}
// 根据消息类型生成显示文本
displayText = getMessageDisplayText(message)
} else {
displayText = "[暂无消息]"
}
} catch (e: JsonSyntaxException) {
// JSON 解析失败,使用原始文本
displayText = latestMsgJson ?: "[消息解析失败]"
} catch (e: Exception) {
// 其他异常
displayText = "[消息]"
}
return Pair(displayText, isSelf)
}
/**
* 根据消息类型获取显示文本
* @param message OpenIM Message 对象
* @return 消息的显示文本
*/
private fun getMessageDisplayText(message: Message): String {
// 检查是否为通知类型消息
// 1. 检查消息类型是否为通知类型
// 2. 检查发送者ID是否为系统账户如 "imAdmin"、"administrator" 等)
val sendID = message.sendID ?: ""
val isSystemAccount = sendID == "imAdmin" || sendID == "administrator" || sendID.isEmpty()
val isNotificationType = OpenIMMessageType.isNotification(message.contentType)
val isNotification = isNotificationType || isSystemAccount
if (isNotification) {
// 使用 NotificationMessageHelper 生成通知文本
return NotificationMessageHelper.getNotificationText(message)
}
return when (message.contentType) {
101 -> { // TEXT
message.textElem?.content ?: "[文本消息]"
}
102 -> "[图片]" // IMAGE
103 -> "[语音]" // AUDIO
104 -> "[视频]" // VIDEO
105 -> "[文件]" // FILE
106 -> "[位置]" // LOCATION
107 -> "[自定义消息]" // CUSTOM
108 -> "[合并消息]" // MERGE
109 -> "[名片]" // CARD
110 -> "[引用消息]" // QUOTE
else -> "[消息]"
}
}
}