tencent cloud

文档反馈

Android&iOS&Windows&Mac

最后更新时间:2024-08-01 16:24:20

    功能描述

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

    效果展示

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

    社群管理

    创建社群

    创建支持话题的社群分两步:
    1. 创建 V2TIMGroupInfo (Android / iOS & Mac / Windows) 对象,其中 groupType 设置为 CommunityisSupportTopic 设置为 true/YES
    2. 调用 createGroup (Android / iOS & Mac / Windows) 接口创建群组。
    示例代码如下:
    Android
    iOS & Mac
    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) {
    // 创建社群成功
    }
    
    @Override
    public void onError(int code, String desc) {
    // 创建社群失败
    }
    });
    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) {
    // 创建社群成功
    } fail:^(int code, NSString *desc) {
    // 创建社群失败
    }];
    template <class T>
    class ValueCallback final : public V2TIMValueCallback<T> {
    public:
    using SuccessCallback = std::function<void(const T&)>;
    using ErrorCallback = std::function<void(int, const V2TIMString&)>;
    
    ValueCallback() = default;
    ~ValueCallback() override = default;
    
    void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
    success_callback_ = std::move(success_callback);
    error_callback_ = std::move(error_callback);
    }
    
    void OnSuccess(const T& value) override {
    if (success_callback_) {
    success_callback_(value);
    }
    }
    void OnError(int error_code, const V2TIMString& error_message) override {
    if (error_callback_) {
    error_callback_(error_code, error_message);
    }
    }
    
    private:
    SuccessCallback success_callback_;
    ErrorCallback error_callback_;
    };
    
    V2TIMGroupInfo info;
    info.groupType = "Community";
    info.groupName = "This is a Community";
    info.isSupportTopic = true;
    
    auto callback = new ValueCallback<V2TIMString>{};
    callback->SetCallback(
    [=](const V2TIMString& groupID) {
    // 创建社群成功
    delete callback;
    },
    [=](int error_code, const V2TIMString& error_message) {
    // 创建社群失败
    delete callback;
    });
    
    V2TIMManager::GetInstance()->GetGroupManager()->CreateGroup(info, {}, callback);

    获取加入的社群列表

    调用 getJoinedCommunityList(Android / iOS & Mac / Windows) 获取已经加入的支持话题的社群列表。
    示例代码如下:
    Android
    iOS & Mac
    Windows
    V2TIMManager.getGroupManager().getJoinedCommunityList(new V2TIMValueCallback<List<V2TIMGroupInfo>>() {
    @Override
    public void onSuccess(List<V2TIMGroupInfo> v2TIMGroupInfos) {
    // 获取社群列表成功
    }
    @Override
    public void onError(int code, String desc) {
    // 获取社群列表失败
    }
    });
    
    [[V2TIMManager sharedInstance] getJoinedCommunityList:^(NSArray<V2TIMGroupInfo *> *groupList) {
    // 获取社群列表成功
    } fail:^(int code, NSString *desc) {
    // 获取社群列表失败
    }];
    template <class T>
    class ValueCallback final : public V2TIMValueCallback<T> {
    public:
    using SuccessCallback = std::function<void(const T&)>;
    using ErrorCallback = std::function<void(int, const V2TIMString&)>;
    
    ValueCallback() = default;
    ~ValueCallback() override = default;
    
    void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
    success_callback_ = std::move(success_callback);
    error_callback_ = std::move(error_callback);
    }
    
    void OnSuccess(const T& value) override {
    if (success_callback_) {
    success_callback_(value);
    }
    }
    void OnError(int error_code, const V2TIMString& error_message) override {
    if (error_callback_) {
    error_callback_(error_code, error_message);
    }
    }
    
    private:
    SuccessCallback success_callback_;
    ErrorCallback error_callback_;
    };
    
    auto callback = new ValueCallback<V2TIMGroupInfoVector>{};
    callback->SetCallback(
    [=](const V2TIMGroupInfoVector& groupInfoList) {
    // 获取社群列表成功
    delete callback;
    },
    [=](int error_code, const V2TIMString& error_message) {
    // 获取社群列表失败
    delete callback;
    });
    
    V2TIMManager::GetInstance()->GetGroupManager()->GetJoinedCommunityList(callback);

    其他管理接口

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

    话题管理

    同一社群下可创建多个话题,多个话题共享一套社群成员关系,但不同话题内独立收发消息、互不干扰。
    说明:
    登录 控制台 并开启社群开关后方可使用,开关路径:Applications > Your App > Chat > Configuration > Group Configuration > Community。

    创建话题

    创建话题分两步:
    1. 创建 V2TIMTopicInfo (Android / iOS & Mac / Windows) 对象。
    2. 调用 createTopicInCommunity (Android / iOS & Mac / Windows) 接口创建话题。
    示例代码如下:
    Android
    iOS & Mac
    Windows
    V2TIMTopicInfo topicInfo = new V2TIMTopicInfo();
    topicInfo.setTopicName(topicName);
    topicInfo.setTopicFaceUrl(topicFaceUrl);
    topicInfo.setIntroduction(topicIntroduction);
    topicInfo.setNotification(topicNotification);
    topicInfo.setCustomString(topicCustomString);
    
    // groupID 填支持话题的社群 ID
    V2TIMManager.getGroupManager().createTopicInCommunity(groupID, topicInfo, new V2TIMValueCallback<String>() {
    @Override
    public void onSuccess(String topicID) {
    // 创建话题成功
    }
    
    @Override
    public void onError(int code, String desc) {
    // 创建话题失败
    }
    });
    
    V2TIMTopicInfo *topicInfo = [[V2TIMTopicInfo alloc] init];
    topicInfo.topicName = @"topicName";
    topicInfo.topicFaceURL = @"topicFaceUrl";
    topicInfo.introduction = @"topicIntroduction";
    topicInfo.notification = @"topicNotification";
    topicInfo.customString = @"topicCustomString";
    
    // groupID 填支持话题的社群 ID
    [[V2TIMManager sharedInstance] createTopicInCommunity:@"groupID" topicInfo:topicInfo succ:^(NSString *topicID) {
    // 创建话题成功
    } fail:^(int code, NSString *desc) {
    // 创建话题失败
    }];
    template <class T>
    class ValueCallback final : public V2TIMValueCallback<T> {
    public:
    using SuccessCallback = std::function<void(const T&)>;
    using ErrorCallback = std::function<void(int, const V2TIMString&)>;
    
    ValueCallback() = default;
    ~ValueCallback() override = default;
    
    void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
    success_callback_ = std::move(success_callback);
    error_callback_ = std::move(error_callback);
    }
    
    void OnSuccess(const T& value) override {
    if (success_callback_) {
    success_callback_(value);
    }
    }
    void OnError(int error_code, const V2TIMString& error_message) override {
    if (error_callback_) {
    error_callback_(error_code, error_message);
    }
    }
    
    private:
    SuccessCallback success_callback_;
    ErrorCallback error_callback_;
    };
    
    V2TIMTopicInfo topicInfo;
    topicInfo.topicID = "topicID";
    topicInfo.topicName = "topicName";
    topicInfo.topicFaceURL = "topicFaceURL";
    topicInfo.introduction = "introduction";
    topicInfo.notification = "notification";
    
    auto callback = new ValueCallback<V2TIMString>{};
    callback->SetCallback(
    [=](const V2TIMString& string) {
    // 创建话题成功
    delete callback;
    },
    [=](int error_code, const V2TIMString& error_message) {
    // 创建话题失败
    delete callback;
    });
    
    // groupID 填支持话题的社群 ID
    V2TIMManager::GetInstance()->GetGroupManager()->CreateTopicInCommunity("groupID", topicInfo, callback);

    删除话题

    调用 deleteTopicFromCommunity(Android / iOS & Mac / Windows) 接口删除话题。
    示例代码如下:
    Android
    iOS & Mac
    Windows
    V2TIMManager.getGroupManager().deleteTopicFromCommunity(groupID, topicIDList, new V2TIMValueCallback<List<V2TIMTopicOperationResult>>() {
    @Override
    public void onSuccess(List<V2TIMTopicOperationResult> v2TIMTopicOperationResults) {
    // 删除话题成功
    }
    
    @Override
    public void onError(int code, String desc) {
    // 删除话题失败
    }
    });
    
    [[V2TIMManager sharedInstance] deleteTopicFromCommunity:@"groupID" topicIDList:@[@"topic1", @"topic2"] succ:^(NSMutableArray<V2TIMTopicOperationResult *> *resultList) {
    // 删除话题成功
    } fail:^(int code, NSString *desc) {
    // 删除话题失败
    }];
    template <class T>
    class ValueCallback final : public V2TIMValueCallback<T> {
    public:
    using SuccessCallback = std::function<void(const T&)>;
    using ErrorCallback = std::function<void(int, const V2TIMString&)>;
    
    ValueCallback() = default;
    ~ValueCallback() override = default;
    
    void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
    success_callback_ = std::move(success_callback);
    error_callback_ = std::move(error_callback);
    }
    
    void OnSuccess(const T& value) override {
    if (success_callback_) {
    success_callback_(value);
    }
    }
    void OnError(int error_code, const V2TIMString& error_message) override {
    if (error_callback_) {
    error_callback_(error_code, error_message);
    }
    }
    
    private:
    SuccessCallback success_callback_;
    ErrorCallback error_callback_;
    };
    
    V2TIMStringVector topicIDList;
    topicIDList.PushBack("topic1");
    topicIDList.PushBack("topic2");
    
    auto callback = new ValueCallback<V2TIMTopicOperationResultVector>{};
    callback->SetCallback(
    [=](const V2TIMTopicOperationResultVector& topicOperationResultList) {
    // 删除话题成功
    delete callback;
    },
    [=](int error_code, const V2TIMString& error_message) {
    // 删除话题失败
    delete callback;
    });
    
    V2TIMManager::GetInstance()->GetGroupManager()->DeleteTopicFromCommunity("groupID", topicIDList, callback);

    修改话题信息

    修改话题信息分两步:
    1. 创建 V2TIMTopicInfo (Android / iOS & Mac / Windows) 对象,并设置需要修改的字段。
    2. 调用 setTopicInfo (Android / iOS & Mac / Windows) 接口修改话题信息。
    示例代码如下:
    Android
    iOS & Mac
    Windows
    V2TIMTopicInfo topicInfo = new V2TIMTopicInfo();
    topicInfo.setTopicID(topicID);
    topicInfo.setTopicName(topicName);
    topicInfo.setTopicFaceUrl(topicFaceUrl);
    topicInfo.setIntroduction(topicIntroduction);
    topicInfo.setNotification(topicNotification);
    topicInfo.setCustomString(topicCustomString);
    topicInfo.setDraft(topicDraft);
    topicInfo.setAllMute(false);
    V2TIMManager.getGroupManager().setTopicInfo(topicInfo, new V2TIMCallback() {
    @Override
    public void onSuccess() {
    // 修改话题信息成功
    }
    
    @Override
    public void onError(int code, String desc) {
    // 修改话题信息失败
    }
    });
    
    V2TIMTopicInfo *topicInfo = [[V2TIMTopicInfo alloc] init];
    topicInfo.topicID = @"topicID";
    topicInfo.topicName = @"topicName";
    topicInfo.topicFaceURL = @"topicFaceUrl";
    topicInfo.introduction = @"topicIntroduction";
    topicInfo.notification = @"topicNotification";
    topicInfo.customString = @"topicCustomString";
    topicInfo.draftText = @"topicDraft";
    topicInfo.isAllMuted = NO;
    [[V2TIMManager sharedInstance] setTopicInfo:topicInfo succ:^{
    // 修改话题信息成功
    } fail:^(int code, NSString *desc) {
    // 修改话题信息失败
    }];
    class Callback final : public V2TIMCallback {
    public:
    using SuccessCallback = std::function<void()>;
    using ErrorCallback = std::function<void(int, const V2TIMString&)>;
    
    Callback() = default;
    ~Callback() override = default;
    
    void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
    success_callback_ = std::move(success_callback);
    error_callback_ = std::move(error_callback);
    }
    
    void OnSuccess() override {
    if (success_callback_) {
    success_callback_();
    }
    }
    void OnError(int error_code, const V2TIMString& error_message) override {
    if (error_callback_) {
    error_callback_(error_code, error_message);
    }
    }
    
    private:
    SuccessCallback success_callback_;
    ErrorCallback error_callback_;
    };
    
    V2TIMTopicInfo topicInfo;
    topicInfo.topicID = "topicID";
    topicInfo.topicName = "topicName";
    topicInfo.notification = "topicFaceURL";
    topicInfo.introduction = "introduction";
    topicInfo.topicFaceURL = "notification";
    topicInfo.customString = "customString";
    topicInfo.modifyFlag = 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(
    [=]() {
    // 修改话题信息成功
    delete callback;
    },
    [=](int error_code, const V2TIMString& error_message) {
    // 修改话题信息失败
    delete callback;
    });
    
    V2TIMManager::GetInstance()->GetGroupManager()->SetTopicInfo(topicInfo, callback);
    修改话题其他信息可参考禁言成员修改话题消息接收选项
    话题的消息接收选项可以选择是否继承自社群,接口为 setInheritMessageReceiveOptionFromCommunityAndroid / iOS & Mac / Windows)。
    示例代码如下:
    Android
    iOS & Mac
    Windows
    V2TIMTopicInfo topicInfo = new V2TIMTopicInfo();
    topicInfo.setTopicID(topicID);
    topicInfo.setInheritMessageReceiveOptionFromCommunity(true, new V2TIMCallback() { @Override public void onSuccess() { // 继承成功 } @Override public void onError(int code, String desc) { // 继承失败 } });
    
    V2TIMTopicInfo *topicInfo = [[V2TIMTopicInfo alloc] init];
    topicInfo.topicID = @"topicID";
    [topicInfo setInheritMessageReceiveOptionFromCommunity:YES succ:^{
    // 继承成功
    } fail:^(int code, NSString *desc) {
    // 继承失败
    }];
    class Callback final : public V2TIMCallback {
    public:
    using SuccessCallback = std::function<void()>;
    using ErrorCallback = std::function<void(int, const V2TIMString&)>;
    
    Callback() = default;
    ~Callback() override = default;
    
    void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
    success_callback_ = std::move(success_callback);
    error_callback_ = std::move(error_callback);
    }
    
    void OnSuccess() override {
    if (success_callback_) {
    success_callback_();
    }
    }
    void OnError(int error_code, const V2TIMString& error_message) override {
    if (error_callback_) {
    error_callback_(error_code, error_message);
    }
    }
    
    private:
    SuccessCallback success_callback_;
    ErrorCallback error_callback_;
    };
    
    auto callback = new Callback;
    callback->SetCallback(
    [=]() {
    // 继承成功
    delete callback;
    },
    [=](int error_code, const V2TIMString& error_message) {
    // 继承失败
    delete callback;
    });
    
    V2TIMTopicInfo topicInfo;
    topicInfo.topicID = "topicID";
    topicInfo.SetInheritMessageReceiveOptionFromCommunity(true, callback);

    获取话题列表

    调用 getTopicInfoList (Android / iOS & Mac / Windows) 接口获取话题列表。
    当 topicIDList 列表为空时,为获取该社群下的所有话题列表。
    当 topicIDList 填入指定的话题 ID 时,为获取填入的话题列表。
    示例代码如下:
    Android
    iOS & Mac
    Windows
    V2TIMManager.getGroupManager().getTopicInfoList(groupID, topicIDList, new V2TIMValueCallback<List<V2TIMTopicInfoResult>>() {
    @Override
    public void onSuccess(List<V2TIMTopicInfoResult> v2TIMTopicInfoResults) {
    // 获取话题列表成功
    }
    
    @Override
    public void onError(int code, String desc) {
    // 获取话题列表失败
    }
    });
    [[V2TIMManager sharedInstance] getTopicInfoList:@"groupID" topicIDList:@[@"topic1", @"topic2"] succ:^(NSMutableArray<V2TIMTopicInfoResult *> *resultList) {
    // 获取话题列表成功
    } fail:^(int code, NSString *desc) {
    // 获取话题列表失败
    }];
    template <class T>
    class ValueCallback final : public V2TIMValueCallback<T> {
    public:
    using SuccessCallback = std::function<void(const T&)>;
    using ErrorCallback = std::function<void(int, const V2TIMString&)>;
    
    ValueCallback() = default;
    ~ValueCallback() override = default;
    
    void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
    success_callback_ = std::move(success_callback);
    error_callback_ = std::move(error_callback);
    }
    
    void OnSuccess(const T& value) override {
    if (success_callback_) {
    success_callback_(value);
    }
    }
    void OnError(int error_code, const V2TIMString& error_message) override {
    if (error_callback_) {
    error_callback_(error_code, error_message);
    }
    }
    
    private:
    SuccessCallback success_callback_;
    ErrorCallback error_callback_;
    };
    
    V2TIMStringVector topicIDList;
    topicIDList.PushBack("topic1");
    topicIDList.PushBack("topic2");
    
    auto callback = new ValueCallback<V2TIMTopicInfoResultVector>{};
    callback->SetCallback(
    [=](const V2TIMTopicInfoResultVector& topicInfoResultList) {
    // 获取话题列表成功
    delete callback;
    },
    [=](int error_code, const V2TIMString& error_message) {
    // 获取话题列表失败
    delete callback;
    });
    
    V2TIMManager::GetInstance()->GetGroupManager()->GetTopicInfoList("groupID", topicIDList, callback);

    实现话题分组

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

    配置社群的分组列表

    此处直接修改群组 groupInfocustomInfo 即可。此处为一个 Map,key 值是您定义的话题分组列表字段名。 示例代码如下:
    Android
    iOS & Mac
    Windows
    List<String> categoryList = new ArrayList<>();
    categoryList.add("分组1");
    categoryList.add("分组2");
    byte[] categoriesByteArray = gson.toJson(categoryList).getBytes();
    
    Map<String, byte[]> customMap = new HashMap<>();
    // 需要先在控制台配置群自定义字段 topic_category
    customMap.put("topic_category", categoriesByteArray);
    
    V2TIMGroupInfo modifyInfo = new V2TIMGroupInfo();
    modifyInfo.setGroupID(groupID);
    modifyInfo.setCustomInfo(customMap);
    V2TIMManager.getGroupManager().setGroupInfo(modifyInfo, new V2TIMCallback() {
    @Override
    public void onSuccess() {
    // 修改群资料成功
    }
    
    @Override
    public void onError(int code, String desc) {
    // 修改群资料失败
    }
    });
    
    NSArray *categoryList = @[@"分组 1", @"分组 2"];
    NSError *error = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:categoryList
    options:NSJSONWritingPrettyPrinted
    error:&error];
    if ([data length] > 0 && error == nil) {
    // 需要先在控制台配置群自定义字段 topic_category
    NSDictionary *customInfo = @{@"topic_category": data};
    
    V2TIMGroupInfo *info = [[V2TIMGroupInfo alloc] init];
    info.groupID = @"需要修改的群 ID";
    info.customInfo = customInfo;
    [[V2TIMManager sharedInstance] setGroupInfo:info
    succ:^{
    // 修改群资料成功
    } fail:^(int code, NSString *desc) {
    // 修改群资料失败
    }];
    }
    class Callback final : public V2TIMCallback {
    public:
    using SuccessCallback = std::function<void()>;
    using ErrorCallback = std::function<void(int, const V2TIMString&)>;
    
    Callback() = default;
    ~Callback() override = default;
    
    void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
    success_callback_ = std::move(success_callback);
    error_callback_ = std::move(error_callback);
    }
    
    void OnSuccess() override {
    if (success_callback_) {
    success_callback_();
    }
    }
    void OnError(int error_code, const V2TIMString& error_message) override {
    if (error_callback_) {
    error_callback_(error_code, error_message);
    }
    }
    
    private:
    SuccessCallback success_callback_;
    ErrorCallback error_callback_;
    };
    
    V2TIMGroupInfo info;
    info.groupID = "groupA";
    
    V2TIMCustomInfo customInfo;
    std::string str{u8"[\\"分组1\\", \\"分组2\\"]"};
    // 需要先在控制台配置群自定义字段 topic_category
    customInfo.Insert("topic_category", {reinterpret_cast<const uint8_t*>(str.data()), str.size()});
    info.modifyFlag = V2TIMGroupInfoModifyFlag::V2TIM_GROUP_INFO_MODIFY_FLAG_CUSTOM_INFO;
    
    auto callback = new Callback;
    callback->SetCallback(
    [=]() {
    // 修改群资料成功
    delete callback;
    },
    [=](int error_code, const V2TIMString& error_message) {
    // 修改群资料失败
    delete callback;
    });
    
    V2TIMManager::GetInstance()->GetGroupManager()->SetGroupInfo(info, callback);

    获取社群的分组列表

    示例代码如下:
    Android
    iOS & Mac
    Windows
    String groupID = "group1";
    List<String> groupIDList = new ArrayList<>();
    groupIDList.add(groupID);
    V2TIMManager.getGroupManager().getGroupsInfo(groupIDList, new V2TIMValueCallback<List<V2TIMGroupInfoResult>>() {
    @Override
    public void onSuccess(List<V2TIMGroupInfoResult> v2TIMGroupInfos) {
    if (v2TIMGroupInfos.size() == 0) {
    return;
    }
    V2TIMGroupInfoResult v2TIMGroupInfoResult = v2TIMGroupInfos.get(0);
    if (v2TIMGroupInfoResult.getResultCode() == BaseConstants.ERR_SUCC) {
    byte[] topicCategoryBytes = v2TIMGroupInfoResult.getGroupInfo().getCustomInfo().get("topic_category");
    List<String> topicCategories = null;
    if (topicCategoryBytes != null) {
    Gson gson = new Gson();
    try {
    // 解析出分组列表
    topicCategories = gson.fromJson(new String(topicCategoryBytes), List.class);
    } catch (JsonParseException e) {
    }
    }
    }
    }
    
    @Override
    public void onError(int code, String desc) {
    
    }
    });
    NSArray *groupIDList = @[@"group1"];
    [[V2TIMManager sharedInstance] getGroupsInfo:groupIDList
    succ:^(NSArray<V2TIMGroupInfoResult *> *groupResultList) {
    // 获取成功
    if (groupResultList.count == 0) {
    return;
    }
    V2TIMGroupInfoResult *result = groupResultList.firstObject;
    if (result.resultCode != 0) {
    return;
    }
    NSData *categoryData = result.info.customInfo[@"topic_category"];
    if (categoryData == nil) {
    return;
    }
    NSArray *categoryList;
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:categoryData
    options:NSJSONReadingAllowFragments
    error:nil];
    if (jsonObject != nil && error == nil) {
    // 解析出分组列表
    categoryList = (NSArray *)jsonObject;
    }
    } fail:^(int code, NSString *desc) {
    // 获取失败
    }];
    template <class T>
    class ValueCallback final : public V2TIMValueCallback<T> {
    public:
    using SuccessCallback = std::function<void(const T&)>;
    using ErrorCallback = std::function<void(int, const V2TIMString&)>;
    
    ValueCallback() = default;
    ~ValueCallback() override = default;
    
    void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
    success_callback_ = std::move(success_callback);
    error_callback_ = std::move(error_callback);
    }
    
    void OnSuccess(const T& value) override {
    if (success_callback_) {
    success_callback_(value);
    }
    }
    void OnError(int error_code, const V2TIMString& error_message) override {
    if (error_callback_) {
    error_callback_(error_code, error_message);
    }
    }
    
    private:
    SuccessCallback success_callback_;
    ErrorCallback error_callback_;
    };
    
    V2TIMStringVector groupIDList;
    groupIDList.PushBack("group1");
    
    auto callback = new ValueCallback<V2TIMGroupInfoResultVector>{};
    callback->SetCallback(
    [=](const V2TIMGroupInfoResultVector& groupInfoResultList) {
    if (groupInfoResultList.Size() == 1) {
    const V2TIMGroupInfoResult& groupInfoResult = groupInfoResultList[0];
    if (groupInfoResult.resultCode == 0) {
    V2TIMGroupInfo info = groupInfoResult.info;
    V2TIMCustomInfo customInfo = info.customInfo;
    if (customInfo.Count("topic_category")) {
    const V2TIMBuffer& topicCategory = customInfo.Get("topic_category");
    // 解析出分组列表 ...
    }
    }
    }
    
    delete callback;
    },
    [=](int error_code, const V2TIMString& error_message) {
    // 获取社群的分组列表失败
    delete callback;
    });
    V2TIMManager::GetInstance()->GetGroupManager()->GetGroupsInfo(groupIDList, callback);

    为话题添加分组

    话题所属的分组可以使用话题的 customString 字段来保存。
    示例代码如下:
    Android
    iOS & Mac
    Windows
    Map<String, Object> map = new HashMap<>();
    map.put("category", "分组1");
    Gson gson = new Gson();
    V2TIMTopicInfo topicInfo = new V2TIMTopicInfo();
    topicInfo.setTopicID(topicID);
    topicInfo.setCustomString(gson.toJson(map));
    V2TIMManager.getGroupManager().setTopicInfo(topicInfo, new V2TIMCallback() {
    @Override
    public void onSuccess() {
    // 修改话题信息成功
    }
    
    @Override
    public void onError(int code, String desc) {
    // 修改话题信息失败
    }
    });
    
    NSDictionary *dict = @{@"category": @"分组 1"};
    NSError *error = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict
    options:0
    error:&error];
    if ([data length] > 0 && error == nil) {
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    V2TIMTopicInfo *info = [[V2TIMTopicInfo alloc] init];
    info.topicID = @"需要设置的话题 ID";
    info.customString = dataStr;
    [[V2TIMManager sharedInstance] setTopicInfo:info succ:^{
    // 设置话题资料成功
    } fail:^(int code, NSString *desc) {
    // 设置话题资料失败
    }];
    }
    class Callback final : public V2TIMCallback {
    public:
    using SuccessCallback = std::function<void()>;
    using ErrorCallback = std::function<void(int, const V2TIMString&)>;
    
    Callback() = default;
    ~Callback() override = default;
    
    void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
    success_callback_ = std::move(success_callback);
    error_callback_ = std::move(error_callback);
    }
    
    void OnSuccess() override {
    if (success_callback_) {
    success_callback_();
    }
    }
    void OnError(int error_code, const V2TIMString& error_message) override {
    if (error_callback_) {
    error_callback_(error_code, error_message);
    }
    }
    
    private:
    SuccessCallback success_callback_;
    ErrorCallback error_callback_;
    };
    
    V2TIMTopicInfo topicInfo;
    topicInfo.topicID = "topicID";
    topicInfo.customString = u8"{\\"category\\": \\"分组1\\"}}";
    topicInfo.modifyFlag = V2TIMGroupInfoModifyFlag::V2TIM_TOPIC_INFO_MODIFY_FLAG_CUSTOM_STRING;
    
    auto callback = new Callback;
    callback->SetCallback(
    [=]() {
    // 修改话题信息成功
    delete callback;
    },
    [=](int error_code, const V2TIMString& error_message) {
    // 修改话题信息失败
    delete callback;
    });
    
    V2TIMManager::GetInstance()->GetGroupManager()->SetTopicInfo(topicInfo, callback);

    获取话题所在分组

    直接从 获取话题列表 中,取出 customString 解析 json 即可。

    监听话题回调

    V2TIMGroupListener (Android / iOS & Mac / Windows) 中增加了话题相关的回调方法,onTopicCreatedonTopicDeletedonTopicInfoChanged,用来监听话题事件。
    示例代码如下:
    Android
    iOS & Mac
    Windows
    V2TIMGroupListener v2TIMGroupListener = new V2TIMGroupListener() {
    @Override
    public void onTopicCreated(String groupID, String topicID) {
    // 监听话题创建通知
    }
    
    @Override
    public void onTopicDeleted(String groupID, List<String> topicIDList) {
    // 监听话题删除通知
    }
    
    @Override
    public void onTopicInfoChanged(String groupID, V2TIMTopicInfo topicInfo) {
    // 监听话题信息更新通知
    }
    };
    V2TIMManager.getInstance().addGroupListener(v2TIMGroupListener);
    [[V2TIMManager sharedInstance] addGroupListener:self];
    - (void)onTopicCreated:(NSString *)groupID topicID:(NSString *)topicID {
    // 监听话题创建通知
    }
    - (void)onTopicDeleted:(NSString *)groupID topicIDList:(NSArray<NSString *> *)topicIDList {
    // 监听话题删除通知
    }
    - (void)onTopicChanged:(NSString *)groupID topicInfo:(V2TIMTopicInfo *)topicInfo {
    // 监听话题信息更新通知
    }
    class GroupListener final : public V2TIMGroupListener {
    public:
    GroupListener() = default;
    ~GroupListener() override = default;
    
    void OnTopicCreated(const V2TIMString& groupID, const V2TIMString& topicID) override {
    // 监听话题创建通知
    }
    void OnTopicDeleted(const V2TIMString& groupID, const V2TIMStringVector& topicIDList) override {
    // 监听话题删除通知
    }
    void OnTopicChanged(const V2TIMString& groupID, const V2TIMTopicInfo& topicInfo) override {
    // 监听话题信息更新通知
    }
    };
    
    // 添加群事件监听器,注意在移除监听器之前需要保持 groupListener 的生命期,以免接收不到事件回调
    GroupListener groupListener;
    V2TIMManager::GetInstance()->AddGroupListener(&groupListener);

    话题消息

    话题消息相关功能跟普通消息使用方法一样,涉及的接口有:
    功能
    接口
    说明
    sendMessage (Android / iOS & Mac / Windows)
    参数 groupID 填为话题 topicID
    V2TIMAdvancedMsgListener (Android / iOS & Mac / Windows) 中的 onRecvNewMessage 方法
    消息中的 groupID 即为话题 topicID
    markGroupMessageAsRead (Android / iOS & Mac / Windows)
    参数 groupID 填为话题 topicID
    getGroupHistoryMessageList (Android / iOS & Mac / Windows)
    参数 groupID 填为话题 topicID
    revokeMessage (Android / iOS & Mac / Windows)
    参数 groupID 填为话题 topicID
    
    联系我们

    联系我们,为您的业务提供专属服务。

    技术支持

    如果你想寻求进一步的帮助,通过工单与我们进行联络。我们提供7x24的工单服务。

    7x24 电话支持