删除会话

功能描述

删除好友或退出群组后,SDK 不会自动删除对应的单聊或群聊会话。客户可以调用下文的接口,主动删除会话。
会话删除默认不支持多端同步,您可以在 即时通信 IM 控制台 开启多端同步。配置页面如下图所示:



说明
删除会话多端同步功能仅 SDK 5.1.1 及以上版本支持。

删除单个会话

您可以调用 deleteConversation(Android / iOS & Mac / Windows) 接口,删除指定的会话。
注意:
删除会话会同时删除终端和服务器的历史消息,且无法恢复。如果您希望保留历史消息,请使用 删除多个会话接口。
示例代码如下:
Android
iOS & Mac
Windows
String conversationID = "conversationID";
V2TIMManager.getConversationManager().deleteConversation(conversationID, new V2TIMCallback() {
@Override
public void onSuccess() {
// 删除会话成功
}
@Override
public void onError(int code, String desc) {
// 删除会话失败
}
});
NSString *conversationID = @"conversationID";
[[V2TIMManager sharedInstance] deleteConversation:conversationID
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"conversationID";
auto callback = new Callback;
callback->SetCallback(
[=]() {
// 删除会话成功
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// 删除会话失败
delete callback;
});
V2TIMManager::GetInstance()->GetConversationManager()->DeleteConversationGroup(groupName, callback);

删除多个会话

您可以调用 deleteConversationList(Android / iOS & Mac / Windows) 接口,删除指定的会话。clearMessage 设置为 false 时,会保留会话消息;设置为 true 时,本地和服务器的消息会一起删除,并且不可恢复。
说明:
仅 SDK 7.1 及以上版本支持。
每次最多支持删除 100 个会话。
示例代码如下:
Android
iOS & Mac
Windows
List<String> conversationIDList = new ArrayList<>();
conversationIDList.add("c2c_userID");
conversationIDList.add("group_groupID");
V2TIMManager.getConversationManager().deleteConversationList(
conversationIDList, true, new V2TIMValueCallback<List<V2TIMConversationOperationResult>>() {
@Override
public void onSuccess(List<V2TIMConversationOperationResult> results) {
// 删除会话成功
for (V2TIMConversationOperationResult result : results) {
int code = result.getResultCode();
String info = result.getResultInfo();
String conversationID = result.getConversationID();
}
}
@Override
public void onError(int code, String desc) {
// 删除会话失败
}
});
NSMutableArray *conversationIDList = [NSMutableArray array];
[conversationIDList addObject:@"c2c_userID"];
[conversationIDList addObject:@"group_groupID"];
[[V2TIMManager sharedInstance] deleteConversationList:conversationIDList
clearMessage:true
succ:^(NSArray<V2TIMConversationOperationResult *> *results) {
// 删除会话成功
for (V2TIMConversationOperationResult *result in results) {
int code = result.resultCode;
NSString *info = result.resultInfo;
NSString *conversationID = result.conversationID;
}
}
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_;
};

V2TIMStringVector conversationIDList;
conversationIDList.PushBack(u8"c2c_userID");
conversationIDList.PushBack(u8"group_groupID");
auto callback = new ValueCallback<V2TIMConversationOperationResultVector>{};
callback->SetCallback(
[=](const V2TIMConversationOperationResultVector& results) {
// 删除会话成功
for (size_t i = 0; i < results.Size(); ++i) {
const V2TIMConversationOperationResult& result = results[i];
int code = result.resultCode;
V2TIMString info = result.resultInfo;
V2TIMString conversationID = result.conversationID;
}
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// 删除会话失败
delete callback;
});
V2TIMManager::GetInstance()->GetConversationManager()->DeleteConversationList(conversationIDList, true, callback);

会话被删除通知

如果您事先调用了 addConversationListener(Android / iOS & Mac / Windows) 添加会话监听器,就可以在 onConversationDeleted 中获取会话被删除的通知。
示例代码如下:
Android
iOS & Mac
Windows
@Override
public void onConversationDeleted(List<String> conversationIDList) {
Log.i("imsdk", "onConversationDeleted");
}
- (void)onConversationDeleted:(NSArray<NSString *> *)conversationIDList {
// 会话被删除
}
void OnConversationDeleted(const V2TIMStringVector &conversationIDList) override {
// 收到会话被删除通知
}