会话分组
功能描述
在某些场景下,您可能需要对会话进行分组,例如分为 "产品体验"、"需求研发" 等,您可以调用以下接口实现。
效果展示
使用本功能,您可以在您的 App 中实现如下的会话分组效果:
接口说明
新建会话分组
说明
会话分组最大支持 20 个,超过后创建新的分组会报
51010
错误,不再使用的分组请及时删除。属性 | 含义 | 说明 |
groupName | 会话分组名 | 长度要大于 0,最大支持 32 bytes,超过后会报 51011 错误 |
conversationIDList | 会话 ID 列表 | 不能为空 |
示例代码如下:
List<String> conversationIDList = new ArrayList<>();conversationIDList.add("c2c_user1");V2TIMManager.getConversationManager().createConversationGroup("conversation_group", conversationIDList, new V2TIMValueCallback<List<V2TIMConversationOperationResult>>() {@Overridepublic void onSuccess(List<V2TIMConversationOperationResult> v2TIMConversationOperationResults) {// 创建会话分组成功}@Overridepublic void onError(int code, String desc) {// 创建会话分组失败}});
// 创建会话分组[[V2TIMManager sharedInstance] createConversationGroup:@"conversation_group" conversationIDList:@[@"c2c_yahaha"] succ:^(NSArray<V2TIMConversationOperationResult *> *result) {// 创建会话分组成功} fail:^(int code, NSString *desc) {// 创建会话分组失败}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMString groupName = u8"conversation_group";V2TIMStringVector conversationIDList;conversationIDList.PushBack(u8"c2c_user1");auto callback = new ValueCallback<V2TIMConversationOperationResultVector>{};callback->SetCallback([=](const V2TIMConversationOperationResultVector& conversationOperationResultList) {// 创建会话分组成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 创建会话分组失败delete callback;});V2TIMManager::GetInstance()->GetConversationManager()->CreateConversationGroup(groupName, conversationIDList,callback);
删除会话分组
说明
如果会话分组不存在,删除对应分组会报
51009
错误。示例代码如下:
V2TIMManager.getConversationManager().deleteConversationGroup("conversation_group", new V2TIMCallback() {@Overridepublic void onSuccess() {// 删除会话分组成功}@Overridepublic void onError(int code, String desc) {// 删除会话分组失败}});
// 删除会话分组[[V2TIMManager sharedInstance] deleteConversationGroup:@"conversation_group" succ:^{// 删除会话分组成功} fail:^(int code, NSString *desc) {// 删除会话分组失败}];
class Callback final : public V2TIMCallback {public:using SuccessCallback = std::function<void()>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;Callback() = default;~Callback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess() override {if (success_callback_) {success_callback_();}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMString groupName = u8"conversation_group";auto callback = new Callback;callback->SetCallback([=]() {// 删除会话分组成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 删除会话分组失败delete callback;});V2TIMManager::GetInstance()->GetConversationManager()->DeleteConversationGroup(groupName, callback);
重命名会话分组
示例代码如下:
V2TIMManager.getConversationManager().renameConversationGroup("conversation_group", "conversation_group_rename", new V2TIMCallback() {@Overridepublic void onSuccess() {// 重命名会话分组成功}@Overridepublic void onError(int code, String desc) {// 重命名会话分组失败}});
// 重命名会话分组[[V2TIMManager sharedInstance] renameConversationGroup:@"conversation_group" newName:@"conversation_group_rename" succ:^{// 重命名会话分组成功} fail:^(int code, NSString *desc) {// 重命名会话分组失败}];
class Callback final : public V2TIMCallback {public:using SuccessCallback = std::function<void()>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;Callback() = default;~Callback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess() override {if (success_callback_) {success_callback_();}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMString oldName = u8"conversation_group";V2TIMString newName = u8"conversation_group_rename";auto callback = new Callback;callback->SetCallback([=]() {// 重命名会话分组成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 重命名会话分组失败delete callback;});V2TIMManager::GetInstance()->GetConversationManager()->RenameConversationGroup(oldName, newName, callback);
获取会话分组列表
示例代码如下:
V2TIMManager.getConversationManager().getConversationGroupList(new V2TIMValueCallback<List<String>>() {@Overridepublic void onSuccess(List<String> strings) {// 获取分组列表成功}@Overridepublic void onError(int code, String desc) {// 获取分组列表失败}});
// 获取会话分组列表[[V2TIMManager sharedInstance] getConversationGroupList:^(NSArray<NSString *> *groupList) {// 获取分组列表成功} fail:^(int code, NSString *desc) {// 获取分组列表失败}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};auto callback = new ValueCallback<V2TIMStringVector>{};callback->SetCallback([=](const V2TIMStringVector& stringList) {// 获取分组列表成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 获取分组列表失败delete callback;});V2TIMManager::GetInstance()->GetConversationManager()->GetConversationGroupList(callback);
示例代码如下:
V2TIMConversationListFilter filter = new V2TIMConversationListFilter();filter.setGroupName("conversation_group");filter.setCount(50);filter.setNextSeq(0);V2TIMManager.getConversationManager().getConversationListByFilter(filter, new V2TIMValueCallback<V2TIMConversationResult>() {@Overridepublic void onSuccess(V2TIMConversationResult v2TIMConversationResult) {// 获取会话列表成功}@Overridepublic void onError(int code, String desc) {// 获取会话列表失败}});
// 拉取指定标记会话V2TIMConversationListFilter *filter = [[V2TIMConversationListFilter alloc] init];filter.groupName = @"conversation_group";filter.count = 50;filter.nextSeq = 0;[[V2TIMManager sharedInstance] getConversationListByFilter:filter succ:^(NSArray<V2TIMConversation *> *list, uint64_t nextSeq, BOOL isFinished) {// 获取会话列表成功,list 为会话列表} fail:^(int code, NSString *desc) {// 获取会话列表失败}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMConversationListFilter filter;filter.nextSeq = 0;filter.count = 50;filter.groupName = u8"conversation_group";auto callback = new ValueCallback<V2TIMConversationResult>{};callback->SetCallback([=](const V2TIMConversationResult& conversationResult) {// 获取会话列表成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 获取会话列表失败delete callback;});V2TIMManager::GetInstance()->GetConversationManager()->GetConversationListByFilter(filter, callback);
添加会话到一个分组
示例代码如下:
List<String> conversationIDList = new ArrayList<>();conversationIDList.add("c2c_user2");V2TIMManager.getConversationManager().addConversationsToGroup("conversation_group", conversationIDList, new V2TIMValueCallback<List<V2TIMConversationOperationResult>>() {@Overridepublic void onSuccess(List<V2TIMConversationOperationResult> v2TIMConversationOperationResults) {// 添加会话到分组成功}@Overridepublic void onError(int code, String desc) {// 添加会话到分组失败}});
// 添加会话到一个分组[[V2TIMManager sharedInstance] addConversationsToGroup:@"conversation_group" conversationIDList:@[@"c2c_yahaha"] succ:^(NSArray<V2TIMConversationOperationResult *> *result) {// 添加会话到分组成功} fail:^(int code, NSString *desc) {// 添加会话到分组失败}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMString groupName = u8"conversation_group";V2TIMStringVector conversationIDList;conversationIDList.PushBack(u8"c2c_user1");auto callback = new ValueCallback<V2TIMConversationOperationResultVector>{};callback->SetCallback([=](const V2TIMConversationOperationResultVector& conversationOperationResultList) {// 添加会话到分组成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 添加会话到分组失败delete callback;});V2TIMManager::GetInstance()->GetConversationManager()->AddConversationsToGroup(groupName, conversationIDList,callback);
从分组中删除某会话
示例代码如下:
List<String> conversationIDList = new ArrayList<>();conversationIDList.add("c2c_user2");V2TIMManager.getConversationManager().deleteConversationsFromGroup("conversation_group", conversationIDList, new V2TIMValueCallback<List<V2TIMConversationOperationResult>>() {@Overridepublic void onSuccess(List<V2TIMConversationOperationResult> v2TIMConversationOperationResults) {// 从分组删除会话成功}@Overridepublic void onError(int code, String desc) {// 从分组删除会话失败}});
// 从分组中删除某会话[[V2TIMManager sharedInstance] deleteConversationsFromGroup:@"conversation_group" conversationIDList:@[@"c2c_yahaha"] succ:^(NSArray<V2TIMConversationOperationResult *> *result) {// 从分组删除会话成功} fail:^(int code, NSString *desc) {// 从分组删除会话失败}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMString groupName = u8"conversation_group";V2TIMStringVector conversationIDList;conversationIDList.PushBack(u8"c2c_user1");auto callback = new ValueCallback<V2TIMConversationOperationResultVector>{};callback->SetCallback([=](const V2TIMConversationOperationResultVector& conversationOperationResultList) {// 从分组删除会话成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 从分组删除会话失败delete callback;});V2TIMManager::GetInstance()->GetConversationManager()->DeleteConversationsFromGroup(groupName, conversationIDList, callback);
监听会话分组变更通知
示例代码如下:
V2TIMConversationListener listener = new V2TIMConversationListener() {@Overridepublic void onConversationGroupCreated(String groupName, List<V2TIMConversation> conversationList) {// 收到会话分组创建通知}@Overridepublic void onConversationGroupDeleted(String groupName) {// 收到会话分组删除通知}@Overridepublic void onConversationGroupNameChanged(String oldName, String newName) {// 收到会话分组重命名通知}@Overridepublic void onConversationsAddedToGroup(String groupName, List<V2TIMConversation> conversationList) {// 收到会话分组添加会话通知}@Overridepublic void onConversationsDeletedFromGroup(String groupName, List<V2TIMConversation> conversationList, int reason) {// 收到会话分组删除会话通知// reason 表示会话从所在分组删除的原因,其取值有:// 当 reason 为 0 时,表示由用户主动调用 deleteConversationsFromGroup 触发// 当 reason 为 1 时,表示添加到分组的会话数量超过 1000,最早添加进分组的会话被淘汰(当会话数超限时,添加新会话到分组默认会报错,如需添加成功并淘汰最早的会话,请提工单联系客服配置)}};V2TIMManager.getConversationManager().addConversationListener(listener);
[[V2TIMManager sharedInstance] addConversationListener:self];- (void)onConversationGroupCreated:(NSString *)groupName conversationList:(NSArray<V2TIMConversation *> *)conversationList {// 收到会话分组创建通知}- (void)onConversationGroupDeleted:(NSString *)groupName {// 收到会话分组删除通知}- (void)onConversationGroupNameChanged:(NSString *)oldName newName:(NSString *)newName {// 收到会话分组重命名通知}- (void)onConversationsAddedToGroup:(NSString *)groupName conversationList:(NSArray<V2TIMConversation *> *)conversationList {// 收到会话分组添加会话通知}- (void)onConversationsDeletedFromGroup:(NSString *)groupName conversationList:(NSArray<V2TIMConversation *> *)conversationList reason:(uint32_t)reason {// 收到会话分组删除会话通知// reason 表示会话从所在分组删除的原因,其取值有:// 当 reason 为 0 时,表示由用户主动调用 deleteConversationsFromGroup 触发// 当 reason 为 1 时,表示添加到分组的会话数量超过 1000,最早添加进分组的会话被淘汰}
class ConversationListener final : public V2TIMConversationListener {public:ConversationListener() = default;~ConversationListener() override = default;void OnConversationGroupCreated(const V2TIMString& groupName,const V2TIMConversationVector& conversationList) override {// 收到会话分组创建通知}void OnConversationGroupDeleted(const V2TIMString& groupName) override {// 收到会话分组删除通知}void OnConversationGroupNameChanged(const V2TIMString& oldName, const V2TIMString& newName) override {// 收到会话分组重命名通知}void OnConversationsAddedToGroup(const V2TIMString& groupName,const V2TIMConversationVector& conversationList) override {// 收到会话分组添加会话通知}void OnConversationsDeletedFromGroup(const V2TIMString& groupName,const V2TIMConversationVector& conversationList,uint32_t reason) override {// 收到会话分组删除会话通知// reason 表示会话从所在分组删除的原因,其取值有:// 当 reason 为 0 时,表示由用户主动调用 deleteConversationsFromGroup 触发// 当 reason 为 1 时,表示添加到分组的会话数量超过 1000,最早添加进分组的会话被淘汰}};// 添加会话事件监听器,注意在移除监听器之前需要保持 conversationListener 的生命期,以免接收不到事件回调ConversationListener conversationListener;V2TIMManager::GetInstance()->GetConversationManager()->AddConversationListener(&conversationListener);