iOS

频道管理

更新时间: 2024/03/14 17:08:38

NIM SDK 的NIMQChatChannelManager类提供管理频道的方法,支持创建、修改、查询和删除频道。

前提条件

  • 已注册onRecvSystemNotification:监听圈组的系统通知。示例代码参见圈组系统通知收发

    具体与频道管理相关的系统通知类型,见本文末尾的相关系统通知

  • 创建服务器

  • 已配置“管理频道的权限”,拥有“管理频道的权限”(NIMQChatPermissionTypeManageChannel)才能创建、修改和删除频道。权限通过身份组进行配置和管理,具体请参见身份组概述及其他身份组相关文档。

使用限制

单个服务器的频道数量上限默认为 100 个。

若需要扩展上限,可在控制台配置圈组子功能项(单 server 可创建的 channel 数),具体请参考开通和配置圈组功能

实现方法

创建频道

调用createChannel:completion:方法在某个服务器下创建频道。 调用时需要传入服务器 ID (serverId)、频道名称(name)和频道类型(type)。

该方法的入参结构为QChatCreateChannelParam,其重要参数说明如下:

类型
参数
说明
unsigned long long type 频道类型(NIMQChatChannelType):类型:0-消息频道,1-实时互动频道,100-自定义频道
NIMQChatChannelViewMode viewMode 频道的查看模式,包括:
  • NIMQChatChannelViewModePrivate :私密模式,该模式下,频道只对该频道白名单中的用户可见
  • NIMQChatChannelViewModePublic:公开模式,该模式下,频道对未被加入该频道的黑名单的用户均可见
  • 频道黑白名单相关说明,请参见频道黑白名单

如果将同步模式syncMode设置为同步,那么无法单独设置频道的查看模式(该情况下设置了查看模式将会报 414 错误)。同步模式与频道分组相关,详情请参见频道分组与频道的关联逻辑
unsigned long long categoryId 通过传入频道分组的 ID 为频道指定其所属的分组,频道分组详情请参见频道分组
NIMQChatChannelSyncMode sycnMode 频道的同步模式:
  • NIMQChatChannelSyncModeNotSync:不同步频道分组的配置
  • NIMQChatChannelSyncModeSync:同步频道分组的配置,具体同步的数据包括查看模式(私密或公开)、黑白名单和身份组权限
只有将同步模式设置为不同步,才能单独设置频道的查看模式 viewMode
NIMQChatVisitorMode visitorMode 频道是否对游客可见:
  • NIMQChatVisitorModeNone = -1:不传
  • NIMQChatVisitorModeVisible = 0:可见
  • NIMQChatVisitorModeInvisible = 1:不可见
  • NIMQChatVisitorModeFollow = 2:跟随模式(默认),即如果该频道的查看模式(viewMode)被设置为“公开”则该频道对游客可见,如果被设置为“私密”则对游客不可见
如果频道的 visitorMode 为跟随模式,且同步模式(syncMode)为“与频道分组同步”,则当该频道所属的频道分组的查看模式(viewMode)变更后,该频道对游客的可见性也将变更。例如,在这种情况下,频道分组的查看模式由公开变为私密,则此时该频道对游客从“可见”变为“不可见”。
NSString * antispamBusinessId 设置频道资料内容的内容审核(反垃圾)配置,更多相关说明请参见圈组内容审核

示例代码如下:

id<NIMQChatChannelManager> qchatChannelManager = [[NIMSDK sharedSDK] qchatChannelManager];
NIMQChatCreateChannelParam * param = [[NIMQChatCreateChannelParam alloc] init];
param.serverId = 123456;
param.name = @"云信Channel";
param.type = NIMQChatChannelTypeMsg;
//反垃圾业务id
param.antispamBusinessId = @"{\"picbid\": \"804265342b7425324f53425c343454\", \"txtbid\": \"804265342b7425324f53425c343454\"}";
[qchatChannelManager createChannel:param
    completion:^(NSError *__nullable error, NIMQChatChannel *__nullable result) {
    // your code
}];

修改频道

调用updateChannel:completion:方法修改某个频道的信息,如频道名称、查看模式(公开或私密)、是否对游客可见、频道主题和自定义扩展字段等。调用时需要传入待修改的频道的 ID(channelId)。

调用该方法时,您可设置频道资料的内容审核(反垃圾)配置(antispamBusinessId),内容审核详情请参见圈组内容审核

示例代码如下:

id<NIMQChatChannelManager> qchatChannelManager = [[NIMSDK sharedSDK] qchatChannelManager];
NIMQChatUpdateChannelParam * param = [[NIMQChatUpdateChannelParam alloc] init];
param.channelId = 121212;
param.name = @"更新后的频道名称";
//反垃圾业务id
param.antispamBusinessId = @"{\"picbid\": \"804265342b7584903253244\", \"txtbid\": \"804265342b7534265432523\"}";
[qchatChannelManager updateChannel:param
    completion:^(NSError *__nullable error, NIMQChatChannel *__nullable result) {
    // your code
}];

删除频道

调用deleteChannel:completion:方法可将某个频道删除。调用时需传入待删除频道的 ID(channelId)。

示例代码如下:

id<NIMQChatChannelManager> qchatChannelManager = [[NIMSDK sharedSDK] qchatChannelManager];
NIMQChatDeleteChannelParam * param = [[NIMQChatDeleteChannelParam alloc] init];
param.channelId = 121212;
[qchatChannelManager deleteChannel:param
    completion:^(NSError *error) {
    // your code
}];

频道查询

分页查询频道列表

用户进入服务器后,如果想要获取当前服务器已有(且对该用户可见)的频道,可调用getChannelsByPage:completion:方法分页查询频道列表。

示例代码如下:

id<NIMQChatChannelManager> qchatChannelManager = [[NIMSDK sharedSDK] qchatChannelManager];
NIMQChatGetChannelsByPageParam * param = [[NIMQChatGetChannelsByPageParam alloc] init];
param.serverId = 123456;
// 传0拉取最新的Channel
param.timeTag = 0;
param.limit = 20;
[qchatChannelManager getChannelsByPage:param
    completion:^(NSError *__nullable error, NIMQChatGetChannelsByPageResult *__nullable result) {
    // your code
}];

根据频道 ID 查询频道列表

用户进入服务器后,如果想要检索当前服务器的频道,可调用getChannels:completion:方法根据频道的 ID 进行检索。

示例代码如下:

id<NIMQChatChannelManager> qchatChannelManager = [[NIMSDK sharedSDK] qchatChannelManager];
NIMQChatGetChannelsParam * param = [[NIMQChatGetChannelsParam alloc] init];
param.channelIdArray = @[@(121212), @(131313), @(141414)];
[qchatChannelManager getChannels:param
    completion:^(NSError *__nullable error, NIMQChatGetChannelsResult *__nullable result) {
    // your code
}];

分页查询频道成员列表

用户进入频道后,如果想要检索当前频道的成员有哪些(换而言之,当前频道对哪些用户可见),可调用getChannelMembersByPage:completion:方法可分页查询频道成员列表。

如果需要查询当前时间,timeTag请务必传 0。


示例代码如下:

id<NIMQChatChannelManager> qchatChannelManager = [[NIMSDK sharedSDK] qchatChannelManager];
NIMQChatGetChannelMembersByPageParam * param = [[NIMQChatGetChannelMembersByPageParam alloc] init];
param.serverId = 123456;
param.channelId = 121212;
param.timeTag = 0;
param.limit = 20;
[qchatChannelManager getChannelMembersByPage:param
    completion:^(NSError *__nullable error, NIMQChatGetChannelMembersByPageResult *__nullable result) {
    // your code
}];

查询频道未读信息

用户进入服务器后,如果想获取频道的未读信息(包括未读数和未读状态),可调用getChannelUnreadInfos:completion:方法进行查询。

该方法单次最多查询频道数量为 100。


示例代码如下:

id<NIMQChatChannelManager> qchatChannelManager = [[NIMSDK sharedSDK] qchatChannelManager];
NIMQChatGetChannelUnreadInfosParam * param = [[NIMQChatGetChannelUnreadInfosParam alloc] init];
param.targets = @[@(121212), @(131313), @(141414)];
[qchatChannelManager getChannelUnreadInfos:param
    completion:^(NSError *__nullable error, NIMQChatGetChannelUnreadInfosResult *__nullable result) {
    // your code
}];

相关参考

相关系统通知

圈组系统通知的类型在NIMQChatSystemNotificationType枚举中定义,与频道管理相关的内置系统通知类型如下:

枚举值 说明
NIMQChatSystemNotificationTypeChannelCreate 创建频道
NIMQChatSystemNotificationTypeChannelRemove 删除频道
NIMQChatSystemNotificationTypeChannelUpdate 修改频道信息

更多圈组系统通知相关说明,请参见圈组系统通知相关

API 调用时序

下图可能因为网络问题显示异常,如显示异常,一般刷新当前页面即可正常显示。

uml diagram

上图中:

此文档是否对你有帮助?
有帮助
去反馈
  • 前提条件
  • 使用限制
  • 实现方法
  • 创建频道
  • 修改频道
  • 删除频道
  • 频道查询
  • 分页查询频道列表
  • 根据频道 ID 查询频道列表
  • 分页查询频道成员列表
  • 查询频道未读信息
  • 相关参考
  • 相关系统通知
  • API 调用时序