Manage Community
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
.Sample code:
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>() {@Overridepublic void onSuccess(String groupID) {// Community group created successfully}@Overridepublic 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 successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Creation faileddelete 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:
V2TIMManager.getGroupManager().getJoinedCommunityList(new V2TIMValueCallback<List<V2TIMGroupInfo>>() {@Overridepublic void onSuccess(List<V2TIMGroupInfo> v2TIMGroupInfos) {// Community group list got successfully}@Overridepublic 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 successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to get the community group listdelete 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 | ||
| ||
| ||
| ||
| ||
Community group member management | ||
| ||
| ||
|
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:
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 topicsV2TIMManager.getGroupManager().createTopicInCommunity(groupID, topicInfo, new V2TIMValueCallback<String>() {@Overridepublic void onSuccess(String topicID) {// Topic created successfully}@Overridepublic 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 successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to create the topicdelete callback;});// Set `groupID` to the ID of a community group that supports topicsV2TIMManager::GetInstance()->GetGroupManager()->CreateTopicInCommunity("groupID", topicInfo, callback);
Deleting a Topic
Sample code:
V2TIMManager.getGroupManager().deleteTopicFromCommunity(groupID, topicIDList, new V2TIMValueCallback<List<V2TIMTopicOperationResult>>() {@Overridepublic void onSuccess(List<V2TIMTopicOperationResult> v2TIMTopicOperationResults) {// Topic deleted successfully}@Overridepublic 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 successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to delete the topicdelete 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:
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() {@Overridepublic void onSuccess() {// Topic information modified successfully}@Overridepublic 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 successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to modify the topic informationdelete 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
setInheritMessageReceiveOptionFromCommunity
(Android / iOS & Mac / Windows).Sample code:
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([=]() {// successdelete callback;},[=](int error_code, const V2TIMString& error_message) {// faileddelete callback;});V2TIMTopicInfo topicInfo;topicInfo.topicID = "topicID";topicInfo.SetInheritMessageReceiveOptionFromCommunity(true, callback);
Getting 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:
V2TIMManager.getGroupManager().getTopicInfoList(groupID, topicIDList, new V2TIMValueCallback<List<V2TIMTopicInfoResult>>() {@Overridepublic void onSuccess(List<V2TIMTopicInfoResult> v2TIMTopicInfoResults) {// Topic list obtained successfully}@Overridepublic 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 successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to obtain the topic listdelete 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: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() {@Overridepublic void onSuccess() {// Modified the group profile successfully}@Overridepublic 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:categoryListoptions:NSJSONWritingPrettyPrintederror:&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:infosucc:^{// 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 successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to modify the group profiledelete callback;});V2TIMManager::GetInstance()->GetGroupManager()->SetGroupInfo(info, callback);
Getting the List of Groups in the Community
Sample code:
String groupID = "group1";List<String> groupIDList = new ArrayList<>();groupIDList.add(groupID);V2TIMManager.getGroupManager().getGroupsInfo(groupIDList, new V2TIMValueCallback<List<V2TIMGroupInfoResult>>() {@Overridepublic 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 listtopicCategories = gson.fromJson(new String(topicCategoryBytes), List.class);} catch (JsonParseException e) {}}}}@Overridepublic void onError(int code, String desc) {}});
NSArray *groupIDList = @[@"group1"];[[V2TIMManager sharedInstance] getGroupsInfo:groupIDListsucc:^(NSArray<V2TIMGroupInfoResult *> *groupResultList) {// Conversation profile obtained successfullyif (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:categoryDataoptions:NSJSONReadingAllowFragmentserror:nil];if (jsonObject != nil && error == nil) {// Parse the group listcategoryList = (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 listdelete 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:
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() {@Overridepublic void onSuccess() {// Topic information modified successfully}@Overridepublic void onError(int code, String desc) {// Failed to modify the topic information}});
NSDictionary *dict = @{@"category": @"Group 1"};NSError *error = nil;NSData *data = [NSJSONSerialization dataWithJSONObject:dictoptions:0error:&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 successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to modify the topic informationdelete callback;});V2TIMManager::GetInstance()->GetGroupManager()->SetTopicInfo(topicInfo, callback);
Getting the Topic Group
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:
V2TIMGroupListener v2TIMGroupListener = new V2TIMGroupListener() {@Overridepublic void onTopicCreated(String groupID, String topicID) {// Listen for topic creation notifications}@Overridepublic void onTopicDeleted(String groupID, List<String> topicIDList) {// Listen for topic deletion notifications}@Overridepublic 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 |
Set groupID to the topic ID. | ||
Set groupID in the message to the topic ID. | ||
Set groupID to the topic ID. | ||
Set groupID to the topic ID. | ||
Set groupID to the topic ID. |