云信自定义消息流程

1、编写Podfile文件,屏蔽UI库,放开UI源码库的本地依赖。

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'YXDemo' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  #登录组件
  pod 'YXLogin', '1.0.0'
  
  #可选UI库
  #pod 'NEContactUIKit', '9.3.0'
  #pod 'NEQChatUIKit', '9.3.0'
  #pod 'NEConversationUIKit', '9.3.1'
  #pod 'NEChatUIKit', '9.3.0'
  #pod 'NETeamUIKit', '9.3.0'
  
  
  #可选Kit库(和UIKit对应)
  pod 'NEContactKit', '9.3.0'
  pod 'NEQChatKit', '9.3.0'
  pod 'NEConversationKit', '9.3.0'
  pod 'NEChatKit', '9.3.0'
  pod 'NETeamKit', '9.3.0'
  
  #基础kit库
  pod 'NECommonUIKit', '9.3.0'
  pod 'NECommonKit', '9.3.0'
  pod 'NECoreIMKit', '9.3.0'
  pod 'NECoreKit', '9.3.0'
  
  #扩展库
  pod 'NEMapKit', '9.3.1'


  # 如果需要查看UI部分源码请注释掉以上在线依赖,打开下面的本地依赖
  pod 'NEQChatUIKit', :path => 'NEQChatUIKit/NEQChatUIKit.podspec'
  pod 'NEContactUIKit', :path => 'NEContactUIKit/NEContactUIKit.podspec'
  pod 'NEConversationUIKit', :path => 'NEConversationUIKit/NEConversationUIKit.podspec'
  pod 'NETeamUIKit', :path => 'NETeamUIKit/NETeamUIKit.podspec'
  pod 'NEChatUIKit', :path => 'NEChatUIKit/NEChatUIKit.podspec'
  #pod 'NEMapKit', :path => 'NEMapKit/NEMapKit.podspec'

end

#fix bug in Xcode 14
post_install do |installer|
  installer.pods_project.targets.each do |target|
    if target.name == 'RSKPlaceholderTextView'
      target.build_configurations.each do |config|
        config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
      end
    end
  end
end

2、将官方demo里的UI源码库,拷贝到项目根目录。


iShot_2023-04-04_15.10.58.png

3、执行pod update,首次安装可能需要多次install才能成功。


iShot_2023-04-04_15.17.50.png

4、进行初始化操作。
@implementation AppDelegate
@synthesize window = _window;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [[IMManager shared] initIM];
    
    ViewController *vc = [[ViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];

    return YES;
}

    static let shared = IMManager()
    let account = "10086"
    let token = ""

    func initIM() {
        
        // init
        let option = NIMSDKOption()
        option.appKey = ""
        IMKitClient.instance.setupCoreKitIM(option)

        NEKeyboardManager.shared.enable = true
        NEKeyboardManager.shared.shouldResignOnTouchOutside = true
    }

5、自定义消息的封装和解析。
方式一(不推荐):注册新建的自定义消息解析器。
1)假设新类型的名称叫recruit,新建Attachment文件,编写recruit类型的消息封装类和反序列解码类,实现NIMCustomAttachment和NIMCustomAttachmentCoding的协议方法。

//
//  IMRecruitAttachment.swift
//  NEChatUIKit
//
//  Created by iss730001004790 on 2023/3/29.
//

import UIKit
import NIMSDK

@objcMembers
open class IMRecruitAttachment: NSObject, NIMCustomAttachment {

    public var iconUrl = ""

    public var linkUrl = ""

    public var receiveId = ""

    public var subTitle = ""

    public var title = ""

    public var price = ""

    public func encode() -> String {
      let info = ["iconUrl": iconUrl, "linkUrl": linkUrl, "receiveId": receiveId, "subTitle": subTitle, "title": title, "price": price] as [String: Any]

        let dic = ["type":2000,"data":info] as [String : Any]

      let jsonData = try? JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted)
      var content = ""
      if let data = jsonData {
        content = String(data: data, encoding: .utf8) ?? ""
      }
      return content
    }
  }

  public class IMRecruitAttachmentDecoder: NSObject, NIMCustomAttachmentCoding {
    public func decodeAttachment(_ content: String?) -> NIMCustomAttachment? {

        var attachment: NIMCustomAttachment?
        let data = content?.data(using: .utf8)
        guard let dataInfo = data else {
          return attachment
        }

        let infoDict = try? JSONSerialization.jsonObject(
          with: dataInfo,
          options: .mutableContainers
        )
  //      let infoResult = infoDict as? [String: Any]
  //      let type = infoResult?["type"] as? Int

  //      switch type {
  //      case 0:
          attachment = decodeCustomMessage(info: infoDict as? [String: Any] ?? [String(): String()])
  //      default:
  //        print("test")
  //      }

        return attachment
      }

    func decodeCustomMessage(info: [String: Any]) -> IMRecruitAttachment {

        let customAttachment = IMRecruitAttachment()

        if let dataInfo = info["data"] as? [String : Any] {
            customAttachment.iconUrl = dataInfo["iconUrl"] as? String ?? ""
            customAttachment.linkUrl = dataInfo["linkUrl"] as? String ?? ""
            customAttachment.receiveId = dataInfo["receiveId"] as? String ?? ""
            customAttachment.subTitle = dataInfo["subTitle"] as? String ?? ""
            customAttachment.title = dataInfo["title"] as? String ?? ""
            customAttachment.price = dataInfo["price"] as? String ?? ""
        }

//      if let type = info["type"] as? Int {
//        customAttachment.type = type
//      }

      return customAttachment
    }
  }

2)删除ChatViewController里的自带的注册自定义消息解析器的代码。


iShot_2023-04-04_15.44.52.png

3)添加新建的recruit类型消息解析器。

//注册自定义消息的解析器
NIMCustomObject.registerCustomDecoder(IMRecruitAttachmentDecoder())

方式二(推荐):使用SDK的自定义消息解析器。
1)在SDK原有的文件IMCustomAttachment,编写子类型recruit和candidates的消息封装类和反序列解码的代码。


// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

import UIKit
import NIMSDK

public class CustomAttachment: NSObject, NIMCustomAttachment {
  public var type = 0

  public var goodsName = "name"

  public var goodsURL = "url"

  public func encode() -> String {
    let info = ["goodsName": goodsName, "goodsURL": goodsURL, "type": type] as [String: Any]

    let jsonData = try? JSONSerialization.data(withJSONObject: info, options: .prettyPrinted)
    var content = ""
    if let data = jsonData {
      content = String(data: data, encoding: .utf8) ?? ""
    }
    return content
  }
}

open class IMRecruitAttachment: NSObject, NIMCustomAttachment {

    public var type = 0

    public var iconUrl = ""

    public var linkUrl = ""

    public var receiveId = ""

    public var subTitle = ""

    public var title = ""

    public var price = ""

    public func encode() -> String {
      let info = ["iconUrl": iconUrl, "linkUrl": linkUrl, "receiveId": receiveId, "subTitle": subTitle, "title": title, "price": price] as [String: Any]

        //添加子类型
        let dic = ["type":2000,"data":info] as [String : Any]

      let jsonData = try? JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted)
      var content = ""
      if let data = jsonData {
        content = String(data: data, encoding: .utf8) ?? ""
      }
      return content
    }
  }

open class IMCandidatesAttachment: NSObject , NIMCustomAttachment {

    public var iconUrl = ""

    public var linkUrl = ""

    public var receiveId = ""

    public var subTitle = ""

    public var title = ""

    public var price = ""

    public func encode() -> String {
      let info = ["iconUrl": iconUrl, "linkUrl": linkUrl, "receiveId": receiveId, "subTitle": subTitle, "title": title, "price": price] as [String: Any]

        //添加子类型
        let dic = ["type":3000,"data":info] as [String : Any]

      let jsonData = try? JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted)
      var content = ""
      if let data = jsonData {
        content = String(data: data, encoding: .utf8) ?? ""
      }
      return content
    }
  }


public class CustomAttachmentDecoder: NSObject, NIMCustomAttachmentCoding {
  public func decodeAttachment(_ content: String?) -> NIMCustomAttachment? {
    var attachment: NIMCustomAttachment?
    let data = content?.data(using: .utf8)
    guard let dataInfo = data else {
      return attachment
    }

    let infoDict = try? JSONSerialization.jsonObject(
      with: dataInfo,
      options: .mutableContainers
    )
    let infoResult = infoDict as? [String: Any]
    let type = infoResult?["type"] as? Int

    switch type {
    case 0:
      attachment =
        decodeCustomMessage(info: infoDict as? [String: Any] ?? [String(): String()])
    case 2000:
      attachment =
        decodeRecruitMessage(info: infoDict as? [String: Any] ?? [String(): String()])
    case 3000:
      attachment =
        decodeCandidatesMessage(info: infoDict as? [String: Any] ?? [String(): String()])

    default:
      print("test")
    }

    return attachment
  }

  func decodeCustomMessage(info: [String: Any]) -> CustomAttachment {
    let customAttachment = CustomAttachment()
    customAttachment.goodsName = info["goodsName"] as? String ?? ""
    customAttachment.goodsURL = info["goodsURL"] as? String ?? ""
    if let type = info["type"] as? Int {
      customAttachment.type = type
    }

    return customAttachment
  }


    func decodeRecruitMessage(info: [String: Any]) -> IMRecruitAttachment {

        let customAttachment = IMRecruitAttachment()

        if let dataInfo = info["data"] as? [String : Any] {
            customAttachment.iconUrl = dataInfo["iconUrl"] as? String ?? ""
            customAttachment.linkUrl = dataInfo["linkUrl"] as? String ?? ""
            customAttachment.receiveId = dataInfo["receiveId"] as? String ?? ""
            customAttachment.subTitle = dataInfo["subTitle"] as? String ?? ""
            customAttachment.title = dataInfo["title"] as? String ?? ""
            customAttachment.price = dataInfo["price"] as? String ?? ""
        }

      if let type = info["type"] as? Int {
        customAttachment.type = type
      }

      return customAttachment
    }

    func decodeCandidatesMessage(info: [String: Any]) -> IMCandidatesAttachment {

        let customAttachment = IMCandidatesAttachment()

        if let dataInfo = info["data"] as? [String : Any] {
            customAttachment.iconUrl = dataInfo["iconUrl"] as? String ?? ""
            customAttachment.linkUrl = dataInfo["linkUrl"] as? String ?? ""
            customAttachment.receiveId = dataInfo["receiveId"] as? String ?? ""
            customAttachment.subTitle = dataInfo["subTitle"] as? String ?? ""
            customAttachment.title = dataInfo["title"] as? String ?? ""
            customAttachment.price = dataInfo["price"] as? String ?? ""
        }

//      if let type = info["type"] as? Int {
//        customAttachment.type = type
//      }

      return customAttachment
    }

}

2)保留ChatViewController里的注册自定义消息解析器的代码。

// 注册自定义消息的解析器
NIMCustomObject.registerCustomDecoder(CustomAttachmentDecoder())

6、在MessageModel里新增recruit和candidates类型的MessageType。另外,新建MessageRecruitMode文件承担起viewmodel的功能,将NIMMessage数据转为cell可用MessageContentModel。

  case recruit
  case candidates
//
//  MessageRecruitModel.swift
//  NEChatUIKit
//
//  Created by iss730001004790 on 2023/3/29.
//

import UIKit
import NIMSDK

class MessageRecruitModel: MessageContentModel {

    public var imageUrl: String?
    public var text: String?
    public var price: String?

    required init(message: NIMMessage?) {
      super.init(message: message)

        contentSize = CGSize(width: qChat_content_maxW, height: 300)

        height = Float(contentSize.height + qChat_margin) + fullNameHeight

        type = .recruit
        if let messageObject = message?.messageObject as? NIMCustomObject {

            let attachment = messageObject.attachment as NIMCustomAttachment?
            if attachment!.isMember(of: IMRecruitAttachment.self){
                let imageTextObject = attachment! as! IMRecruitAttachment

                print(imageTextObject)

                imageUrl = imageTextObject.iconUrl

                text = imageTextObject.title

                price = imageTextObject.price


              }

            }

    }

}

7、编写recruit类型要展示的cell文件。


iShot_2023-04-04_15.34.31.png

8、在ChatViewController里,注册要展示的cell,并在UITableViewDataSource添加它们。


iShot_2023-04-04_15.37.39.png

iShot_2023-04-04_15.40.42.png

9、在ChatViewModel的modelFromMessage方法里,添加custom类型的消息处理。
iShot_2023-04-04_16.01.20.png

如果此处自定义消息有子类型,需添加额外代码,区别处理消息。
1)笔者采取的方式是:添加一个类方法,返回自定义消息的子类型。

//
//  MessageCustomTypeModel.swift
//  NEChatUIKit
//
//  Created by iss730001004790 on 2023/4/6.
//

import NIMSDK
import UIKit

class MessageCustomTypeModel: NSObject {
    class func type(message: NIMMessage?) -> MessageType {
        if let messageObject = message?.messageObject as? NIMCustomObject, let attachment = messageObject.attachment as NIMCustomAttachment? {
            if attachment.isMember(of: IMRecruitAttachment.self) {
                return .recruit

            } else if attachment.isMember(of: IMCandidatesAttachment.self) {
                return .candidates
            }
        }

        return .text
    }
}

2)分别使用子类型的viewmodel处理消息。

    case .custom:
        let customType = MessageCustomTypeModel.type(message: message)

        if customType == .recruit {
            model = MessageRecruitModel(message: message)
        }else if customType == .candidates {
            model = MessageCandidatesModel(message: message)
        }else {
            // 未识别的消息类型,默认为文本消息类型,text为未知消息
            message.text = "未知消息"
            model = MessageContentModel(message: message)
        }

10、运行代码,在MessageRecruitModel打断点,看recruit消息是否解析成对象。


iShot_2023-04-04_15.58.48.png

11、最后,在自定义cell上展示解析好的数据。因为新增的ChatRecruitCell继承自ChatBaseRightCell/ChatBaseLeftCell,所以重写func setModel(_ model: MessageContentModel)方法赋值即可(此处model即MessageRecruitModel解析完的对象)。

    override func setModel(_ model: MessageContentModel) {
        super.setModel(model)
        if let m = model as? MessageRecruitModel, let url = m.imageUrl?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
        } else {
        }

        if let m = model as? MessageRecruitModel, let text = m.text {
        } else {
        }

        if let m = model as? MessageRecruitModel, let text = m.price {
        } else {
        }
    }

自定义消息发送:

再补充一下,自定义消息的发送。将数据组装成自定义的Attachment,注入附件发送出去。

    //MARK: 发起自定义会话
    func pushCustomeMessageChat(dic: NSDictionary, nav: UINavigationController) {
        
        IMManager.shared.loginIM() {

            let attachment = IMRecruitAttachment()
            if (dic.isKind(of: NSDictionary.self)) {
                attachment.title = "title"
                attachment.linkUrl = "linkUrl"
                attachment.subTitle = "subTitle"
                attachment.iconUrl = "iconUrl"
                attachment.price = "priceStr"
                attachment.receiveId = "123"

                // 构造出具体会话,以发给用户account为例
                let session = NIMSession("10010", type: .P2P)

                // 构造出具体消息并注入附件
                let objct = NIMCustomObject()
                objct.attachment = attachment
                let message = NIMMessage()
                message.messageObject = objct

                //发送消息
                try?  NIMSDK.shared().chatManager.sendForwardMessage(message, to: session)
                
                //跳转会话
                let para = ["nav": nav as Any, "session" : session as Any]
                Router.shared.use(PushP2pChatVCRouter, parameters: para, closure: nil)
            }
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,839评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,543评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,116评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,371评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,384评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,111评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,416评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,053评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,558评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,007评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,117评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,756评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,324评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,315评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,539评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,578评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,877评论 2 345

推荐阅读更多精彩内容