发送自定义消息

更新时间: 2025/09/11 14:15:56

网易云信 IM UIKit 内置了多种基本消息类型,包括文本、图片、语音、视频、文件等,可满足大部分即时通讯场景。当这些基础消息类型无法满足您的业务需求时,您可以使用自定义消息来扩展功能,例如实现电商订单卡片、红包、系统通知等。

发送自定义消息

自定义消息需要定义两个关键参数:

  • customMessageType:消息类型标识符,用于区分不同类型的自定义消息,用于后面的 UI 展示。
  • customHeight:自定义消息视图的高度。
Objective-C@objc func sendCustomButton() {
    // type 自定义消息类型,该字段必须指定,且不可为 101、102(UIKit 内部已使用),否则解析为【未知消息体】
    // customHeight 自定义消息的高度
    let dataDic: [String: Any] = ["type": customMessageType, "customHeight": 1000]
    let dataJson = NECommonUtil.getJSONStringFromDictionary(dataDic)
    let customMessage = MessageUtils.customMessage(text: "this is a custom message, create time:\(Date.timeIntervalSinceReferenceDate)",
                                                   rawAttachment: dataJson)

    ChatRepo.shared.sendMessage(message: customMessage, conversationId: ChatRepo.conversationId) { result, error, pro in
      if let err = error {
        print("send custom message error : ", err.localizedDescription)
      }
    }
  }

自定义消息 UI 展示

若需要在会话界面将自定义消息按照您自定义的 UI 样式进行展示,请完成以下步骤。

实现自定义 CustomChatCell

添加消息展示的 Cell,并使其继承 NEChatBaseCell

Objective-Cimport NEChatUIKit
import UIKit

class CustomChatCell: NEChatBaseCell {
  public var testLabel = UILabel()

  required init?(coder: NSCoder) {
    super.init(coder: coder)
  }

  override public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    selectionStyle = .none
    backgroundColor = .lightGray
    testLabel.translatesAutoresizingMaskIntoConstraints = false
    contentView.addSubview(testLabel)
    NSLayoutConstraint.activate([
      testLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
      testLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
    ])
    testLabel.font = UIFont.systemFont(ofSize: 14)
    testLabel.textColor = UIColor.black
  }

  override func setModel(_ model: MessageContentModel, _ isSend: Bool) {
    if let model = model as? MessageCustomModel {
      print("this is custom message, customType: \(model.customType)")
      testLabel.text = model.message?.text
      
      let customContent = model.message?.attachment?.raw
    }
  }
}

let customContent = model.message?.attachment?.raw 即为自定义消息内容。

注册自定义 NEChatBaseCell

CustomMessageCell 注册到 IM UIKit,使其在加载后能够处理您的自定义消息。

需要在重写的 ChatViewController 页面的 viewDidLoad 方法中的 super.viewDidLoad() 之前添加,此处的 customMessageType 即发送自定义消息时定义的 customMessageType

Objective-CNEChatUIKitClient.instance.regsiterCustomCell(["\(customMessageType)": CustomChatCell.self])

接收消息

IM UIKit 中已实现消息接收逻辑,您只需在 ChatViewController 重写父类的 onRecvMessages 方法即可。

Objective-Copen func onRecvMessages(_ messages: [V2NIMMessage], _ indexs: [IndexPath]) {
    removeOperationView()
    insertRows(indexs)
  }
此文档是否对你有帮助?
有帮助
去反馈
  • 发送自定义消息
  • 自定义消息 UI 展示
  • 实现自定义 CustomChatCell
  • 注册自定义 NEChatBaseCell
  • 接收消息