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

커뮤니티 관리

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(Android) / V2TIMManager(Group)(iOS and macOS) core class.
APIs related to messages in topics are in the V2TIMMessageManager(Android) / V2TIMManager(Message)(iOS and Mac) 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

You need to perform two steps to create a community group that supports topics:
1. Create the V2TIMGroupInfo object (Android / iOS and macOS / Windows) and set groupType to Community and isSupportTopic to true/YES.
2. Call the createGroup (Android / iOS & Mac / Windows) API to create a group.
Sample code:
Android
iOS and macOS
Windows
V2TIMGroupInfo v2TIMGroupInfo = new V2TIMGroupInfo();
v2TIMGroupInfo.setGroupName("This is a Community");
v2TIMGroupInfo.setGroupType(V2TIMManager.GROUP_TYPE_COMMUNITY);
v2TIMGroupInfo.setSupportTopic(true);
V2TIMManager.getGroupManager().createGroup(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
}
});
V2TIMGroupInfo *groupInfo = [[V2TIMGroupInfo alloc] init];;
groupInfo.groupName = @"This is a Community";
groupInfo.groupType = GroupType_Community;
groupInfo.isSupportTopic = YES;
[[V2TIMManager sharedInstance] createGroup: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";
info.isSupportTopic = true;

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()->GetGroupManager()->CreateGroup(info, {}, callback);

Getting the List of Community Groups Joined

You can call the getJoinedCommunityList(Android / iOS & Mac / Windows) API to get the list of topic supporting communities that you have joined.
Sample code:
Android
iOS and macOS
Windows
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 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 (Android / iOS and macOS / Windows)
quitGroup (Android / iOS and macOS / Windows)
dismissGroup (Android / iOS and macOS / Windows)
getGroupsInfo (Android / iOS and macOS / Windows)
setGroupInfo (Android / iOS and macOS / Windows)
Community group member management
getGroupMemberList (Android / iOS and macOS / Windows)
getGroupMembersInfo (Android / iOS and macOS / Windows)
setGroupMemberInfo (Android / iOS and macOS / Windows)
kickGroupMember (Android / iOS and macOS / Windows)

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 (Android / iOS and macOS / Windows).
2. Call the createTopicInCommunity (Android / iOS and Mac / Windows) API to create a topic.
Sample code:
Android
iOS and macOS
Windows
V2TIMTopicInfo topicInfo = new V2TIMTopicInfo();
topicInfo.setTopicName(topicName);
topicInfo.setTopicFaceUrl(topicFaceUrl);
topicInfo.setIntroduction(topicIntroduction);
topicInfo.setNotification(topicNotification);
topicInfo.setCustomString(topicCustomString);

// Set `groupID` to the ID of a community group that supports topics
V2TIMManager.getGroupManager().createTopicInCommunity(groupID, topicInfo, new V2TIMValueCallback<String>() {
@Override
public void onSuccess(String topicID) {
// Topic created successfully
}

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

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

// Set `groupID` to the ID of a community group that supports topics
[[V2TIMManager sharedInstance] createTopicInCommunity:@"groupID" topicInfo:topicInfo succ:^(NSString *topicID) {
// Topic created successfully
} fail:^(int code, NSString *desc) {
// Failed to create 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_;
};

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) {
// Topic created successfully
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// Failed to create the topic
delete callback;
});

// Set `groupID` to the ID of a community group that supports topics
V2TIMManager::GetInstance()->GetGroupManager()->CreateTopicInCommunity("groupID", topicInfo, callback);

Deleting a Topic

You can call the deleteTopicFromCommunity(Android / iOS & Mac / Windows) API to delete a topic.
Sample code:
Android
iOS and macOS
Windows
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 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 (Android / iOS and macOS / Windows) and set the fields to be modified.
2. Call the setTopicInfo (Android / iOS and Mac / Windows) API to modify topic information.
Sample code:
Android
iOS and macOS
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.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
}
});

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 setInheritMessageReceiveOptionFromCommunityAndroid / iOS & Mac / Windows).
Sample code:
Android
iOS & Mac
Windows
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 } });

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 (Android / iOS and Mac / Windows) 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:
Android
iOS and macOS
Windows
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 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 (Android / iOS and macOS / Windows) in the community profile defines a field to store the topic group list of the community. The customString field (Android / iOS and macOS / Windows) 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:
Android
iOS and macOS
Windows
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
}
});

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:
Android
iOS and macOS
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 {
// Parse the group list
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) {
// 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:
Android
iOS and macOS
Windows
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
}
});

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 (Android / iOS and macOS / Windows), topic callback methods such as onTopicCreated, onTopicDeleted, and onTopicInfoChanged are added to listen for topic events.
Sample code:
Android
iOS and macOS
Windows
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 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 (Android / iOS & Mac / Windows)
Set groupID to the topic ID.
onRecvNewMessage method in V2TIMAdvancedMsgListener (Android / iOS & Mac / Windows)
Set groupID in the message to the topic ID.
markGroupMessageAsRead (Android / iOS & Mac / Windows)
Set groupID to the topic ID.
getGroupHistoryMessageList (Android / iOS & Mac / Windows)
Set groupID to the topic ID.
revokeMessage (Android / iOS & Mac / Windows)
Set groupID to the topic ID.