频道会话管理
功能简介
每个社群频道都对应一个类型为 COMMUNITY_CHANNEL 的会话。通过频道的 conversationID,可以使用 ZIM 标准的会话管理接口来操作频道会话,包括:
- 获取频道列表(含会话 ID)
- 清除频道未读消息数
- 设置/清除频道会话草稿
- 设置频道会话免打扰
- 监听频道会话变更
- 将频道会话同步到会话列表
前提条件
- 请参考 实现基本消息收发 完成 ZIM SDK 获取、初始化和用户登录。
- 请参考 使用 Token 鉴权 实现用户鉴权登录。
- 社群功能需要 ZIM SDK 3.0.0 及以上版本。
- 社群功能为旗舰版功能,使用前请联系 ZEGO 技术支持开通。
- 社群功能的 Token 生成方式与其他 ZIM 功能一致,无需额外权限声明。
- 频道会话的
conversationID 与频道的 channelID 不同,发送消息和管理会话时请使用从 ZIMCommunityChannel 对象中获取的 conversationID 字段。
请勿将 channelID 误当作 conversationID 使用。发送消息和进行会话管理操作时,必须使用从频道对象(ZIMCommunityChannel)中获取的 conversationID,而非 channelID。
获取频道列表
在进行频道会话管理之前,需要先调用 queryCommunityChannelListByCommunityID 接口获取频道列表,从返回的 ZIMCommunityChannel 对象中取得 conversationID,用于后续的会话操作。
分页规则:首次将 config.nextFlag 设为 0,将返回的 nextFlag 传入下次请求,直到返回 0 为止。
示例代码
import im.zego.zim.ZIM;
import im.zego.zim.callback.ZIMCommunityChannelListQueriedCallback;
import im.zego.zim.entity.ZIMCommunityChannel;
import im.zego.zim.entity.ZIMCommunityChannelListQueryConfig;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.enums.ZIMErrorCode;
ZIMCommunityChannelListQueryConfig config = new ZIMCommunityChannelListQueryConfig();
config.setNextFlag(0);
zim.queryCommunityChannelList(communityID, 100, config,
new ZIMCommunityChannelListQueriedCallback() {
@Override
public void onCommunityChannelListQueried(String communityID, ArrayList<ZIMCommunityChannel> channelList, long nextFlag, ZIMError errorInfo) {
if (errorInfo.code == ZIMErrorCode.SUCCESS) {
for (ZIMCommunityChannel channel : channelList) {
String conversationID = channel.getConversationID(); // 用于后续会话管理
}
}
}
});
import im.zego.zim.ZIM;
import im.zego.zim.callback.ZIMCommunityChannelListQueriedCallback;
import im.zego.zim.entity.ZIMCommunityChannel;
import im.zego.zim.entity.ZIMCommunityChannelListQueryConfig;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.enums.ZIMErrorCode;
ZIMCommunityChannelListQueryConfig config = new ZIMCommunityChannelListQueryConfig();
config.setNextFlag(0);
zim.queryCommunityChannelList(communityID, 100, config,
new ZIMCommunityChannelListQueriedCallback() {
@Override
public void onCommunityChannelListQueried(String communityID, ArrayList<ZIMCommunityChannel> channelList, long nextFlag, ZIMError errorInfo) {
if (errorInfo.code == ZIMErrorCode.SUCCESS) {
for (ZIMCommunityChannel channel : channelList) {
String conversationID = channel.getConversationID(); // 用于后续会话管理
}
}
}
});
示例代码
ZIMCommunityChannelListQueryConfig *config = [[ZIMCommunityChannelListQueryConfig alloc] init];
config.nextFlag = 0;
[zim queryCommunityChannelList:communityID count:100 config:config callback:^(NSString * _Nonnull communityID, NSArray<ZIMCommunityChannel *> * _Nonnull channelList, long long nextFlag, ZIMError * _Nonnull errorInfo) {
if (errorInfo.code == ZIMErrorCodenoneSuccess) {
for (ZIMCommunityChannel *channel in channelList) {
NSString *conversationID = channel.conversationID; // 用于后续会话管理
}
}
}];
ZIMCommunityChannelListQueryConfig *config = [[ZIMCommunityChannelListQueryConfig alloc] init];
config.nextFlag = 0;
[zim queryCommunityChannelList:communityID count:100 config:config callback:^(NSString * _Nonnull communityID, NSArray<ZIMCommunityChannel *> * _Nonnull channelList, long long nextFlag, ZIMError * _Nonnull errorInfo) {
if (errorInfo.code == ZIMErrorCodenoneSuccess) {
for (ZIMCommunityChannel *channel in channelList) {
NSString *conversationID = channel.conversationID; // 用于后续会话管理
}
}
}];
示例代码
zim::ZIMCommunityChannelListQueryConfig config;
config.nextFlag = 0;
zim_->queryCommunityChannelList(communityID, 100, config,
[=](const std::string &communityID,
const std::vector<zim::ZIMCommunityChannel> &channelList,
long long nextFlag,
const zim::ZIMError &errorInfo) {
if (errorInfo.code == zim::ZIM_ERROR_CODE_SUCCESS) {
for (const auto &channel : channelList) {
std::string conversationID = channel.conversationID; // 用于后续会话管理
}
}
});
zim::ZIMCommunityChannelListQueryConfig config;
config.nextFlag = 0;
zim_->queryCommunityChannelList(communityID, 100, config,
[=](const std::string &communityID,
const std::vector<zim::ZIMCommunityChannel> &channelList,
long long nextFlag,
const zim::ZIMError &errorInfo) {
if (errorInfo.code == zim::ZIM_ERROR_CODE_SUCCESS) {
for (const auto &channel : channelList) {
std::string conversationID = channel.conversationID; // 用于后续会话管理
}
}
});
示例代码
const config: ZIMCommunityChannelListQueryConfig = { nextFlag: 0 };
zim.queryCommunityChannelList(communityID, 100, config)
.then((result: ZIMCommunityChannelListQueriedResult) => {
for (const channel of result.channelList) {
const conversationID = channel.conversationID; // 用于后续会话管理
}
})
.catch((err: ZIMError) => {
// 查询失败
});
const config: ZIMCommunityChannelListQueryConfig = { nextFlag: 0 };
zim.queryCommunityChannelList(communityID, 100, config)
.then((result: ZIMCommunityChannelListQueriedResult) => {
for (const channel of result.channelList) {
const conversationID = channel.conversationID; // 用于后续会话管理
}
})
.catch((err: ZIMError) => {
// 查询失败
});
示例代码
ZIMCommunityChannelListQueryConfig config =
ZIMCommunityChannelListQueryConfig()..nextFlag = 0;
try {
ZIMCommunityChannelListQueriedResult result =
await ZIM.getInstance()!.queryCommunityChannelList(communityID, 100, config);
for (final channel in result.channelList) {
final conversationID = channel.conversationID; // 用于后续会话管理
}
} on PlatformException catch (onError) {
// 查询失败
}
ZIMCommunityChannelListQueryConfig config =
ZIMCommunityChannelListQueryConfig()..nextFlag = 0;
try {
ZIMCommunityChannelListQueriedResult result =
await ZIM.getInstance()!.queryCommunityChannelList(communityID, 100, config);
for (final channel in result.channelList) {
final conversationID = channel.conversationID; // 用于后续会话管理
}
} on PlatformException catch (onError) {
// 查询失败
}
清除未读消息数
调用 clearConversationUnreadMessageCount 接口,将指定频道会话的未读消息数清零。清除成功后,会触发 conversationChanged 回调,反映最新的会话状态。
示例代码
// conversationID 来自 ZIMCommunityChannel.conversationID
String conversationID = channel.getConversationID();
ZIMConversationType conversationType = ZIMConversationType.COMMUNITY_CHANNEL;
zim.clearConversationUnreadMessageCount(conversationID, conversationType,
new ZIMConversationUnreadMessageCountClearedCallback() {
@Override
public void onConversationUnreadMessageCountCleared(String conversationID, ZIMConversationType conversationType, ZIMError errorInfo) {
if (errorInfo.code == ZIMErrorCode.SUCCESS) {
// 未读数已清零
}
}
});
// conversationID 来自 ZIMCommunityChannel.conversationID
String conversationID = channel.getConversationID();
ZIMConversationType conversationType = ZIMConversationType.COMMUNITY_CHANNEL;
zim.clearConversationUnreadMessageCount(conversationID, conversationType,
new ZIMConversationUnreadMessageCountClearedCallback() {
@Override
public void onConversationUnreadMessageCountCleared(String conversationID, ZIMConversationType conversationType, ZIMError errorInfo) {
if (errorInfo.code == ZIMErrorCode.SUCCESS) {
// 未读数已清零
}
}
});
示例代码
[zim clearConversationUnreadMessageCount:channel.conversationID conversationType:ZIMConversationTypeCommunityChannel callback:^(NSString * _Nonnull conversationID, ZIMConversationType conversationType, ZIMError * _Nonnull errorInfo) {
if (errorInfo.code == ZIMErrorCodenoneSuccess) {
// 未读数已清零
}
}];
[zim clearConversationUnreadMessageCount:channel.conversationID conversationType:ZIMConversationTypeCommunityChannel callback:^(NSString * _Nonnull conversationID, ZIMConversationType conversationType, ZIMError * _Nonnull errorInfo) {
if (errorInfo.code == ZIMErrorCodenoneSuccess) {
// 未读数已清零
}
}];
示例代码
std::string conversationID = channel.conversationID;
zim::ZIMConversationType conversationType = zim::ZIM_CONVERSATION_TYPE_COMMUNITY_CHANNEL;
zim_->clearConversationUnreadMessageCount(conversationID, conversationType,
[=](const zim::ZIMError &errorInfo) {
if (errorInfo.code == zim::ZIM_ERROR_CODE_SUCCESS) {
// 未读数已清零
}
});
std::string conversationID = channel.conversationID;
zim::ZIMConversationType conversationType = zim::ZIM_CONVERSATION_TYPE_COMMUNITY_CHANNEL;
zim_->clearConversationUnreadMessageCount(conversationID, conversationType,
[=](const zim::ZIMError &errorInfo) {
if (errorInfo.code == zim::ZIM_ERROR_CODE_SUCCESS) {
// 未读数已清零
}
});
示例代码
const conversationID = channel.conversationID;
const conversationType = ZIMConversationType.CommunityChannel;
zim.clearConversationUnreadMessageCount(conversationID, conversationType)
.then((result: ZIMConversationUnreadMessageCountClearedResult) => {
// 未读数已清零
})
.catch((err: ZIMError) => {
// 操作失败
});
const conversationID = channel.conversationID;
const conversationType = ZIMConversationType.CommunityChannel;
zim.clearConversationUnreadMessageCount(conversationID, conversationType)
.then((result: ZIMConversationUnreadMessageCountClearedResult) => {
// 未读数已清零
})
.catch((err: ZIMError) => {
// 操作失败
});
示例代码
try {
await ZIM.getInstance()!.clearConversationUnreadMessageCount(
channel.conversationID,
ZIMConversationType.communityChannel,
);
// 未读数已清零
} on PlatformException catch (onError) {
// 操作失败
}
try {
await ZIM.getInstance()!.clearConversationUnreadMessageCount(
channel.conversationID,
ZIMConversationType.communityChannel,
);
// 未读数已清零
} on PlatformException catch (onError) {
// 操作失败
}
设置会话草稿
调用 setConversationDraft 接口,为指定频道会话保存草稿内容,用于用户退出频道后继续编辑。草稿内容仅存储在本地,不会发送给其他用户。
示例代码
String draft = "这是草稿内容";
String conversationID = channel.getConversationID();
ZIMConversationType conversationType = ZIMConversationType.COMMUNITY_CHANNEL;
zim.setConversationDraft(draft, conversationID, conversationType,
new ZIMConversationDraftSetCallback() {
@Override
public void onConversationDraftSet(String conversationID, ZIMConversationType conversationType, ZIMError errorInfo) {
if (errorInfo.code == ZIMErrorCode.SUCCESS) {
// 草稿保存成功
}
}
});
String draft = "这是草稿内容";
String conversationID = channel.getConversationID();
ZIMConversationType conversationType = ZIMConversationType.COMMUNITY_CHANNEL;
zim.setConversationDraft(draft, conversationID, conversationType,
new ZIMConversationDraftSetCallback() {
@Override
public void onConversationDraftSet(String conversationID, ZIMConversationType conversationType, ZIMError errorInfo) {
if (errorInfo.code == ZIMErrorCode.SUCCESS) {
// 草稿保存成功
}
}
});
示例代码
[zim setConversationDraft:@"这是草稿内容" conversationID:channel.conversationID conversationType:ZIMConversationTypeCommunityChannel callback:^(NSString * _Nonnull conversationID, ZIMConversationType conversationType, ZIMError * _Nonnull errorInfo) {
if (errorInfo.code == ZIMErrorCodenoneSuccess) {
// 草稿保存成功
}
}];
[zim setConversationDraft:@"这是草稿内容" conversationID:channel.conversationID conversationType:ZIMConversationTypeCommunityChannel callback:^(NSString * _Nonnull conversationID, ZIMConversationType conversationType, ZIMError * _Nonnull errorInfo) {
if (errorInfo.code == ZIMErrorCodenoneSuccess) {
// 草稿保存成功
}
}];
示例代码
std::string draft = "这是草稿内容";
std::string conversationID = channel.conversationID;
zim::ZIMConversationType conversationType = zim::ZIM_CONVERSATION_TYPE_COMMUNITY_CHANNEL;
zim_->setConversationDraft(draft, conversationID, conversationType,
[=](const zim::ZIMError &errorInfo) {
if (errorInfo.code == zim::ZIM_ERROR_CODE_SUCCESS) {
// 草稿保存成功
}
});
std::string draft = "这是草稿内容";
std::string conversationID = channel.conversationID;
zim::ZIMConversationType conversationType = zim::ZIM_CONVERSATION_TYPE_COMMUNITY_CHANNEL;
zim_->setConversationDraft(draft, conversationID, conversationType,
[=](const zim::ZIMError &errorInfo) {
if (errorInfo.code == zim::ZIM_ERROR_CODE_SUCCESS) {
// 草稿保存成功
}
});
示例代码
const draft = '这是草稿内容';
const conversationID = channel.conversationID;
const conversationType = ZIMConversationType.CommunityChannel;
zim.setConversationDraft(draft, conversationID, conversationType)
.then((result: ZIMConversationDraftSetResult) => {
// 草稿保存成功
})
.catch((err: ZIMError) => {
// 操作失败
});
const draft = '这是草稿内容';
const conversationID = channel.conversationID;
const conversationType = ZIMConversationType.CommunityChannel;
zim.setConversationDraft(draft, conversationID, conversationType)
.then((result: ZIMConversationDraftSetResult) => {
// 草稿保存成功
})
.catch((err: ZIMError) => {
// 操作失败
});
示例代码
try {
await ZIM.getInstance()!.setConversationDraft(
'这是草稿内容',
channel.conversationID,
ZIMConversationType.communityChannel,
);
// 草稿保存成功
} on PlatformException catch (onError) {
// 操作失败
}
try {
await ZIM.getInstance()!.setConversationDraft(
'这是草稿内容',
channel.conversationID,
ZIMConversationType.communityChannel,
);
// 草稿保存成功
} on PlatformException catch (onError) {
// 操作失败
}
设置会话通知状态
调用 setConversationNotificationStatus 接口,设置指定频道会话的消息免打扰状态。设置为免打扰后,该频道的未读消息数将不再累加到社群总未读数中,也不会触发系统推送。
示例代码
String conversationID = channel.getConversationID();
ZIMConversationType conversationType = ZIMConversationType.COMMUNITY_CHANNEL;
// ZIMConversationNotificationStatus.DO_NOT_DISTURB = 免打扰
// ZIMConversationNotificationStatus.NOTIFY = 正常通知
zim.setConversationNotificationStatus(
ZIMConversationNotificationStatus.DO_NOT_DISTURB,
conversationID,
conversationType,
new ZIMConversationNotificationStatusSetCallback() {
@Override
public void onConversationNotificationStatusSet(String conversationID, ZIMConversationType conversationType, ZIMError errorInfo) {
if (errorInfo.code == ZIMErrorCode.SUCCESS) {
// 设置成功
}
}
});
String conversationID = channel.getConversationID();
ZIMConversationType conversationType = ZIMConversationType.COMMUNITY_CHANNEL;
// ZIMConversationNotificationStatus.DO_NOT_DISTURB = 免打扰
// ZIMConversationNotificationStatus.NOTIFY = 正常通知
zim.setConversationNotificationStatus(
ZIMConversationNotificationStatus.DO_NOT_DISTURB,
conversationID,
conversationType,
new ZIMConversationNotificationStatusSetCallback() {
@Override
public void onConversationNotificationStatusSet(String conversationID, ZIMConversationType conversationType, ZIMError errorInfo) {
if (errorInfo.code == ZIMErrorCode.SUCCESS) {
// 设置成功
}
}
});
示例代码
[zim setConversationNotificationStatus:ZIMConversationNotificationStatusDoNotDisturb conversationID:channel.conversationID conversationType:ZIMConversationTypeCommunityChannel callback:^(NSString * _Nonnull conversationID, ZIMConversationType conversationType, ZIMError * _Nonnull errorInfo) {
if (errorInfo.code == ZIMErrorCodenoneSuccess) {
// 设置成功
}
}];
[zim setConversationNotificationStatus:ZIMConversationNotificationStatusDoNotDisturb conversationID:channel.conversationID conversationType:ZIMConversationTypeCommunityChannel callback:^(NSString * _Nonnull conversationID, ZIMConversationType conversationType, ZIMError * _Nonnull errorInfo) {
if (errorInfo.code == ZIMErrorCodenoneSuccess) {
// 设置成功
}
}];
示例代码
std::string conversationID = channel.conversationID;
zim::ZIMConversationType conversationType = zim::ZIM_CONVERSATION_TYPE_COMMUNITY_CHANNEL;
zim_->setConversationNotificationStatus(
zim::ZIM_CONVERSATION_NOTIFICATION_STATUS_DO_NOT_DISTURB,
conversationID, conversationType,
[=](const zim::ZIMError &errorInfo) {
if (errorInfo.code == zim::ZIM_ERROR_CODE_SUCCESS) {
// 设置成功
}
});
std::string conversationID = channel.conversationID;
zim::ZIMConversationType conversationType = zim::ZIM_CONVERSATION_TYPE_COMMUNITY_CHANNEL;
zim_->setConversationNotificationStatus(
zim::ZIM_CONVERSATION_NOTIFICATION_STATUS_DO_NOT_DISTURB,
conversationID, conversationType,
[=](const zim::ZIMError &errorInfo) {
if (errorInfo.code == zim::ZIM_ERROR_CODE_SUCCESS) {
// 设置成功
}
});
示例代码
const conversationID = channel.conversationID;
const conversationType = ZIMConversationType.CommunityChannel;
const status = 2; // 2 = 免打扰,1 = 正常通知
zim.setConversationNotificationStatus(status, conversationID, conversationType)
.then((result: ZIMConversationNotificationStatusSetResult) => {
// 设置成功
})
.catch((err: ZIMError) => {
// 设置失败
});
const conversationID = channel.conversationID;
const conversationType = ZIMConversationType.CommunityChannel;
const status = 2; // 2 = 免打扰,1 = 正常通知
zim.setConversationNotificationStatus(status, conversationID, conversationType)
.then((result: ZIMConversationNotificationStatusSetResult) => {
// 设置成功
})
.catch((err: ZIMError) => {
// 设置失败
});
示例代码
try {
await ZIM.getInstance()!.setConversationNotificationStatus(
ZIMConversationNotificationStatus.doNotDisturb,
channel.conversationID,
ZIMConversationType.communityChannel,
);
// 设置成功
} on PlatformException catch (onError) {
// 设置失败
}
try {
await ZIM.getInstance()!.setConversationNotificationStatus(
ZIMConversationNotificationStatus.doNotDisturb,
channel.conversationID,
ZIMConversationType.communityChannel,
);
// 设置成功
} on PlatformException catch (onError) {
// 设置失败
}
将频道会话同步到会话列表
开启该功能后,社群的频道会话会与单聊、群聊会话一起进入 ZIM 的会话列表:您可以通过 queryConversationListWithConfig 一次性拉取包含频道会话的完整列表,并通过 conversationChanged 感知频道会话的最新消息、未读数、草稿等变更,无需为社群频道单独维护一套列表。
同步到会话列表中的频道会话,其会话类型为 COMMUNITY_CHANNEL,conversationID 仍为频道对象 ZIMCommunityChannel 中的 conversationID 字段。
支持的会话管理能力
频道会话进入会话列表后,可使用的会话管理接口与本文前述能力保持一致:
获取频道会话所属的社群 ID
ZIMConversation 新增 relatedID 属性,用于标识会话所关联的对象 ID。当会话类型为 COMMUNITY_CHANNEL 时,该值为频道所属社群的 communityID。
在会话列表场景中,您可以直接通过 relatedID 定位频道会话所属的社群,用于查询社群公共信息、拉取社群成员列表等场景,无需自行维护 conversationID 与 communityID 的映射关系。
当前版本中,仅当会话类型为 COMMUNITY_CHANNEL 时,relatedID 的含义有明确定义。
监听社群频道会话更新
频道会话的新增、更新与删除,与单聊、群聊会话一样通过 conversationChanged 回调通知。您可以在回调中筛选出类型为 COMMUNITY_CHANNEL 的会话,更新社群频道在会话列表中的展示。
示例代码
// 注册 SDK 事件通知回调
zim.setEventHandler(new ZIMEventHandler() {
@Override
public void onConversationChanged(ZIM zim, ArrayList<ZIMConversationChangeInfo> conversationChangeInfoList) {
for (ZIMConversationChangeInfo changeInfo : conversationChangeInfoList) {
ZIMConversation conversation = changeInfo.conversation;
if (conversation.type != ZIMConversationType.COMMUNITY_CHANNEL) {
// 非频道会话,按原有逻辑处理
continue;
}
// changeInfo.action:ADDED(新增会话)、UPDATED(会话更新)、DELETED(会话删除)
String conversationID = conversation.conversationID; // 频道会话 ID
String communityID = conversation.relatedID; // 该频道会话所属的社群 ID
int unreadMessageCount = conversation.unreadMessageCount;
String draft = conversation.draft;
}
}
});
// 注册 SDK 事件通知回调
zim.setEventHandler(new ZIMEventHandler() {
@Override
public void onConversationChanged(ZIM zim, ArrayList<ZIMConversationChangeInfo> conversationChangeInfoList) {
for (ZIMConversationChangeInfo changeInfo : conversationChangeInfoList) {
ZIMConversation conversation = changeInfo.conversation;
if (conversation.type != ZIMConversationType.COMMUNITY_CHANNEL) {
// 非频道会话,按原有逻辑处理
continue;
}
// changeInfo.action:ADDED(新增会话)、UPDATED(会话更新)、DELETED(会话删除)
String conversationID = conversation.conversationID; // 频道会话 ID
String communityID = conversation.relatedID; // 该频道会话所属的社群 ID
int unreadMessageCount = conversation.unreadMessageCount;
String draft = conversation.draft;
}
}
});
示例代码
// 注册 SDK 事件通知回调
[self.zim setEventHandler:self];
...
- (void)zim:(ZIM *)zim
conversationChanged:(NSArray<ZIMConversationChangeInfo *> *)conversationChangeInfoList {
for (ZIMConversationChangeInfo *changeInfo in conversationChangeInfoList) {
ZIMConversation *conversation = changeInfo.conversation;
if (conversation.type != ZIMConversationTypeCommunityChannel) {
// 非频道会话,按原有逻辑处理
continue;
}
// changeInfo.action:Added(新增会话)、Updated(会话更新)、Deleted(会话删除)
NSString *conversationID = conversation.conversationID; // 频道会话 ID
NSString *communityID = conversation.relatedID; // 该频道会话所属的社群 ID
unsigned int unreadMessageCount = conversation.unreadMessageCount;
NSString *draft = conversation.draft;
}
}
// 注册 SDK 事件通知回调
[self.zim setEventHandler:self];
...
- (void)zim:(ZIM *)zim
conversationChanged:(NSArray<ZIMConversationChangeInfo *> *)conversationChangeInfoList {
for (ZIMConversationChangeInfo *changeInfo in conversationChangeInfoList) {
ZIMConversation *conversation = changeInfo.conversation;
if (conversation.type != ZIMConversationTypeCommunityChannel) {
// 非频道会话,按原有逻辑处理
continue;
}
// changeInfo.action:Added(新增会话)、Updated(会话更新)、Deleted(会话删除)
NSString *conversationID = conversation.conversationID; // 频道会话 ID
NSString *communityID = conversation.relatedID; // 该频道会话所属的社群 ID
unsigned int unreadMessageCount = conversation.unreadMessageCount;
NSString *draft = conversation.draft;
}
}
示例代码
// 注册 SDK 事件通知回调
class ZIMEventHandlerImpl : public zim::ZIMEventHandler {
void onConversationChanged(
zim::ZIM * /*zim*/,
const std::vector<zim::ZIMConversationChangeInfo> &conversationChangeInfoList) override {
for (const auto &changeInfo : conversationChangeInfoList) {
const auto &conversation = changeInfo.conversation;
if (!conversation ||
conversation->type != zim::ZIM_CONVERSATION_TYPE_COMMUNITY_CHANNEL) {
// 非频道会话,按原有逻辑处理
continue;
}
// changeInfo.action:ADDED(新增会话)、UPDATED(会话更新)、DELETED(会话删除)
std::string conversationID = conversation->conversationID; // 频道会话 ID
std::string communityID = conversation->relatedID; // 该频道会话所属的社群 ID
unsigned int unreadMessageCount = conversation->unreadMessageCount;
std::string draft = conversation->draft;
}
}
};
// 注册 SDK 事件通知回调
class ZIMEventHandlerImpl : public zim::ZIMEventHandler {
void onConversationChanged(
zim::ZIM * /*zim*/,
const std::vector<zim::ZIMConversationChangeInfo> &conversationChangeInfoList) override {
for (const auto &changeInfo : conversationChangeInfoList) {
const auto &conversation = changeInfo.conversation;
if (!conversation ||
conversation->type != zim::ZIM_CONVERSATION_TYPE_COMMUNITY_CHANNEL) {
// 非频道会话,按原有逻辑处理
continue;
}
// changeInfo.action:ADDED(新增会话)、UPDATED(会话更新)、DELETED(会话删除)
std::string conversationID = conversation->conversationID; // 频道会话 ID
std::string communityID = conversation->relatedID; // 该频道会话所属的社群 ID
unsigned int unreadMessageCount = conversation->unreadMessageCount;
std::string draft = conversation->draft;
}
}
};
示例代码
// 注册 SDK 事件通知回调
zim.on('conversationChanged', (zim: ZIM, data: ZIMConversationChangedEventResult) => {
data.infoList.forEach((info) => {
const conversation = info.conversation;
if (conversation.type !== ZIMConversationType.CommunityChannel) {
// 非频道会话,按原有逻辑处理
return;
}
// info.action:Added(新增会话)、Deleted(会话删除)、Updated(会话更新)
const conversationID = conversation.conversationID; // 频道会话 ID
const communityID = conversation.relatedID; // 该频道会话所属的社群 ID
const unreadMessageCount = conversation.unreadMessageCount;
const draft = conversation.draft;
});
});
// 注册 SDK 事件通知回调
zim.on('conversationChanged', (zim: ZIM, data: ZIMConversationChangedEventResult) => {
data.infoList.forEach((info) => {
const conversation = info.conversation;
if (conversation.type !== ZIMConversationType.CommunityChannel) {
// 非频道会话,按原有逻辑处理
return;
}
// info.action:Added(新增会话)、Deleted(会话删除)、Updated(会话更新)
const conversationID = conversation.conversationID; // 频道会话 ID
const communityID = conversation.relatedID; // 该频道会话所属的社群 ID
const unreadMessageCount = conversation.unreadMessageCount;
const draft = conversation.draft;
});
});
示例代码
// 注册 SDK 事件通知回调
ZIMEventHandler.onConversationChanged =
(ZIM zim, List<ZIMConversationChangeInfo> conversationChangeInfoList) {
for (final ZIMConversationChangeInfo changeInfo in conversationChangeInfoList) {
final ZIMConversation? conversation = changeInfo.conversation;
if (conversation?.type != ZIMConversationType.communityChannel) {
// 非频道会话,按原有逻辑处理
continue;
}
// changeInfo.action:added(新增会话)、updated(会话更新)、deleted(会话删除)
final conversationID = conversation!.conversationID; // 频道会话 ID
final communityID = conversation.relatedID; // 该频道会话所属的社群 ID
final unreadMessageCount = conversation.unreadMessageCount;
final draft = conversation.draft;
}
};
// 注册 SDK 事件通知回调
ZIMEventHandler.onConversationChanged =
(ZIM zim, List<ZIMConversationChangeInfo> conversationChangeInfoList) {
for (final ZIMConversationChangeInfo changeInfo in conversationChangeInfoList) {
final ZIMConversation? conversation = changeInfo.conversation;
if (conversation?.type != ZIMConversationType.communityChannel) {
// 非频道会话,按原有逻辑处理
continue;
}
// changeInfo.action:added(新增会话)、updated(会话更新)、deleted(会话删除)
final conversationID = conversation!.conversationID; // 频道会话 ID
final communityID = conversation.relatedID; // 该频道会话所属的社群 ID
final unreadMessageCount = conversation.unreadMessageCount;
final draft = conversation.draft;
}
};
监听频道列表变更
当社群中频道被创建或解散或列表可见信息变更时,频道列表会随之变化。SDK 会通过 onCommunityChannelListChanged 回调通知开发者,开发者可在该回调中重新获取频道列表,更新本地的 conversationID 映射关系。详见 社群频道管理 — 监听频道列表变更。
相关参考