Android&iOS&Windows&Mac

最近更新时间:2024-10-10 13:01:07

我的收藏

功能描述

IM SDK 提供获取会话的接口,可以获取指定的单个、多个会话的 V2TIMConversation 对象信息。

获取指定的单个会话

您可以调用 getConversation(Android / iOS & Mac / Windows) 获取单个会话的信息,会话信息是一个 V2TIMConversation 对象。
示例代码如下:
Android
iOS & Mac
Windows
String conversationID = "conversationID";
V2TIMManager.getConversationManager().getConversation(conversationID, new V2TIMValueCallback<V2TIMConversation>() {
@Override
public void onSuccess(V2TIMConversation v2TIMConversation) {
Log.i("imsdk", "success");
}

@Override
public void onError(int code, String desc) {
Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);
}
});
[V2TIMManager.sharedInstance getConversation:@"conversationID" // 要查询的会话 ID
succ:^(V2TIMConversation *conv) {
// 获取成功,返回 V2TIMConversation 对象
} 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 conversationID = u8"conversationID";

auto callback = new ValueCallback<V2TIMConversation>{};
callback->SetCallback(
[=](const V2TIMConversation& conversation) {
// 获取单个会话成功
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// 获取单个会话失败
delete callback;
});

V2TIMManager::GetInstance()->GetConversationManager()->GetConversation(conversationID, callback);

获取指定的多个会话

getConversationList(Android / iOS & Mac / Windows) 获取指定的会话列表,列表中存储的是 V2TIMConversation 对象。
示例代码如下:
Android
iOS & Mac
Windows
List<String> conversationIDList = new ArrayList<>();
conversationIDList.add("conversationID1");
conversationIDList.add("conversationID1");

V2TIMManager.getConversationManager().getConversationList(conversationIDList, new V2TIMValueCallback<List<V2TIMConversation>>() {
@Override
public void onSuccess(List<V2TIMConversation> conversationList) {
Log.i("imsdk", "success");
}

@Override
public void onError(int code, String desc) {
Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);
}
});
[V2TIMManager.sharedInstance getConversationList:@[@"convID1", @"convID2"] // 要查询的会话 ID 列表
succ:^(NSArray<V2TIMConversation *> *list) {
// 获取成功,list 中包含 V2TIMConversation 对象
} 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"conversationID1");
conversationIDList.PushBack(u8"conversationID2");

auto callback = new ValueCallback<V2TIMConversationVector>{};
callback->SetCallback(
[=](const V2TIMConversationVector& conversationList) {
// 获取多个会话成功
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// 获取多个会话失败
delete callback;
});

V2TIMManager::GetInstance()->GetConversationManager()->GetConversationList(conversationIDList, callback);