IM 即时通讯
圈组消息管理(已废弃)
更新时间: 2022/12/09 19:39:57
本文已不再维护,请前往圈组消息相关查看更新的文档。
消息管理
消息定义
圈组消息类包括常用消息所用到的字段,同时含有消息扩展,反垃圾相关设置 NIMQChatMessageAntispamSetting 等。 圈组消息也支持消息抄送服务和第三方回调功能,抄送服务和第三方回调功能详见功能介绍。
/**
* 消息处理状态
*/
typedef NS_ENUM(NSInteger, NIMQChatMessageStatus) {
/**
* 初始状态
* @discussion
*/
NIMQChatMessageStatusInit = 0,
/**
* 已撤回
* @discussion
*/
NIMQChatMessageStatusRevoked = 1,
/**
* 已删除
* @discussion
*/
NIMQChatMessageStatusDeleted = 2,
/**
* 自定义
* @discussion 自定义状态值必须大于10000
*/
NIMQChatMessageStatusCustom = 10000,
};
/**
* 消息引用类型
*/
typedef NS_ENUM(NSInteger, NIMQChatMessageReferType) {
/**
* 回复的
* @discussion
*/
NIMQChatMessageReferTypeReply = 1,
/**
* Thread
* @discussion
*/
NIMQChatMessageReferTypeThread = 2,
/**
* 所有
* @discussion
*/
NIMQChatMessageReferTypeAll = 3,
};
/**
* 消息结构
*/
@interface NIMQChatMessage : NSObject
消息结构原型
/**
* 消息类型
*/
@property (nonatomic,assign,readonly) NIMMessageType messageType;
/**
* 消息所属会话
*/
@property (nullable,nonatomic,copy,readonly) NIMSession *session;
/**
* 消息ID,唯一标识
*/
@property (nonatomic,copy,readonly) NSString *messageId;
/**
* 消息服务端ID
*/
@property (nonatomic,copy,readonly) NSString *serverID;
/**
* 消息文本
* @discussion 所有消息类型都支持
*/
@property (nullable,nonatomic,copy) NSString *text;
/**
* 消息附件内容对象
*/
@property (nullable,nonatomic,strong) id<NIMMessageObject> messageObject;
/**
* 消息附件的字符串内容
*/
@property (nullable,nonatomic, copy, readonly) NSString *rawAttachment;
/**
* 消息设置
* @discussion 可以通过这个字段制定当前消息的各种设置
*/
@property (nullable,nonatomic,strong) NIMMessageSetting *setting;
/**
* 消息推送文案,长度限制500字,撤回消息时该字段无效
*/
@property (nullable,nonatomic,copy) NSString *apnsContent;
/**
* 消息推送Payload
* @discussion 可以通过这个字段定义消息推送 Payload ,支持字段参考苹果技术文档,长度限制 2K,撤回消息时该字段无效
*/
@property (nullable,nonatomic,copy) NSDictionary *apnsPayload;
/**
* 服务器扩展
* @discussion 客户端可以设置这个字段,这个字段将在本地存储且发送至对端,上层需要保证 NSDictionary 可以转换为 JSON,长度限制 1K
*/
@property (nullable,nonatomic,copy) NSDictionary *remoteExt;
/**
* 客户端本地扩展
* @discussion 客户端可以设置这个字段,这个字段只在本地存储,不会发送至对端,上层需要保证 NSDictionary 可以转换为 JSON
*/
@property (nullable,nonatomic,copy) NSDictionary *localExt;
/**
* 消息发送时间
* @discussion 发送成功后将被服务器自动修正
*/
@property (nonatomic,assign) NSTimeInterval timestamp;
/**
* 消息投递状态 仅针对发送的消息
*/
@property (nonatomic,assign,readonly) NIMMessageDeliveryState deliveryState;
/**
* 消息附件下载状态 仅针对收到的消息
*/
@property (nonatomic,assign,readonly) NIMMessageAttachmentDownloadState attachmentDownloadState;
/**
* 是否是收到的消息
* @discussion
*/
@property (nonatomic,assign,readonly) BOOL isReceivedMsg;
/**
* 是否是往外发的消息
* @discussion
*/
@property (nonatomic,assign,readonly) BOOL isOutgoingMsg;
/**
* 消息是否标记为已删除
* @discussion
*/
@property (nonatomic,assign,readonly) BOOL isDeleted;
/**
* 消息是否标记为已撤回
* @discussion
*/
@property (nonatomic,assign,readonly) BOOL isRevoked;
/**
* 消息处理状态
*/
@property (nonatomic,assign) NIMQChatMessageStatus status;
/**
* 消息来源
*/
@property (nullable,nonatomic,copy) NSString *from;
/**
* 消息发送者名字
* @discussion
*/
@property (nullable,nonatomic,copy,readonly) NSString *senderName;
/**
* 发送者客户端类型
*/
@property (nonatomic,assign,readonly) NIMLoginClientType senderClientType;
/**
* 圈组频道ID
*/
@property (nonatomic,readonly) unsigned long long qchatChannelId;
/**
* 圈组服务器ID
*/
@property (nonatomic,readonly) unsigned long long qchatServerId;
/**
* 是否艾特所有人
* @discussion
*/
@property(nonatomic, assign) BOOL mentionedAll;
/**
* 被艾特的人的accid列表
* @discussion
*/
@property(nonatomic, copy) NSArray<NSString *> *mentionedAccids;
/**
* 消息更新时间
* @discussion
*/
@property(nonatomic, assign) NSTimeInterval updateTimestamp;
/**
* 更新消息内容
* @discussion 如果是更新的消息,里面有具体更新内容
*/
@property (nullable, nonatomic, strong) NIMQChatMessageUpdateContent *updateContent;
/**
* 消息更新操作信息
* @discussion 如果是更新的消息,里面有具体操作信息
*/
@property (nullable, nonatomic, strong) NIMQChatMessageUpdateOperatorInfo *updateOperatorInfo;
/**
* 环境变量
* @discussion 环境变量,用于指向不同的抄送、第三方回调等配置
*/
@property (nullable,nonatomic,copy) NSString *env;
/**
* 回复消息引用
* @discussion
*/
@property(nullable, nonatomic, copy, readonly) NIMQChatMessageRefer *replyRefer;
/**
* thread消息引用
* @discussion
*/
@property(nullable, nonatomic, copy, readonly) NIMQChatMessageRefer *threadRefer;
/**
* 是否来自缓存
* @discussion
*/
@property (nonatomic,assign,readonly) BOOL fromCache;
/**
* 易盾反垃圾设置
*/
@property (nullable, nonatomic, copy) NIMQChatMessageAntispamSetting *yidunAntiSpamSetting;
/**
* 易盾反垃圾返回的结果字段
*/
@property (nullable, nonatomic, copy) NIMQChatMessageAntispamResult *yidunAntiSpamResult;
/**
* 第三方回调回来的自定义扩展字段
*/
@property (nonatomic,copy,readonly) NSString *callbackExt;
/**
* 消息子类型, 大于0的int NSNumber包装值
*/
@property (nonatomic, strong) NSNumber *subType;
@end
反垃圾类原型
反垃圾业务和开通详见产品介绍及IM中的反垃圾模块。
/**
* 圈组消息反垃圾设置
*/
@interface NIMQChatMessageAntispamSetting : NSObject
/**
* 指定是否需要使用自定义反垃圾字段(即antiSpamContent), YES:不需要,NO:需要, 默认NO
*
*/
@property (nonatomic, assign) BOOL enableAntiSpamContent;
/**
* 开发者自定义的反垃圾字段
* @discussion 如果提供了这个字段, 在过反垃圾时不会再使用content或者attach
*/
@property (nullable,nonatomic,copy) NSString *antiSpamContent;
/**
* 用户配置的对某些单条消息另外的反垃圾的业务ID
*/
@property (nullable,nonatomic,copy) NSString *antiSpamBusinessId;
/**
* 此条消息是否使用易盾反垃圾, NO:(在开通易盾的情况下)表示这条消息不过易盾反垃圾, 其他都是按照原来的规则
*/
@property (nonatomic, assign) BOOL antiSpamUsingYidun;
/**
* 易盾check的回调URL, 目前仅支持Audio类型的消息, 最长256个字符, 如果不合法则忽略该参数
*/
@property (nullable,nonatomic,copy) NSString *yidunCallback;
/**
* 易盾反垃圾增强反作弊专属字段
* @discussion 透传易盾反垃圾增强反作弊专属字段
*/
@property (nullable,nonatomic,copy) NSDictionary *yidunAntiCheating;
/**
* 易盾反垃圾扩展字段,限制json,长度限制1024
*/
@property (nullable,nonatomic,copy) NSString *yidunAntiSpamExt;
@end
发送消息
- 接口原型
/**
* 发送消息
*
* @param message 消息
* @param session 接受方
* @param error 错误 如果在准备发送消息阶段发生错误,这个error会被填充相应的信息
*
* @return 是否调用成功,这里返回的 result 只是表示当前这个函数调用是否成功,需要后续的回调才能够判断消息是否已经发送至服务器
*/
- (BOOL)sendMessage:(NIMQChatMessage *)message
toSession:(NIMSession *)session
error:(NSError * __nullable *)error;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NIMQChatMessage *message = [[NIMQChatMessage alloc] init];
NIMSession * session = [NIMSession sessionForQChat:121212 qchatServerId:123456];
message.text = @"文本消息的内容";
//反垃圾
NIMQChatMessageAntispamSetting *antispamSetting = [NIMQChatMessageAntispamSetting new];
antispamSetting.antiSpamBusinessId = @"{\"txtbid\":\"5624564236342d543\"}";
antispamSetting.antiSpamUsingYidun = YES;
message.yidunAntiSpamSetting = antispamSetting;
//抄送
message.env = @"***"
NSError *error = nil;
BOOL result = [qchatMessageManager sendMessage:message
toSession:session
error:&error];
异步发送消息
- 接口原型
/**
* 异步发送消息
*
* @param message 消息
* @param session 接收方
* @param completion 发送完成后的回调,这里的回调完成只表示当前这个函数调用完成,需要后续的回调才能判断消息是否已经发送至服务器
*/
- (void)sendMessage:(NIMQChatMessage *)message
toSession:(NIMSession *)session
completion:(nullable NIMQChatHandler)completion;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NIMQChatMessage *message = [[NIMQChatMessage alloc] init];
NIMSession * session = [NIMSession sessionForQChat:121212 qchatServerId:123456];
message.text = @"文本消息的内容";
[qchatMessageManager sendMessage:message
toSession:session
completion:^(NSError *__nullable error) {
// your code
}];
取消正在发送的消息
- 接口原型
/**
* 取消正在发送的消息
*
* @param message 目标消息
* @return 是否调用成功
*/
- (BOOL)cancelSendingMessage:(NIMQChatMessage *)message;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
BOOL result = [qchatMessageManager cancelSendingMessage:(NIMQChatMessage *)message];
重发消息
- 接口原型
/**
* 重发消息
*
* @param message 重发消息
* @param error 错误 如果在准备发送消息阶段发生错误,这个error会被填充相应的信息
*
* @return 是否调用成功,这里返回的 result 只是表示当前这个函数调用是否成功,需要后续的回调才能够判断消息是否已经发送至服务器
*/
- (BOOL)resendMessage:(NIMQChatMessage *)message
error:(NSError * __nullable *)error;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NSError *error = nil;
BOOL result = [qchatMessageManager resendMessage:(NIMQChatMessage *)message
error:&error];
生成转发消息
- 接口原型
/**
* 生成转发消息
* 得到转发消息后,用户自己再调用sendMessage:toSession:error: 或 sendMessage:toSession:completion:进行发送 和 直接调用forwardMessage:toSession:error:效果一样,但是这样可以得到转发消息的进度方法回调和是否转发成功方法回调
*
* @param message 要转发的消息
* @param error 错误 如果在准备发送消息阶段发生错误,这个error会被填充相应的信息
*
* @return 生成的需要转发的消息
*/
- (nullable NIMQChatMessage *)makeForwardMessage:(NIMQChatMessage *)message
error:(NSError * __nullable *)error;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NSError *error = nil;
NIMQChatMessage *result = [qchatMessageManager makeForwardMessage:(NIMQChatMessage *)message
error:&error];
转发消息
- 接口原型
/**
* 转发消息
*
* @param message 消息
* @param session 接收方
* @param error 错误 如果在准备发送消息阶段发生错误,这个error会被填充相应的信息
*
* @return 是否调用成功,这里返回的 result 只是表示当前这个函数调用是否成功,需要后续的回调才能够判断消息是否已经发送至服务器
*/
- (BOOL)forwardMessage:(NIMQChatMessage *)message
toSession:(NIMSession *)session
error:(NSError * __nullable *)error;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NSError *error = nil;
NIMSession * session = [NIMSession sessionForQChat:121213 qchatServerId:123456];
BOOL result = [qchatMessageManager forwardMessage:(NIMQChatMessage *)message
toSession:(NIMSession *)session
error:&error];
消息是否正在传输
- 接口原型
/**
* 消息是否正在传输 (发送/接受附件)
*
* @param message 消息
*
* @return 是否正在传输
*/
- (BOOL)messageInTransport:(NIMQChatMessage *)message;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
BOOL result = [qchatMessageManager messageInTransport:(NIMQChatMessage *)message];
传输消息的进度
- 接口原型
/**
* 传输消息的进度 (发送/接受附件)
*
* @param message 消息
*
* @return 正在传输的消息进度,如果消息不在传输,则返回0
*/
- (float)messageTransportProgress:(NIMQChatMessage *)message;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
float result = [qchatMessageManager messageTransportProgress:(NIMQChatMessage *)message];
即将发送消息回调
- 接口原型
/**
* 即将发送消息回调
* @discussion 因为发消息之前可能会有个准备过程,所以需要在收到这个回调时才将消息加入到 Datasource 中
* @param message 当前发送的消息
*/
- (void)willSendMessage:(NIMQChatMessage *)message;
- 示例代码
// 添加监听
- (void)addListener {
[[[NIMSDK sharedSDK] qchatMessageManager] addDelegate:self];
}
// 移除监听
- (void)removeListener {
[[[NIMSDK sharedSDK] qchatMessageManager] removeDelegate:self];
}
// 回调方法
- (void)qchatKickedOut:(NIMLoginKickoutResult *)result {
// your code
}
上传资源文件成功的回调
- 接口原型
/**
* 上传资源文件成功的回调
* @discussion 对于需要上传资源的消息(图片,视频,音频等),SDK 将在上传资源成功后通过这个接口进行回调,上层可以在收到该回调后进行推送信息的重新配置 (APNS payload)
* @param urlString 当前消息资源获得的 url 地址
* @param message 当前发送的消息
*/
- (void)uploadAttachmentSuccess:(NSString *)urlString
forMessage:(NIMQChatMessage *)message;
```
- 示例代码
```objc
// 添加监听
- (void)addListener {
[[[NIMSDK sharedSDK] qchatMessageManager] addDelegate:self];
}
// 移除监听
- (void)removeListener {
[[[NIMSDK sharedSDK] qchatMessageManager] removeDelegate:self];
}
// 回调方法
- (void)qchatKickedOut:(NIMLoginKickoutResult *)result {
// your code
}
发送消息进度回调
- 接口原型
/**
* 发送消息进度回调
*
* @param message 当前发送的消息
* @param progress 进度
*/
- (void)sendMessage:(NIMQChatMessage *)message
progress:(float)progress;
```
- 示例代码
```objc
// 添加监听
- (void)addListener {
[[[NIMSDK sharedSDK] qchatMessageManager] addDelegate:self];
}
// 移除监听
- (void)removeListener {
[[[NIMSDK sharedSDK] qchatMessageManager] removeDelegate:self];
}
// 回调方法
- (void)qchatKickedOut:(NIMLoginKickoutResult *)result {
// your code
}
发送消息完成回调
- 接口原型
/**
* 发送消息完成回调
*
* @param message 当前发送的消息
* @param error 失败原因,如果发送成功则error为nil
*/
- (void)sendMessage:(NIMQChatMessage *)message
didCompleteWithError:(nullable NSError *)error;
```
- 示例代码
```objc
// 添加监听
- (void)addListener {
[[[NIMSDK sharedSDK] qchatMessageManager] addDelegate:self];
}
// 移除监听
- (void)removeListener {
[[[NIMSDK sharedSDK] qchatMessageManager] removeDelegate:self];
}
// 回调方法
- (void)qchatKickedOut:(NIMLoginKickoutResult *)result {
// your code
}
收取消息附件
- 接口原型
/**
* 收取消息附件
*
* @param message 需要收取附件的消息
* @param error 错误
*
* @return 是否调用成功
* @discussion 附件包括:图片消息的图片缩略图,视频消息的视频缩略图,音频消息的音频文件,文件消息的文件以及自定义消息中的自定义文件
*/
- (BOOL)fetchMessageAttachment:(NIMQChatMessage *)message
error:(NSError * __nullable *)error;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NSError *error = nil;
BOOL result = [qchatMessageManager fetchMessageAttachment:(NIMQChatMessage *)message
error:&error];
取消收取消息附件
- 接口原型
/**
* 取消收取消息附件
*
* @param message 需要取消收取附件的消息
*
* @discussion 附件包括:图片消息的图片缩略图,视频消息的视频缩略图,音频消息的音频文件,文件消息的文件以及自定义消息中的自定义文件
*/
- (void)cancelFetchingMessageAttachment:(NIMQChatMessage *)message;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
[qchatMessageManager cancelFetchingMessageAttachment:(NIMQChatMessage *)message];
收取消息附件回调
- 接口原型
/**
* 收取消息附件回调
* @param message 当前收取的消息
* @param progress 进度
* @discussion 附件包括:图片,视频的缩略图,语音文件
*/
- (void)fetchMessageAttachment:(NIMQChatMessage *)message
progress:(float)progress;
```
- 示例代码
```objc
// 添加监听
- (void)addListener {
[[[NIMSDK sharedSDK] qchatMessageManager] addDelegate:self];
}
// 移除监听
- (void)removeListener {
[[[NIMSDK sharedSDK] qchatMessageManager] removeDelegate:self];
}
// 回调方法
- (void)qchatKickedOut:(NIMLoginKickoutResult *)result {
// your code
}
收取消息附件完成回调
- 接口原型
/**
* 收取消息附件完成回调
*
* @param message 当前收取的消息
* @param error 错误返回,如果收取成功,error为nil
*/
- (void)fetchMessageAttachment:(NIMQChatMessage *)message
didCompleteWithError:(nullable NSError *)error;
```
- 示例代码
```objc
// 添加监听
- (void)addListener {
[[[NIMSDK sharedSDK] qchatMessageManager] addDelegate:self];
}
// 移除监听
- (void)removeListener {
[[[NIMSDK sharedSDK] qchatMessageManager] removeDelegate:self];
}
// 回调方法
- (void)qchatKickedOut:(NIMLoginKickoutResult *)result {
// your code
}
收到消息回调
收到的消息内部包含反垃圾结果 NIMQChatMessageAntispamResult,第三方抄送的回调 callbackExt 等。
- 接口原型
/**
* 收到消息回调
*
* @param messages 消息列表,内部为NIMQChatMessage
*/
- (void)onRecvMessages:(NSArray<NIMQChatMessage *> *)messages;
- 示例代码
// 添加监听
- (void)addListener {
[[[NIMSDK sharedSDK] qchatMessageManager] addDelegate:self];
}
// 移除监听
- (void)removeListener {
[[[NIMSDK sharedSDK] qchatMessageManager] removeDelegate:self];
}
// 回调方法
- (void)qchatKickedOut:(NIMLoginKickoutResult *)result {
// your code
}
更新圈组消息
- 接口原型
/**
* 更新圈组消息
* @discussion 支持文本,服务器扩展,自定义消息状态
*
* @param param 传入参数
* @param completion 结果回调
*/
- (void)updateMessage:(NIMQChatUpdateMessageParam *)param
completion:(nullable NIMQChatUpdateMessageHandler)completion;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NIMQChatUpdateMessageParam *param = [[NIMQChatUpdateMessageParam alloc] init];
param.message = message;
NIMQChatUpdateMessageInfo *updateInfo = [[NIMQChatUpdateMessageInfo alloc] init];
// 修改为自定义状态
updateInfo.status = 1000123;
param.updateInfo = updateInfo;
//更新设置
NIMQChatUpdateParam *universalUpdateInfo = [NIMQChatUpdateParam new];
universalUpdateInfo.postscript = @"更新消息";
universalUpdateInfo.pushContent = @"更新了消息";
universalUpdateInfo.env = @"***";
universalUpdateInfo.routeEnable = YES;
param.updateParam = universalUpdateInfo;
[qchatMessageManager updateMessage:param
completion:^(NSError *__nullable error, NIMQChatUpdateMessageResult *__nullable result) {
// your code
}];
圈组消息更新事件回调
- 接口原型
/**
* 圈组消息更新事件回调
*
* @param event 事件详情
*/
- (void)onMessageUpdate:(NIMQChatUpdateMessageEvent *)event;
- 示例代码
// 添加监听
- (void)addListener {
[[[NIMSDK sharedSDK] qchatMessageManager] addDelegate:self];
}
// 移除监听
- (void)removeListener {
[[[NIMSDK sharedSDK] qchatMessageManager] removeDelegate:self];
}
// 回调方法
- (void)qchatKickedOut:(NIMLoginKickoutResult *)result {
// your code
}
撤回圈组消息
- 接口原型
撤回圈组消息,目前没有时间限制。
/**
* 撤回圈组消息
* @discussion
*
* @param param 传入参数
* @param completion 结果回调
*/
- (void)revokeMessage:(NIMQChatRevokeMessageParam *)param
completion:(nullable NIMQChatUpdateMessageHandler)completion;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NIMQChatRevokeMessageParam *param = [[NIMQChatRevokeMessageParam alloc] init];
param.message = message;
NIMQChatUpdateParam *updateParam = [[NIMQChatUpdateParam alloc] init];
updateParam.postscript = @"撤回附言";
param.updateParam = updateParam;
[qchatMessageManager revokeMessage:param
completion:^(NSError *__nullable error, NIMQChatUpdateMessageResult *__nullable result) {
// your code
}];
删除圈组消息
- 接口原型
/**
* 删除圈组消息
* @discussion
*
* @param param 传入参数
* @param completion 结果回调
*/
- (void)deleteMessage:(NIMQChatDeleteMessageParam *)param
completion:(nullable NIMQChatUpdateMessageHandler)completion;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NIMQChatDeleteMessageParam *param = [[NIMQChatDeleteMessageParam alloc] init];
param.message = message;
NIMQChatUpdateParam *updateParam = [[NIMQChatUpdateParam alloc] init];
updateParam.postscript = @"删除附言";
param.updateParam = updateParam;
[qchatMessageManager deleteMessage:param
completion:^(NSError *__nullable error, NIMQChatUpdateMessageResult *__nullable result) {
// your code
}];
标记消息已读
- 接口原型
/**
* 标记消息已读
*
* @param param 传入参数
* @param completion 结果回调
*/
- (void)markMessageRead:(NIMQChatMarkMessageReadParam *)param
completion:(nullable NIMQChatHandler)completion;
该方法调用存在频控,300ms 最多可调用一次。
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NIMQChatMarkMessageReadParam *param = [[NIMQChatMarkMessageReadParam alloc] init];
param.serverId = 123456;
param.channelId = 121212;
param.ackTimestamp = 1641006661.111;
[qchatMessageManager markMessageRead:param
completion:^(NSError *__nullable error) {
// your code
}];
从服务端查询历史消息
- 接口原型
/**
* 从服务端查询历史消息
*
* @param param 传入参数
* @param completion 结果回调
*/
- (void)getMessageHistory:(NIMQChatGetMessageHistoryParam *)param
completion:(nullable NIMQChatGetMessageHistoryHandler)completion;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NIMQChatGetMessageHistoryParam *param = [[NIMQChatGetMessageHistoryParam alloc] init];
param.serverId = 123456;
param.channelId = 121212;
param.fromTime = 0;
//如果fromTime上有多条消息,可以通过excludeMsgId来指定实际的起始消息为excludeMsgId对应的消息的下一条
param.limit = @(20);
[qchatMessageManager getMessageHistory:param
completion:^(NSError *__nullable error, NIMQChatGetMessageHistoryResult *__nullable result) {
// your code
}];
发送自定义系统通知
- 接口原型
/**
* 发送自定义系统通知
*
* @param param 传入参数
* @param completion 结果回调
*/
- (void)sendSystemNotification:(NIMQChatSendSystemNotificationParam *)param
completion:(nullable NIMQChatSendSystemNotificationHandler)completion;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NIMQChatSendSystemNotificationParam *param = [[NIMQChatSendSystemNotificationParam alloc] initWithServerId:123456 channelId:121212];
param.body = @"系统通知内容";
[qchatMessageManager sendSystemNotification:param
completion:^(NSError *__nullable error, NIMQChatSendSystemNotificationResult *__nullable result) {
// your code
}];
重发自定义系统通知
- 接口原型
/**
* 重发自定义系统通知
*
* @param param 传入参数
* @param completion 结果回调
*/
- (void)resendSystemNotification:(NIMQChatResendSystemNotificationParam *)param
completion:(nullable NIMQChatSendSystemNotificationHandler)completion;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NIMQChatResendSystemNotificationParam *param = [[NIMQChatResendSystemNotificationParam alloc] init];
param.systemNotification = systemNotification;
[qchatMessageManager resendSystemNotification:param
completion:^(NSError *__nullable error, NIMQChatSendSystemNotificationResult *__nullable result) {
// your code
}];
圈组系统通知接收事件回调
- 接口原型
/**
* 圈组系统通知接收事件回调
*
* @param result 结果详情
*/
- (void)onRecvSystemNotification:(NIMQChatReceiveSystemNotificationResult *)result;
- 示例代码
// 添加监听
- (void)addListener {
[[[NIMSDK sharedSDK] qchatMessageManager] addDelegate:self];
}
// 移除监听
- (void)removeListener {
[[[NIMSDK sharedSDK] qchatMessageManager] removeDelegate:self];
}
// 回调方法
- (void)qchatKickedOut:(NIMLoginKickoutResult *)result {
// your code
}
更新系统通知
- 接口原型
/**
* 更新系统通知(可以更新状态、也可以更新内容)
*/
- (void)updateSystemNotification:(NIMQChatUpdateSystemNotificationParam *)param
completion:(nullable NIMQChatUpdateSystemNotificationHandler)completion;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NIMQChatUpdateSystemNotificationParam *param = [[NIMQChatUpdateSystemNotificationParam alloc] init];
param.msgServerId = systemNotification.messageServerID;
param.notificationType = NIMQChatSystemNotificationTypeCustom;
param.status = 10000;
//更新设置
NIMQChatUpdateParam *universalUpdateInfo = [NIMQChatUpdateParam new];
universalUpdateInfo.postscript = @"更新系统通知";
universalUpdateInfo.pushContent = @"更新了系统通知";
universalUpdateInfo.env = @"***";
universalUpdateInfo.routeEnable = YES;
param.updateParam = universalUpdateInfo;
[qchatMessageManager updateSystemNotification:param
completion:^(NSError *__nullable error, NIMQChatUpdateSystemNotificationResult *__nullable result) {
// your code
}];
圈组系统通知更新事件回调
- 接口原型
/**
* 圈组系统通知更新事件回调
*
* @param result 结果详情
*/
- (void)onSystemNotificationUpdate:(NIMQChatSystemNotificationUpdateResult *)result;
- 示例代码
// 添加监听
- (void)addListener {
[[[NIMSDK sharedSDK] qchatMessageManager] addDelegate:self];
}
// 移除监听
- (void)removeListener {
[[[NIMSDK sharedSDK] qchatMessageManager] removeDelegate:self];
}
// 回调方法
- (void)qchatKickedOut:(NIMLoginKickoutResult *)result {
// your code
}
标记系统通知已读
- 接口原型
/**
* 标记系统通知已读
*
* @param param 传入参数
* @param completion 结果回调
*/
- (void)markSystemNotificationsRead:(NIMQChatMarkSystemNotificationsReadParam *)param
completion:(nullable NIMQChatHandler)completion;
- 示例代码
id<NIMQChatMessageManager> qchatMessageManager = [[NIMSDK sharedSDK] qchatMessageManager];
NIMQChatMarkSystemNotificationsReadParam *param = [[NIMQChatMarkSystemNotificationsReadParam alloc] init];
NIMQChatMarkSystemNotificationsReadItem *item = [[NIMQChatMarkSystemNotificationsReadItem alloc] init];
item.messageServerId = 10101010;
item.type = NIMQChatSystemNotificationTypeCustom;
param.items = @[item];
[qchatMessageManager markSystemNotificationsRead:param
completion:^(NSError *__nullable error) {
// your code
}];
圈组未读信息变更事件回调
- 接口原型
/**
* 圈组未读信息变更事件回调
*
* @param event 事件详情
*/
- (void)unreadInfoChanged:(NIMQChatUnreadInfoChangedEvent *)event;
- 示例代码
// 添加监听
- (void)addListener {
[[[NIMSDK sharedSDK] qchatMessageManager] addDelegate:self];
}
// 移除监听
- (void)removeListener {
[[[NIMSDK sharedSDK] qchatMessageManager] removeDelegate:self];
}
// 回调方法
- (void)qchatKickedOut:(NIMLoginKickoutResult *)result {
// your code
}
消息缓存设置
圈组消息提供消息缓存功能,只需要打开设置开关即可使用。目前消息提供100个频道(channel),每个频道20条消息的缓存,按照时间淘汰之前的消息。
- 接口原型
/**
* NIM 圈组 配置项目
*/
@interface NIMQChatConfig : NSObject
/**
* 返回配置项实例
*
* @return 配置项
*/
+ (instancetype)sharedConfig;
/**
* 开启消息缓存支持
* @discusssion 默认为 NO。
*/
@property (nonatomic,assign) BOOL enabledMessageCache;
@end
- 使用示例
// 在消息功能使用前设置
[NIMQChatConfig sharedConfig].enabledMessageCache = YES;
消息缓存管理
提供了查询缓存功能和清除缓存功能。
- 接口原型
/**
* 从本地缓存查询消息
*
* @param param 传入参数
* @param completion 结果回调
*/
- (void)getMessageCache:(NIMQChatGetMessageCacheParam *)param
completion:(nullable NIMQChatGetMessageCacheHandler)completion;
/**
* 清理消息本地缓存
*/
- (void)clearMessageCache;
查询缓存时,可根据需要设置是否带有回复的消息和快捷评论。 此为查询缓存时参数:
/**
* 从本地缓存查询消息的参数类型
*/
@interface NIMQChatGetMessageCacheParam : NSObject
/**
* 圈组服务器ID
*/
@property(nonatomic, assign) unsigned long long serverId;
/**
* 圈组频道ID
*/
@property(nonatomic, assign) unsigned long long channelId;
/**
* 是否需要返回引用的消息
*/
@property(nonatomic, assign) BOOL withRefer;
/**
* 是否需要返回快捷评论
*/
@property(nonatomic, assign) BOOL withQuickComment;
@end
- 示例代码
NIMQChatGetMessageCacheParam *param = [[NIMQChatGetMessageCacheParam alloc] init];
param.serverId = 543264;
param.channelId = 534264;
param.withRefer = YES;
param.withQuickComment = YES;
[[NIMSDK sharedSDK].qchatMessageManager getMessageCache:param
completion:^(NSError *__nullable error) {
// your code
}];
//删除缓存
[[NIMSDK sharedSDK].qchatMessageManager clearMessageCache];
搜索消息
调用[NIMQChatMessageManager
]中的searchMsgByPage:completion:
方法,可按照关键字和消息发送者等搜索当前用户所在服务器下的全部频道或单频道的消息,包括文本消息、图片消息、语音消息、视频消息和文件消息(其他类型消息均不支持搜索)。
- 在开通圈组功能后,圈组搜索相关能力还需要再额外开通才可调用。您可通过云信官网首页提供的微信和电话等联系方式咨询商务经理开通。
- 已被撤回或删除的消息不可搜索。
示例代码如下:
NIMQChatSearchMsgByPageParam *param = [[NIMQChatSearchMsgByPageParam alloc] init];
param.serverId = 54325;
param.keyword = @"key";
param.limit = 100;
param.fromTime = 1694524354.435;
param.toTime = 1694895432.432;
[[NIMSDK sharedSDK].qchatMessageManager searchMsgByPage:param completion:^(NSError * _Nullable error, NIMQChatSearchMsgByPageResult * _Nullable result) {
//your code
}];
此文档是否对你有帮助?
有帮助
我要吐槽