Flutter
功能描述
社群是一个由于共同主题而让大家聚集在一起的超大群组,可以在社群下根据不同兴趣创建多个话题。
社群用来管理群成员。社群下的所有话题不仅可以共享社群成员,还可以独立收发消息而不相互干扰。
社群模式详细介绍
社群和话题的管理接口在核心类
TencentImSDKPlugin.v2TIMManager.getGroupManager()
中。话题中的消息相关接口在核心类
TencentImSDKPlugin.v2TIMManager.getMessageManager()
中。社群管理
创建社群
注意:
自定义的社群 ID 前缀必须是@TGS#_。
示例代码如下:
// 创建支持话题的社群final info = V2TimGroupInfo(groupID: "",groupType: GroupType.Community,isSupportTopic: true,groupName: "testGroup",);await TencentImSDKPlugin.v2TIMManager.getCommunityManager().createCommunity(info: info,memberList: memberList,);
获取加入的社群列表
示例代码如下:
// 获取加入的社群列表V2TimValueCallback<List<V2TimGroupInfo>> groupList = await TencentImSDKPlugin.v2TIMManager.getCommunityManager().getJoinedCommunityList();
其他管理接口
其他功能跟普通群组使用方法一样,涉及的接口有:
话题管理
同一社群下可创建多个话题,多个话题共享一套社群成员关系,但不同话题内独立收发消息、互不干扰。
说明:
创建话题
创建话题分两步:
1. 创建
V2TIMTopicInfo
(Details) 对象。2. 调用
createTopicInCommunity
(Details) 接口创建话题。示例代码如下:
// 创建话题communityManager.createTopicInCommunity(groupID: "groupID", topicInfo: V2TimTopicInfo.fromJson({"topicName":"topic"}));
删除话题
示例代码如下:
// 删除话题communityManager.deleteTopicFromCommunity(groupID: "",topicIDList:["topicID"]);
修改话题信息
修改话题信息分两步:
1. 创建
V2TIMTopicInfo
(Details) 对象,并设置需要修改的字段。2. 调用
setTopicInfo
(Details) 接口修改话题信息。示例代码如下:
// 修改话题信息communityManager.setTopicInfo(topicInfo:V2TimTopicInfo.fromJson({"topicName":"topicName"}));
获取话题列表
当 topicIDList 列表为空数组时,为获取该社群下的所有话题列表。
当 topicIDList 填入指定的话题 ID 时,为获取填入的话题列表。
示例代码如下:
// 获取话题列表groupManager.getTopicInfoList(groupID: "",topicIDList: ['topicID']);
话题分组
社群模式(娱乐协作新利器),支持社群-分组-话题三级结构,将消息相互区隔。


加载社群时,根据社群(群组)的
customInfo
的话题分组列表字段,展示分组。该字段建议用 List<String>
形式存储。具体每个分组中有什么话题Topic,则要遍历Topic列表,通过
V2TimTopicInfo
的 customString
获得每个话题所属分组。说明:
获取社群的分组列表
调用
getCommunityCategoryList(String groupID)
方法,示例代码如下:getCommunityCategoryList(String groupID) async {final Map<String, String>? customInfo = await getCommunityCustomInfo(groupID);if(customInfo != null){final String? categoryListString = customInfo["topic_category"];if(categoryListString != null && categoryListString.isNotEmpty){return jsonDecode(categoryListString);}}}Future<Map<String, String>?> getCommunityCustomInfo(String groupID) async {V2TimValueCallback<List<V2TimGroupInfoResult>> res =await TencentImSDKPlugin.v2TIMManager.getGroupManager().getGroupsInfo(groupIDList: [groupID]);if(res.code != 0){final V2TimGroupInfoResult? groupInfo = res.data?[0];if(groupInfo != null){Map<String, String>? customInfo = groupInfo.groupInfo?.customInfo;return customInfo;}}return null;}
配置社群的分组列表
此处直接修改群组
groupInfo
的 customInfo
即可。此处为一个 Map,key 值您定义的话题分组列表字段名。getCommunityCustomInfo
方法已在上一段实现,示例代码如下:setCommunityCategoryList(String groupID, String groupType, List<String> newCategoryList) async {final Map<String, String>? customInfo = await getCommunityCustomInfo(groupID);customInfo?["topic_category"] = jsonEncode(newCategoryList);TencentImSDKPlugin.v2TIMManager.getGroupManager().setGroupInfo(info: V2TimGroupInfo(customInfo: customInfo,groupID: groupID,groupType: groupType,// ...其他资料));}
为话题添加分组
传入V2TimTopicInfo customString的字符串,建议使用JSON格式。
即,下方代码内
categoryName
建议使用格式:{"category":"分组1"}
。示例代码如下:
addCategoryForTopic(String groupID, String categoryName) {TencentImSDKPlugin.v2TIMManager.getGroupManager().setTopicInfo(topicInfo: V2TimTopicInfo(customString: categoryName),groupID: groupID, // 话题所在的群组id);}
获取话题所在分组
监听话题回调
在
V2TIMCommunityListener
(Details) 中增加了话题相关的回调方法,onCreateTopic
,onDeleteTopic
,onChangeTopicInfo
,用来监听话题事件。 示例代码如下:
V2TIMCommunityListener
v2TIMCommunityListener
= newV2TIMCommunityListener
() {onCreateTopic
(String groupID, String topicID) {// 监听话题创建通知}onDeleteTopic
(String groupID, List<String> topicIDList) {// 监听话题删除通知}onChangeTopicInfo
(String groupID, V2TIMTopicInfo topicInfo) {// 监听话题信息更新通知}};TencentImSDKPlugin.v2TIMManager.getCommunityManager().addCommunityListener(listener:v2TIMCommunityListener
);
话题消息
话题消息相关功能跟普通消息使用方法一样,涉及的接口有:
功能 | 接口 | 说明 |
发送消息 | 参数 groupID 填为话题 topicID | |
接收消息 | 消息中的 groupID 即为话题 topicID | |
标记消息已读 | 参数 groupID 填为话题 topicID | |
获取历史消息 | 参数 groupID 填为话题 topicID | |
撤回消息 | 参数 groupID 填为话题 topicID |