이 페이지는 현재 영어로만 제공되며 한국어 버전은 곧 제공될 예정입니다. 기다려 주셔서 감사드립니다.

커뮤니티 관리

Feature Description

A community group is a large group of people brought together by common topics, and multiple topics can be created under the same community group based on different interests. Communities are used to manage group members. All topics under the same community are shared among members, who can send and receive messages within each topic independently.
Community and topic management APIs are in the V2TIMGroupManager(Java) / V2TIMManager+Group(Objective-C、Swift) core class.
APIs related to messages in topics are in the V2TIMMessageManager(Java) / V2TIMManager+Message(Objective-C、Swift) core class.
Note:
Supported by versions 6.2.2363 and later, using the V2TIMCommunityManager and V2TIMCommunityListener classes is recommended starting from version 7.7.5282.
You must purchase the Premium Edition, log in to the Console, and enable the community switch before you can use this feature, toggle path: Applications > Your App > Chat > Configuration > Group Configuration > Community.

Effect

You can use this feature to achieve the community effect as shown in the figure below:




Community Group Management

Creating a community group

Call createCommunity (Java / Swift / Objective-C / Windows) to create a topic-supporting community.
Note:
The custom community ID prefix must be @TGS#_. (Note: Highlighted content)
Below is the sample code:
Java
Swift
Objective-C
C++
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) {
// Community group created successfully
}

@Override
public void onError(int code, String desc) {
// Creation failed
}
});
let groupInfo = V2TIMGroupInfo()
groupInfo.groupType = "Community"
groupInfo.groupName = "CommunityForTestPermissionGroupSwift"
groupInfo.defaultPermissions = 13;
groupInfo.enablePermissionGroup = true;

V2TIMManager.shared.createGroup(info: groupInfo, memberList: nil) { groupID in
print( "createPermissionGroupInCommunity succ, \(groupID)")
} fail: { code, desc in
print( "createPermissionGroupInCommunity fail, \(code), \(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) {
// Community group created successfully
} fail:^(int code, NSString *desc) {
// Creation failed
}];
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) {
// Community group created successfully
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// Creation failed
delete callback;
});

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

Getting the List of Community Groups Joined

You can call the getJoinedCommunityList(Java / Swift / Objective-C / C++) API to get the list of topic supporting communities that you have joined.
Sample code:
Java
Swift
Objective-C
C++
V2TIMManager.getGroupManager().getJoinedCommunityList(new V2TIMValueCallback<List<V2TIMGroupInfo>>() {
@Override
public void onSuccess(List<V2TIMGroupInfo> v2TIMGroupInfos) {
// Community group list got successfully
}
@Override
public void onError(int code, String desc) {
// Failed to get the community group list
}
});

V2TIMManager.shared.getJoinedCommunityList { groupList in
groupList.forEach { item in
print( item.description)
}
} fail: { code, desc in
print( "getJoinedCommunityList fail, \(code), \(desc)")
}
[[V2TIMManager sharedInstance] getJoinedCommunityList:^(NSArray<V2TIMGroupInfo *> *groupList) {
// Community group list got successfully
} fail:^(int code, NSString *desc) {
// Failed to get the community group list
}];
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) {
// Community group list obtained successfully
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// Failed to get the community group list
delete callback;
});

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

Other Management APIs

Other features can be used in the same way as an ordinary group feature and involve the following APIs:
Category
Feature
API
Community group management
joinGroup (Java / Swift / Objective-C / C++)
quitGroup (Java / Swift / Objective-C / C++)
dismissGroup (Java / Swift / Objective-C / C++)
getGroupsInfo (Java / Swift / Objective-C / C++)
setGroupInfo (Java / Swift / Objective-C / C++)
Community group member management
getGroupMemberList (Java / Swift / Objective-C / C++)
getGroupMembersInfo (Java / Swift / Objective-C / C++)
setGroupMemberInfo (Java / Swift / Objective-C / C++)
kickGroupMember (Java / Swift / Objective-C / C++)

Topic Management

Multiple topics can be created under the same community group. All the topics are shared among group members, who can send and receive messages within each topic independently.
Note:
Log in to Console and enable the Community Switch to use this feature. Toggle path: Applications > Your App > Chat > Configuration > Group Configuration > Community.

Creating a Topic

You need to perform two steps to create a topic:
1. Create the V2TIMTopicInfo object (Java / Swift / Objective-C / C++).
2. Call the createTopicInCommunity (Java / Swift / Objective-C / C++) API to create a topic.
Note:
The custom topic ID consists of: GroupId+@TOPIC#_+custom part. For example, if the community ID is @TGS#_123 and the custom part is TestTopic, then the topic ID is @TGS#_123@TOPIC#_TestTopic.
Starting from version 8.4, the interface supports the creation of "private topics" (set the topicType of V2TIMTopicInfo to "Private"). "Private topics" has its own member list (supports up to 10,000 members), community members need to join the topic before sending message (you need to buy Enterprise Edition to use this feature).
Sample code:
Java
Swift
Objective-C
C++
V2TIMTopicInfo topicInfo = new V2TIMTopicInfo();
// Topic type, "Public": public topic; "Private": private topic
topicInfo.setTopicType(topicType);
topicInfo.setTopicName(topicName);
topicInfo.setTopicFaceUrl(topicFaceUrl);
topicInfo.setIntroduction(topicIntroduction);
topicInfo.setNotification(topicNotification);
topicInfo.setCustomString(topicCustomString);
// The following fields are only valid for private topics
topicInfo.setTopicAddOpt(V2TIMTopicInfo.V2TIM_GROUP_ADD_AUTH);
topicInfo.setTopicApproveOpt(V2TIMTopicInfo.V2TIM_GROUP_ADD_AUTH);
topicInfo.setMemberMaxCount(10000);
List<V2TIMCreateGroupMemberInfo> memberList = new ArrayList<>();
V2TIMCreateGroupMemberInfo memberInfo = new V2TIMCreateGroupMemberInfo();
memberInfo.setUserID("xixi");
memberInfo.setRole(1);
memberList.add(memberInfo);
topicInfo.setMemberList(memberList);

// groupID fill in the community ID that supports the topic
V2TIMManager.getCommunityManager().createTopicInCommunity(groupID, topicInfo, new V2TIMValueCallback<String>() {
@Override
public void onSuccess(String topicID) {
// create topic succ
}

@Override
public void onError(int code, String desc) {
// create topic succ
}
});
let topicInfo = V2TIMTopicInfo()
// Topic type, "Public": public topic; "Private": private topic
topicInfo.topicType = "Public"
topicInfo.topicName = "swift topic name"
topicInfo.topicFaceURL = "topicFaceUrl"
topicInfo.introduction = "topicIntroduction"
topicInfo.notification = "topicNotification"
topicInfo.customString = "topicCustomString"
// The following fields are only valid for private topics
topicInfo.topicAddOpt = .V2TIM_GROUP_ADD_AUTH
topicInfo.topicApproveOpt = .V2TIM_GROUP_ADD_AUTH
topicInfo.memberMaxCount = 10000
let memberInfo = V2TIMCreateGroupMemberInfo()
memberInfo.userID = "xixi"
memberInfo.role = 1
topicInfo.memberList.append(memberInfo)

V2TIMManager.shared.createTopicInCommunity(groupID: "groupID", topicInfo: topicInfo) { topicID in
print( "createTopicInCommunity succ, \(topicID)")
} fail: { code, desc in
print( "createTopicInCommunity fail, \(code), \(desc)")
}
V2TIMTopicInfo *topicInfo = [[V2TIMTopicInfo alloc] init];
// Topic type, "Public": public topic; "Private": private topic
topicInfo.topicType = @"Public";
topicInfo.topicName = @"topicName";
topicInfo.topicFaceURL = @"topicFaceUrl";
topicInfo.introduction = @"topicIntroduction";
topicInfo.notification = @"topicNotification";
topicInfo.customString = @"topicCustomString";
// The following fields are only valid for private topics
topicInfo.topicAddOpt = V2TIM_GROUP_ADD_AUTH;
topicInfo.topicApproveOpt = V2TIM_GROUP_ADD_AUTH;
topicInfo.memberMaxCount = 10000;
V2TIMCreateGroupMemberInfo *memberInfo = [[V2TIMCreateGroupMemberInfo alloc] init];
memberInfo.userID = @"xixi";
memberInfo.role = V2TIM_GROUP_MEMBER_ROLE_MEMBER;
topicInfo.memberList = @[memberInfo];

// groupID fill in the community ID that supports the topic
[[V2TIMManager sharedInstance] createTopicInCommunity:@"groupID" topicInfo:topicInfo succ:^(NSString *topicID) {
// create topic succ
} fail:^(int code, NSString *desc) {
// create topic failed
}];
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;
// Topic type, "Public": public topic; "Private": private topic
topicInfo.topicType = "Public";
topicInfo.topicID = "topicID";
topicInfo.topicName = "topicName";
topicInfo.topicFaceURL = "topicFaceURL";
topicInfo.introduction = "introduction";
topicInfo.notification = "notification";
// groupID fill in the community ID that supports the topic
topicInfo.topicAddOpt = V2TIM_GROUP_ADD_AUTH;
topicInfo.topicApproveOpt = V2TIM_GROUP_ADD_AUTH;
topicInfo.memberMaxCount = 20;
V2TIMCreateGroupMemberInfo member;
member.userID = "xixi";
member.role = V2TIM_GROUP_MEMBER_ROLE_MEMBER;
V2TIMCreateGroupMemberInfoVector member_list;
member_list.PushBack(member);
topicInfo.memberlist = member_list;

auto callback = new ValueCallback<V2TIMString>{};
callback->SetCallback(
[=](const V2TIMString& string) {
// create topic succ
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// create topic failed
delete callback;
});

// groupID fill in the community ID that supports the topic
V2TIMManager::GetInstance()->GetCommunityManager()->CreateTopicInCommunity("groupID", topicInfo, callback);

Deleting a Topic

You can call the deleteTopicFromCommunity(Java / Swift / Objective-C / C++) API to delete a topic.
Sample code:
Java
Swift
Objective-C
C++
V2TIMManager.getGroupManager().deleteTopicFromCommunity(groupID, topicIDList, new V2TIMValueCallback<List<V2TIMTopicOperationResult>>() {
@Override
public void onSuccess(List<V2TIMTopicOperationResult> v2TIMTopicOperationResults) {
// Topic deleted successfully
}

@Override
public void onError(int code, String desc) {
// Failed to delete the topic
}
});

V2TIMManager.shared.deleteTopicFromCommunity(groupID: "groupID", topicIDList: ["topic1","topic2"]) { resultList in
resultList.forEach { item in
// V2TIMTopicOperationResult
print( item.description)
}
} fail: { code, desc in
print( "deleteTopicFromCommunity fail, \(code), \(desc)")
}
[[V2TIMManager sharedInstance] deleteTopicFromCommunity:@"groupID" topicIDList:@[@"topic1", @"topic2"] succ:^(NSMutableArray<V2TIMTopicOperationResult *> *resultList) {
// Topic deleted successfully
} fail:^(int code, NSString *desc) {
// Failed to delete the topic
}];
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) {
// Topic deleted successfully
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// Failed to delete the topic
delete callback;
});

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

Modifying Topic Information

You need to perform two steps to modify the information of a topic:
1. Create the V2TIMTopicInfo object (Java / Swift / Objective-C / C++) and set the fields to be modified.
2. Call the setTopicInfo (Java / Swift / Objective-C / C++) API to modify topic information.
Sample code:
Java
Swift
Objective-C
C++
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.getGroupManager().setTopicInfo(topicInfo, new V2TIMCallback() {
@Override
public void onSuccess() {
// Topic information modified successfully
}

@Override
public void onError(int code, String desc) {
// Failed to modify the topic information
}
});

let topicInfo = V2TIMTopicInfo()
topicInfo.topicID = "topicID"
topicInfo.topicName = "swift topic name"
topicInfo.topicFaceURL = "topicFaceUrl"
topicInfo.introduction = "topicIntroduction"
topicInfo.notification = "topicNotification"
topicInfo.customString = "topicCustomString"
topicInfo.draftText = "topicDraft"

V2TIMManager.shared.setTopicInfo(topicInfo: topicInfo) {
print( "setTopicInfo succ, \(topicInfo.description)")
} fail: { code, desc in
print( "setTopicInfo fail, \(code), \(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:^{
// Topic information modified successfully
} fail:^(int code, NSString *desc) {
// Failed to modify the topic information
}];
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 = V2TIMGroupInfoModifyFlag::V2TIM_GROUP_INFO_MODIFY_FLAG_GROUP_NAME |
V2TIMGroupInfoModifyFlag::V2TIM_GROUP_INFO_MODIFY_FLAG_NOTIFICATION |
V2TIMGroupInfoModifyFlag::V2TIM_GROUP_INFO_MODIFY_FLAG_INTRODUCTION |
V2TIMGroupInfoModifyFlag::V2TIM_GROUP_INFO_MODIFY_FLAG_FACE_URL |
V2TIMGroupInfoModifyFlag::V2TIM_TOPIC_INFO_MODIFY_FLAG_CUSTOM_STRING;

auto callback = new Callback;
callback->SetCallback(
[=]() {
// Topic information modified successfully
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// Failed to modify the topic information
delete callback;
});

V2TIMManager::GetInstance()->GetGroupManager()->SetTopicInfo(topicInfo, callback);
For how to modify other information of a topic, see Muting Member and Changing Topic Message Receiving Option.
The message receiving option of a topic can be inherited from the community or not. The interface is setInheritMessageReceiveOptionFromCommunityJava / Swift / Objective-C / C++).
Sample code:
Java
Swift
Objective-C
C++
V2TIMTopicInfo topicInfo = new V2TIMTopicInfo();
topicInfo.setTopicID(topicID);
topicInfo.setInheritMessageReceiveOptionFromCommunity(true, new V2TIMCallback() {
@Override
public void onSuccess() {
// success
}

@Override
public void onError(int code, String desc) {
// failed
}
});

let topicInfo = V2TIMTopicInfo()
topicInfo.topicID = "@TGS#_@TGS#cIIYRJIM62C6@TOPIC#_@TOPIC#cKIYRJIM62CD"
topicInfo.setInheritMessageReceiveOptionFromCommunity(isInherit: true) {
print( "enableTopitInheritMsgFlagFromCommunity success")
} fail: { code, desc in
print( "enableTopitInheritMsgFlagFromCommunity fail, \(code), \(desc)")
}
V2TIMTopicInfo *topicInfo = [[V2TIMTopicInfo alloc] init];
topicInfo.topicID = @"topicID";
[topicInfo setInheritMessageReceiveOptionFromCommunity:YES succ:^{
// success
} fail:^(int code, NSString *desc) {
// failed
}];
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(
[=]() {
// success
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// failed
delete callback;
});

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

Getting the Topic List

You can call the getTopicInfoList (Java / Swift / Objective-C / C++) API to get the topic list.
If topicIDList is empty, the list of all topics of the community group will be got.
If topicIDList is the ID of specified topics, the list of the specified topics will be got.
Sample code:
Java
Swift
Objective-C
C++
V2TIMManager.getGroupManager().getTopicInfoList(groupID, topicIDList, new V2TIMValueCallback<List<V2TIMTopicInfoResult>>() {
@Override
public void onSuccess(List<V2TIMTopicInfoResult> v2TIMTopicInfoResults) {
// Topic list obtained successfully
}

@Override
public void onError(int code, String desc) {
// Failed to obtain the topic list
}
});
V2TIMManager.shared.getTopicInfoList(groupID: "groupID", topicIDList: ["topic1","topic2"]) { resultList in
resultList.forEach { item in
// V2TIMTopicInfoResult
print( "\(item.description)")
}
} fail: { code, desc in
print( "getTopicInfoList fail, \(code), \(desc)")
}
[[V2TIMManager sharedInstance] getTopicInfoList:@"groupID" topicIDList:@[@"topic1", @"topic2"] succ:^(NSMutableArray<V2TIMTopicInfoResult *> *resultList) {
// Topic list obtained successfully
} fail:^(int code, NSString *desc) {
// Failed to obtain the topic list
}];
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) {
// Topic list obtained successfully
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// Failed to obtain the topic list
delete callback;
});

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

Topic Group

The community-group-topic hierarchy is implemented as follows: The customInfo (Java / Swift / Objective-C / C++) in the community profile defines a field to store the topic group list of the community. The customString field (Java / Swift / Objective-C / C++) in the topic profile stores the topic group.
When a community is loaded, the customInfo field for the topic group list in the community (group) profile is used to display the group list.
When the topic list of a community is loaded, the customString in the topic profile is used to get the group name for assignment.
Note:
You can customize the key value of the customInfo field for the topic group list of the community (group). The following sample code names it topic_category.

Configuring the Group List for the Community

You just need to modify the customInfo in groupInfo. Here is a Map, and the key value is the name of the field for the topic group list you defined.
Sample code:
Java
Swift
Objective-C
C++
List<String> categoryList = new ArrayList<>();
categoryList.add("Group 1");
categoryList.add("Group 2");
byte[] categoriesByteArray = gson.toJson(categoryList).getBytes();

Map<String, byte[]> customMap = new HashMap<>();
// You need to configure the custom group field `topic_category` in the console first.
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() {
// Modified the group profile successfully
}

@Override
public void onError(int code, String desc) {
// Failed to modify the group profile
}
});

let info = V2TIMGroupMemberFullInfo()
info.userID = "userA"
info.nameCard = "userA_namecard"
info.customInfo = ["topic_category": "value1".data(using: .utf8) ?? Data()]
V2TIMManager.shared.setGroupMemberInfo(groupID: "groupID", info: info) {
print( info.description)
} fail: { code, desc in
print( "setGroupMemberInfo fail, \(code), \(desc)")
}
NSArray *categoryList = @[@"Group 1", @"Group 2"];
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:categoryList
options:NSJSONWritingPrettyPrinted
error:&error];
if ([data length] > 0 && error == nil) {
// You need to configure the custom group field `topic_category` in the console first.
NSDictionary *customInfo = @{@"topic_category": data};

V2TIMGroupInfo *info = [[V2TIMGroupInfo alloc] init];
info.groupID = @"Group ID of the group to be modified";
info.customInfo = customInfo;
[[V2TIMManager sharedInstance] setGroupInfo:info
succ:^{
// Modified the group profile successfully
} fail:^(int code, NSString *desc) {
// Failed to modify the group profile
}];
}
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"[\"Group 1\", \"Group 2\"]"};
// You need to configure the custom group field `topic_category` in the console first.
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(
[=]() {
// Modified the group profile successfully
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// Failed to modify the group profile
delete callback;
});

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

Getting the List of Groups in the Community

Sample code:
Java
Swift
Objective-C
C++
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 {
// Parse the group list
topicCategories = gson.fromJson(new String(topicCategoryBytes), List.class);
} catch (JsonParseException e) {
}
}
}
}

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

}
});
let groupIDList = ["group1"]

V2TIMManager.shared.getGroupsInfo(groupIDList: groupIDList, succ: { groupResultList in
if groupResultList.isEmpty {
return
}
let result = groupResultList.first!
if result.resultCode != 0 {
return
}
guard let categoryData = result.info?.customInfo?["topic_category"] as? Data else {
return
}
var categoryList: [Any]?
do {
let jsonObject = try JSONSerialization.jsonObject(with: categoryData, options: .allowFragments)
// Parse the group list
if let jsonArray = jsonObject as? [Any] {
categoryList = jsonArray
}
} catch {

}
}, fail: { code, desc in

})
NSArray *groupIDList = @[@"group1"];
[[V2TIMManager sharedInstance] getGroupsInfo:groupIDList
succ:^(NSArray<V2TIMGroupInfoResult *> *groupResultList) {
// Conversation profile obtained successfully
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) {
// Parse the group list
categoryList = (NSArray *)jsonObject;
}
} fail:^(int code, NSString *desc) {
// Failed to obtain
}];
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");
// Parse the group list
}
}
}

delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// Failed to get the community group list
delete callback;
});
V2TIMManager::GetInstance()->GetGroupManager()->GetGroupsInfo(groupIDList, callback);

Adding a Topic to a Group

You can save the topic group with the customString field of the topic.
Sample code:
Java
Swift
Objective-C
C++
Map<String, Object> map = new HashMap<>();
map.put("category", "Group 1");
Gson gson = new Gson();
V2TIMTopicInfo topicInfo = new V2TIMTopicInfo();
topicInfo.setTopicID(topicID);
topicInfo.setCustomString(gson.toJson(map));
V2TIMManager.getGroupManager().setTopicInfo(topicInfo, new V2TIMCallback() {
@Override
public void onSuccess() {
// Topic information modified successfully
}

@Override
public void onError(int code, String desc) {
// Failed to modify the topic information
}
});

let topicInfo = V2TIMTopicInfo()
topicInfo.topicID = "topicID"
topicInfo.topicName = "topicName swift"
topicInfo.customString = "customString"
V2TIMManager.shared.setTopicInfo(topicInfo: topicInfo) {
print( "setTopicInfo succ, \(topicInfo.description)")
} fail: { code, desc in
print( "setTopicInfo fail, \(code), \(desc)")
}
NSDictionary *dict = @{@"category": @"Group 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 of the topic to be set";
info.customString = dataStr;
[[V2TIMManager sharedInstance] setTopicInfo:info succ:^{
// Set the topic profile successfully
} fail:^(int code, NSString *desc) {
// Failed to set the topic profile
}];
}
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\": \"Group 1\"}}";
topicInfo.modifyFlag = V2TIMGroupInfoModifyFlag::V2TIM_TOPIC_INFO_MODIFY_FLAG_CUSTOM_STRING;

auto callback = new Callback;
callback->SetCallback(
[=]() {
// Topic information modified successfully
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// Failed to modify the topic information
delete callback;
});

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

Getting the Topic Group

Use the customString to parse the JSON content after getting the topic list.

Listening for Topic Callbacks

In V2TIMGroupListener (Java / Swift / Objective-C / C++), topic callback methods such as onTopicCreated, onTopicDeleted, and onTopicInfoChanged are added to listen for topic events.
Sample code:
Java
Swift
Objective-C
C++
V2TIMGroupListener v2TIMGroupListener = new V2TIMGroupListener() {
@Override
public void onTopicCreated(String groupID, String topicID) {
// Listen for topic creation notifications
}

@Override
public void onTopicDeleted(String groupID, List<String> topicIDList) {
// Listen for topic deletion notifications
}

@Override
public void onTopicInfoChanged(String groupID, V2TIMTopicInfo topicInfo) {
// Listen for topic information update notifications
}
};
V2TIMManager.getInstance().addGroupListener(v2TIMGroupListener);
V2TIMManager.shared.addCommunityListener(listener: self)

func onCreateTopic(groupID: String, topicID: String) {
print( "groupID:\(groupID), topicID:\(topicID)")
}

func onDeleteTopic(groupID: String, topicIDList: Array<String>) {
print( "groupID:\(groupID), topicIDList:\(topicIDList)")
}

func onChangeTopicInfo(groupID: String, topicInfo: V2TIMTopicInfo) {
print( "groupID:\(groupID), topicInfo:\(topicInfo.description), inheritMsgFlag:\(topicInfo.isInheritMessageReceiveOptionFromCommunity())")
}

func onReceiveTopicRESTCustomData(topicID: String, data: Data?) {
print( "\(topicID), data:\(data?.description ?? "nil")")
}
[[V2TIMManager sharedInstance] addGroupListener:self];
- (void)onTopicCreated:(NSString *)groupID topicID:(NSString *)topicID {
// Listen for topic creation notifications
}
- (void)onTopicDeleted:(NSString *)groupID topicIDList:(NSArray<NSString *> *)topicIDList {
// Listen for topic deletion notifications
}
- (void)onTopicChanged:(NSString *)groupID topicInfo:(V2TIMTopicInfo *)topicInfo {
// Listen for topic information update notifications
}
class GroupListener final : public V2TIMGroupListener {
public:
GroupListener() = default;
~GroupListener() override = default;

void OnTopicCreated(const V2TIMString& groupID, const V2TIMString& topicID) override {
// Listen for topic creation notifications
}
void OnTopicDeleted(const V2TIMString& groupID, const V2TIMStringVector& topicIDList) override {
// Listen for topic deletion notifications
}
void OnTopicChanged(const V2TIMString& groupID, const V2TIMTopicInfo& topicInfo) override {
// Listen for topic information update notifications
}
};

// Add a group event listener. Keep `groupListener` valid before the listener is removed to ensure event callbacks are received.
GroupListener groupListener;
V2TIMManager::GetInstance()->AddGroupListener(&groupListener);

Topic Messages

Topic messages can be used in the same way as ordinary messages and involve the following APIs:
Feature
API
Description
sendMessage (Java / Swift / Objective-C / C++)
Set groupID to the topic ID.
onRecvNewMessage method in V2TIMAdvancedMsgListener (Java / Swift / Objective-C / C++)
Set groupID in the message to the topic ID.
cleanConversationUnreadMessageCount(Java / Swift / Objective-C /C++
Set groupID to the topic ID.
getGroupHistoryMessageList (Java / Swift / Objective-C / C++)
Set groupID to the topic ID.
revokeMessage (Java / Swift / Objective-C / C++)
Set groupID to the topic ID.