Android&iOS&Windows&Mac

最近更新时间:2024-08-13 10:54:31

我的收藏

功能描述

社群是一个由于共同主题而让大家聚集在一起的超大群组,可以在社群下根据不同兴趣创建多个话题。 社群用来管理群成员。社群下的所有话题不仅可以共享社群成员,还可以独立收发消息而不相互干扰。
社群和话题的管理接口在核心类 V2TIMCommunityManager(Android) / V2TIMManager(Community)(iOS & Mac) 中。
话题中的消息相关接口在核心类 V2TIMMessageManager(Android) / V2TIMManager(Message)(iOS & Mac) 中。
说明:
6.2.2363 及以上版本支持,从 7.7.5282 版本开始推荐使用 V2TIMCommunityManager 和 V2TIMCommunityListener 类。
需要 购买旗舰版套餐包 并在 控制台 > 功能配置 > 群组配置 > 群功能配置 > 社群打开开关后方可使用。

效果展示

您可以使用本功能实现如下图所示的社群效果:




社群管理

创建社群

调用 createCommunity (Android / iOS & Mac / Windows) 创建支持话题的社群。
说明:
自定义的社群 ID 前缀必须是@TGS#_。
示例代码如下:
Android
iOS & Mac
Windows
V2TIMGroupInfo v2TIMGroupInfo = new V2TIMGroupInfo();
v2TIMGroupInfo.setGroupName("This is a Community");
v2TIMGroupInfo.setGroupType(V2TIMManager.GROUP_TYPE_COMMUNITY);
V2TIMManager.getCommunityManager().createCommunity(v2TIMGroupInfo, null, new V2TIMValueCallback<String>() {
@Override
public void onSuccess(String groupID) {
// 创建社群成功
}

@Override
public void onError(int code, String desc) {
// 创建社群失败
}
});
V2TIMGroupInfo *groupInfo = [[V2TIMGroupInfo alloc] init];;
groupInfo.groupName = @"This is a Community";
groupInfo.groupType = GroupType_Community;
[[V2TIMManager sharedInstance] createCommunity:groupInfo memberList:nil succ:^(NSString *groupID) {
// 创建社群成功
} 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_;
};

V2TIMGroupInfo info;
info.groupType = "Community";
info.groupName = "This is a Community";

auto callback = new ValueCallback<V2TIMString>{};
callback->SetCallback(
[=](const V2TIMString& groupID) {
// 创建社群成功
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// 创建社群失败
delete callback;
});

V2TIMManager::GetInstance()->GetCommunityManager()->CreateCommunity(info, {}, callback);

获取加入的社群列表

调用 getJoinedCommunityListAndroid / iOS & Mac / Windows)获取已经加入的支持话题的社群列表。
示例代码如下:
Android
iOS & Mac
Windows
V2TIMManager.getCommunityManager().getJoinedCommunityList(new V2TIMValueCallback<List<V2TIMGroupInfo>>() {
@Override
public void onSuccess(List<V2TIMGroupInfo> v2TIMGroupInfos) {
// 获取社群列表成功
}
@Override
public void onError(int code, String desc) {
// 获取社群列表失败
}
});

[[V2TIMManager sharedInstance] getJoinedCommunityList:^(NSArray<V2TIMGroupInfo *> *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<V2TIMGroupInfoVector>{};
callback->SetCallback(
[=](const V2TIMGroupInfoVector& groupInfoList) {
// 获取社群列表成功
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// 获取社群列表失败
delete callback;
});

V2TIMManager::GetInstance()->GetCommunityManager()->GetJoinedCommunityList(callback);

其他管理接口

其他功能跟普通群组使用方法一样,涉及的接口有:
分类
功能
接口
社群管理
joinGroup(Android / iOS & Mac
quitGroup(Android / iOS & Mac
dismissGroup(Android / iOS & Mac
getGroupsInfo(Android / iOS & Mac
setGroupInfo(Android / iOS & Mac
社群成员管理
getGroupMemberList(Android / iOS & Mac
getGroupMembersInfo(Android / iOS & Mac
setGroupMemberInfo (Android / iOS & Mac
kickGroupMember(Android / iOS & Mac

话题管理

同一社群下可创建多个话题,多个话题共享一套社群成员关系,但不同话题内独立收发消息、互不干扰。
说明:
需在 控制台 > 功能配置 > 群组配置 > 群功能配置 > 社群页面,完成开通社群后,再打开开通话题开关,方可使用。

创建话题

创建话题分两步:
1. 创建 V2TIMTopicInfoAndroid / iOS & Mac / Windows)对象。
2. 调用 createTopicInCommunityAndroid / iOS & Mac / Windows)接口创建话题。
说明:
自定义的话题 ID 组成为:GroupId+@TOPIC#_+自定义部分。例如社群 ID 为 @TGS#_123,自定义部分为 TestTopic,那么话题 ID 为 @TGS#_123@TOPIC#_TestTopic。
示例代码如下:
Android
iOS & Mac
Windows
V2TIMTopicInfo topicInfo = new V2TIMTopicInfo();
topicInfo.setTopicName(topicName);
topicInfo.setTopicFaceUrl(topicFaceUrl);
topicInfo.setIntroduction(topicIntroduction);
topicInfo.setNotification(topicNotification);
topicInfo.setCustomString(topicCustomString);

// groupID 填支持话题的社群 ID
V2TIMManager.getCommunityManager().createTopicInCommunity(groupID, topicInfo, new V2TIMValueCallback<String>() {
@Override
public void onSuccess(String topicID) {
// 创建话题成功
}

@Override
public void onError(int code, String desc) {
// 创建话题失败
}
});

V2TIMTopicInfo *topicInfo = [[V2TIMTopicInfo alloc] init];
topicInfo.topicName = @"topicName";
topicInfo.topicFaceURL = @"topicFaceUrl";
topicInfo.introduction = @"topicIntroduction";
topicInfo.notification = @"topicNotification";
topicInfo.customString = @"topicCustomString";

// groupID 填支持话题的社群 ID
[[V2TIMManager sharedInstance] createTopicInCommunity:@"groupID" topicInfo:topicInfo succ:^(NSString *topicID) {
// 创建话题成功
} 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_;
};

V2TIMTopicInfo topicInfo;
topicInfo.topicID = "topicID";
topicInfo.topicName = "topicName";
topicInfo.topicFaceURL = "topicFaceURL";
topicInfo.introduction = "introduction";
topicInfo.notification = "notification";

auto callback = new ValueCallback<V2TIMString>{};
callback->SetCallback(
[=](const V2TIMString& string) {
// 创建话题成功
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// 创建话题失败
delete callback;
});

// groupID 填支持话题的社群 ID
V2TIMManager::GetInstance()->GetCommunityManager()->CreateTopicInCommunity("groupID", topicInfo, callback);

删除话题

调用 deleteTopicFromCommunityAndroid / iOS & Mac / Windows)接口删除话题。
示例代码如下:
Android
iOS & Mac
Windows
V2TIMManager.getCommunityManager().deleteTopicFromCommunity(groupID, topicIDList, new V2TIMValueCallback<List<V2TIMTopicOperationResult>>() {
@Override
public void onSuccess(List<V2TIMTopicOperationResult> v2TIMTopicOperationResults) {
// 删除话题成功
}

@Override
public void onError(int code, String desc) {
// 删除话题失败
}
});

[[V2TIMManager sharedInstance] deleteTopicFromCommunity:@"groupID" topicIDList:@[@"topic1", @"topic2"] succ:^(NSMutableArray<V2TIMTopicOperationResult *> *resultList) {
// 删除话题成功
} 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 topicIDList;
topicIDList.PushBack("topic1");
topicIDList.PushBack("topic2");

auto callback = new ValueCallback<V2TIMTopicOperationResultVector>{};
callback->SetCallback(
[=](const V2TIMTopicOperationResultVector& topicOperationResultList) {
// 删除话题成功
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// 删除话题失败
delete callback;
});

V2TIMManager::GetInstance()->GetCommunityManager()->DeleteTopicFromCommunity("groupID", topicIDList, callback);

修改话题信息

修改话题信息分两步:
1. 创建 V2TIMTopicInfoAndroid / iOS & Mac / Windows)对象,并设置需要修改的字段。
2. 调用 setTopicInfoAndroid / iOS & Mac / Windows)接口修改话题信息。
示例代码如下:
Android
iOS & Mac
Windows
V2TIMTopicInfo topicInfo = new V2TIMTopicInfo();
topicInfo.setTopicID(topicID);
topicInfo.setTopicName(topicName);
topicInfo.setTopicFaceUrl(topicFaceUrl);
topicInfo.setIntroduction(topicIntroduction);
topicInfo.setNotification(topicNotification);
topicInfo.setCustomString(topicCustomString);
topicInfo.setDraft(topicDraft);
topicInfo.setAllMute(false);
V2TIMManager.getCommunityManager().setTopicInfo(topicInfo, new V2TIMCallback() {
@Override
public void onSuccess() {
// 修改话题信息成功
}

@Override
public void onError(int code, String desc) {
// 修改话题信息失败
}
});

V2TIMTopicInfo *topicInfo = [[V2TIMTopicInfo alloc] init];
topicInfo.topicID = @"topicID";
topicInfo.topicName = @"topicName";
topicInfo.topicFaceURL = @"topicFaceUrl";
topicInfo.introduction = @"topicIntroduction";
topicInfo.notification = @"topicNotification";
topicInfo.customString = @"topicCustomString";
topicInfo.draftText = @"topicDraft";
topicInfo.isAllMuted = NO;
[[V2TIMManager sharedInstance] setTopicInfo:topicInfo 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_;
};

V2TIMTopicInfo topicInfo;
topicInfo.topicID = "topicID";
topicInfo.topicName = "topicName";
topicInfo.notification = "topicFaceURL";
topicInfo.introduction = "introduction";
topicInfo.topicFaceURL = "notification";
topicInfo.customString = "customString";
topicInfo.modifyFlag = V2TIMTopicInfoModifyFlag::V2TIM_COMMUNITY_MODIFY_FLAG_TOPIC_NAME |
V2TIMTopicInfoModifyFlag::V2TIM_COMMUNITY_MODIFY_FLAG_TOPIC_NOTIFICATION |
V2TIMTopicInfoModifyFlag::V2TIM_COMMUNITY_MODIFY_FLAG_TOPIC_INTRODUCTION |
V2TIMTopicInfoModifyFlag::V2TIM_COMMUNITY_MODIFY_FLAG_TOPIC_FACE_URL |
V2TIMTopicInfoModifyFlag::V2TIM_COMMUNITY_MODIFY_FLAG_TOPIC_CUSTOM_STRING;

auto callback = new Callback;
callback->SetCallback(
[=]() {
// 修改话题信息成功
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// 修改话题信息失败
delete callback;
});

V2TIMManager::GetInstance()->GetCommunityManager()->SetTopicInfo(topicInfo, callback);
修改话题其他信息可参见 禁言成员修改话题消息接收选项
话题的消息接收选项可以选择是否继承自社群,接口为 setInheritMessageReceiveOptionFromCommunityAndroid / iOS & Mac / Windows)。
示例代码如下:
Android
iOS & Mac
Windows
V2TIMTopicInfo topicInfo = new V2TIMTopicInfo();
topicInfo.setTopicID(topicID);
topicInfo.setInheritMessageReceiveOptionFromCommunity(true, new V2TIMCallback() { @Override public void onSuccess() { // 继承成功 } @Override public void onError(int code, String desc) { // 继承失败 } });

V2TIMTopicInfo *topicInfo = [[V2TIMTopicInfo alloc] init];
topicInfo.topicID = @"topicID";
[topicInfo setInheritMessageReceiveOptionFromCommunity:YES 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_;
};

auto callback = new Callback;
callback->SetCallback(
[=]() {
// 继承成功
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// 继承失败
delete callback;
});

V2TIMTopicInfo topicInfo;
topicInfo.topicID = "topicID";
topicInfo.SetInheritMessageReceiveOptionFromCommunity(true, callback);

获取话题列表

调用 getTopicInfoListAndroid / iOS & Mac / Windows) 接口获取话题列表。
当 topicIDList 列表为空时,为获取该社群下的所有话题列表。
当 topicIDList 填入指定的话题 ID 时,为获取填入的话题列表。
示例代码如下:
Android
iOS & Mac
Windows
V2TIMManager.getCommunityManager().getTopicInfoList(groupID, topicIDList, new V2TIMValueCallback<List<V2TIMTopicInfoResult>>() {
@Override
public void onSuccess(List<V2TIMTopicInfoResult> v2TIMTopicInfoResults) {
// 获取话题列表成功
}

@Override
public void onError(int code, String desc) {
// 获取话题列表失败
}
});
[[V2TIMManager sharedInstance] getTopicInfoList:@"groupID" topicIDList:@[@"topic1", @"topic2"] succ:^(NSMutableArray<V2TIMTopicInfoResult *> *resultList) {
// 获取话题列表成功
} 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 topicIDList;
topicIDList.PushBack("topic1");
topicIDList.PushBack("topic2");

auto callback = new ValueCallback<V2TIMTopicInfoResultVector>{};
callback->SetCallback(
[=](const V2TIMTopicInfoResultVector& topicInfoResultList) {
// 获取话题列表成功
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// 获取话题列表失败
delete callback;
});

V2TIMManager::GetInstance()->GetCommunityManager()->GetTopicInfoList("groupID", topicIDList, callback);

实现话题分组


社群-分组-话题三级结构中的分组实现原理为: 社群资料的 customInfoAndroid / iOS & Mac / Windows) 中定义一个字段保存该社群的话题分组列表;每个话题所属的分组,存储于话题资料的 customStringAndroid / iOS & Mac / Windows)字段中。
加载社群时,根据社群(群组)资料中的 customInfo 的话题分组列表字段,展示分组列表。
加载社群下的 Topic 列表时,通过话题资料中的 customString 获取分组名分配到所属分组中。
说明:
社群(群组)中 customInfo 的话题分组列表字段名,key 值,可由您自行定义,以下示例代码中,将其命名为 topic_category

配置社群的分组列表

此处直接修改群组 groupInfocustomInfo 即可。此处为一个 Map,key 值是您定义的话题分组列表字段名。 示例代码如下:
Android
iOS & Mac
Windows
List<String> categoryList = new ArrayList<>();
categoryList.add("分组1");
categoryList.add("分组2");
byte[] categoriesByteArray = gson.toJson(categoryList).getBytes();

Map<String, byte[]> customMap = new HashMap<>();
// 需要先在控制台配置群自定义字段 topic_category
customMap.put("topic_category", categoriesByteArray);

V2TIMGroupInfo modifyInfo = new V2TIMGroupInfo();
modifyInfo.setGroupID(groupID);
modifyInfo.setCustomInfo(customMap);
V2TIMManager.getGroupManager().setGroupInfo(modifyInfo, new V2TIMCallback() {
@Override
public void onSuccess() {
// 修改群资料成功
}

@Override
public void onError(int code, String desc) {
// 修改群资料失败
}
});

NSArray *categoryList = @[@"分组 1", @"分组 2"];
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:categoryList
options:NSJSONWritingPrettyPrinted
error:&error];
if ([data length] > 0 && error == nil) {
// 需要先在控制台配置群自定义字段 topic_category
NSDictionary *customInfo = @{@"topic_category": data};

V2TIMGroupInfo *info = [[V2TIMGroupInfo alloc] init];
info.groupID = @"需要修改的群 ID";
info.customInfo = customInfo;
[[V2TIMManager sharedInstance] setGroupInfo:info
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_;
};

V2TIMGroupInfo info;
info.groupID = "groupA";

V2TIMCustomInfo customInfo;
std::string str{u8"[\\"分组1\\", \\"分组2\\"]"};
// 需要先在控制台配置群自定义字段 topic_category
customInfo.Insert("topic_category", {reinterpret_cast<const uint8_t*>(str.data()), str.size()});
info.modifyFlag = V2TIMGroupInfoModifyFlag::V2TIM_GROUP_INFO_MODIFY_FLAG_CUSTOM_INFO;

auto callback = new Callback;
callback->SetCallback(
[=]() {
// 修改群资料成功
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// 修改群资料失败
delete callback;
});

V2TIMManager::GetInstance()->GetGroupManager()->SetGroupInfo(info, callback);

获取社群的分组列表

示例代码如下:
Android
iOS & Mac
Windows
String groupID = "group1";
List<String> groupIDList = new ArrayList<>();
groupIDList.add(groupID);
V2TIMManager.getGroupManager().getGroupsInfo(groupIDList, new V2TIMValueCallback<List<V2TIMGroupInfoResult>>() {
@Override
public void onSuccess(List<V2TIMGroupInfoResult> v2TIMGroupInfos) {
if (v2TIMGroupInfos.size() == 0) {
return;
}
V2TIMGroupInfoResult v2TIMGroupInfoResult = v2TIMGroupInfos.get(0);
if (v2TIMGroupInfoResult.getResultCode() == BaseConstants.ERR_SUCC) {
byte[] topicCategoryBytes = v2TIMGroupInfoResult.getGroupInfo().getCustomInfo().get("topic_category");
List<String> topicCategories = null;
if (topicCategoryBytes != null) {
Gson gson = new Gson();
try {
// 解析出分组列表
topicCategories = gson.fromJson(new String(topicCategoryBytes), List.class);
} catch (JsonParseException e) {
}
}
}
}

@Override
public void onError(int code, String desc) {

}
});
NSArray *groupIDList = @[@"group1"];
[[V2TIMManager sharedInstance] getGroupsInfo:groupIDList
succ:^(NSArray<V2TIMGroupInfoResult *> *groupResultList) {
// 获取成功
if (groupResultList.count == 0) {
return;
}
V2TIMGroupInfoResult *result = groupResultList.firstObject;
if (result.resultCode != 0) {
return;
}
NSData *categoryData = result.info.customInfo[@"topic_category"];
if (categoryData == nil) {
return;
}
NSArray *categoryList;
NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:categoryData
options:NSJSONReadingAllowFragments
error:nil];
if (jsonObject != nil && error == nil) {
// 解析出分组列表
categoryList = (NSArray *)jsonObject;
}
} 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 groupIDList;
groupIDList.PushBack("group1");

auto callback = new ValueCallback<V2TIMGroupInfoResultVector>{};
callback->SetCallback(
[=](const V2TIMGroupInfoResultVector& groupInfoResultList) {
if (groupInfoResultList.Size() == 1) {
const V2TIMGroupInfoResult& groupInfoResult = groupInfoResultList[0];
if (groupInfoResult.resultCode == 0) {
V2TIMGroupInfo info = groupInfoResult.info;
V2TIMCustomInfo customInfo = info.customInfo;
if (customInfo.Count("topic_category")) {
const V2TIMBuffer& topicCategory = customInfo.Get("topic_category");
// 解析出分组列表 ...
}
}
}

delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// 获取社群的分组列表失败
delete callback;
});
V2TIMManager::GetInstance()->GetGroupManager()->GetGroupsInfo(groupIDList, callback);

为话题添加分组

话题所属的分组可以使用话题的 customString 字段来保存。
示例代码如下:
Android
iOS & Mac
Windows
Map<String, Object> map = new HashMap<>();
map.put("category", "分组1");
Gson gson = new Gson();
V2TIMTopicInfo topicInfo = new V2TIMTopicInfo();
topicInfo.setTopicID(topicID);
topicInfo.setCustomString(gson.toJson(map));
V2TIMManager.getCommunityManager().setTopicInfo(topicInfo, new V2TIMCallback() {
@Override
public void onSuccess() {
// 修改话题信息成功
}

@Override
public void onError(int code, String desc) {
// 修改话题信息失败
}
});

NSDictionary *dict = @{@"category": @"分组 1"};
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dict
options:0
error:&error];
if ([data length] > 0 && error == nil) {
NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
V2TIMTopicInfo *info = [[V2TIMTopicInfo alloc] init];
info.topicID = @"需要设置的话题 ID";
info.customString = dataStr;
[[V2TIMManager sharedInstance] setTopicInfo:info 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_;
};

V2TIMTopicInfo topicInfo;
topicInfo.topicID = "topicID";
topicInfo.customString = u8"{\\"category\\": \\"分组1\\"}}";
topicInfo.modifyFlag = V2TIMTopicInfoModifyFlag::V2TIM_COMMUNITY_MODIFY_FLAG_TOPIC_CUSTOM_STRING;

auto callback = new Callback;
callback->SetCallback(
[=]() {
// 修改话题信息成功
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// 修改话题信息失败
delete callback;
});

V2TIMManager::GetInstance()->GetCommunityManager()->SetTopicInfo(topicInfo, callback);

获取话题所在分组

直接从 V2TIMTopicInfo 中,取出 customString 解析 json 即可。

监听话题回调

V2TIMCommunityListenerAndroid / iOS & Mac / Windows)中增加了话题相关的回调方法,onCreateTopiconDeleteTopiconChangeTopicInfo,用来监听话题事件。
示例代码如下:
Android
iOS & Mac
Windows
V2TIMCommunityListener v2TIMCommunityListener = new V2TIMCommunityListener() {
@Override
public void onCreateTopic(String groupID, String topicID) {
// 监听话题创建通知
}

@Override
public void onDeleteTopic(String groupID, List<String> topicIDList) {
// 监听话题删除通知
}

@Override
public void onChangeTopicInfo(String groupID, V2TIMTopicInfo topicInfo) {
// 监听话题信息更新通知
}
@Override public void onReceiveTopicRESTCustomData(String topicID, byte[] customData) { // 收到 RESTAPI 下发的话题自定义系统消息 }
};
V2TIMManager.getCommunityManager().addCommunityListener(v2TIMCommunityListener);
[[V2TIMManager sharedInstance] addCommunityListener:self];
- (void)onCreateTopic:(NSString *)groupID topicID:(NSString *)topicID {
// 监听话题创建通知
}
- (void)onDeleteTopic:(NSString *)groupID topicIDList:(NSArray<NSString *> *)topicIDList {
// 监听话题删除通知
}
- (void)onChangeTopicInfo:(NSString *)groupID topicInfo:(V2TIMTopicInfo *)topicInfo {
// 监听话题信息更新通知
}

- (void)onReceiveTopicRESTCustomData:(NSString *)topicID data:(NSData *)data {
// 收到 RESTAPI 下发的话题自定义系统消息
}
class CommunityListener final : public V2TIMCommunityListener {
public:
CommunityListener() = default;
~CommunityListener() override = default;

void OnCreateTopic(const V2TIMString& groupID, const V2TIMString& topicID) override {
// 监听话题创建通知
}
void OnDeleteTopic(const V2TIMString& groupID, const V2TIMStringVector& topicIDList) override {
// 监听话题删除通知
}
void OnChangeTopicInfo(const V2TIMString& groupID, const V2TIMTopicInfo& topicInfo) override {
// 监听话题信息更新通知
}
void OnReceiveTopicRESTCustomData(const V2TIMString &topicID, const V2TIMBuffer &customData) {
// 收到 RESTAPI 下发的话题自定义系统消息
}
};

// 添加群事件监听器,注意在移除监听器之前需要保持 groupListener 的生命期,以免接收不到事件回调
CommunityListener communityListener;
V2TIMManager::GetInstance()->GetCommunityManager()->AddCommunityListener(&communityListener);

话题消息

话题消息相关功能跟普通消息使用方法一样,涉及的接口有:
功能
接口
说明
sendMessage(Android / iOS & Mac
参数 groupID 填为话题 topicID
V2TIMAdvancedMsgListener(Android / iOS & Mac )中的 `onRecvNewMessage` 方法
消息中的 groupID 即为话题 topicID
cleanConversationUnreadMessageCount(Android / iOS & Mac
参数 conversationID 组成为 group_+话题 ID,即 group_topicID。
getGroupHistoryMessageList(Android / iOS & Mac
参数 groupID 填为话题 topicID
revokeMessage(Android / iOS & Mac
参数 groupID 填为话题 topicID

社群成员列表里的在线状态

社群支持在群成员列表里返回在线状态,开启方式:
1. 进入 IM 控制台 开启群成员在线状态的开关。

2. SDK API 通过 getGroupMemberList(Android / iOS & Mac) 接口可以获取到在线状态。