功能描述
在某些场景下,您可能需要对好友进行分组,例如分为 "大学同学"、"公司同事" 等,您可以调用以下接口实现。
新建好友分组
示例代码如下:
List<String> userIDList = new ArrayList<>();userIDList.add("user1");userIDList.add("user2");V2TIMManager.getFriendshipManager().createFriendGroup("大学好友", userIDList, new V2TIMValueCallback<List<V2TIMFriendOperationResult>>() {@Overridepublic void onSuccess(List<V2TIMFriendOperationResult> v2TIMFriendOperationResults) {// 创建好友分组成功}@Overridepublic void onError(int code, String desc) {// 创建好友分组失败}});
// 创建好友分组[[V2TIMManager sharedInstance] createFriendGroup:@"大学好友" userIDList:@[@"user1", @"user2"] succ:^(NSArray<V2TIMFriendOperationResult *> *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_;};V2TIMString groupName = u8"大学好友";V2TIMStringVector userIDList;userIDList.PushBack(u8"user1");userIDList.PushBack(u8"user2");auto callback = new ValueCallback<V2TIMFriendOperationResultVector>{};callback->SetCallback([=](const V2TIMFriendOperationResultVector& friendOperationResultList) {// 创建好友分组成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 创建好友分组失败delete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->CreateFriendGroup(groupName, userIDList, callback);
删除好友分组
示例代码如下:
List<String> friendGroupList = new ArrayList<>();friendGroupList.add("大学好友");V2TIMManager.getFriendshipManager().deleteFriendGroup(friendGroupList, new V2TIMCallback() {@Overridepublic void onSuccess() {// 删除好友分组成功}@Overridepublic void onError(int code, String desc) {// 删除好友分组失败}});
// 删除好友分组[[V2TIMManager sharedInstance] deleteFriendGroup:@[@"大学好友"] 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_;};V2TIMStringVector groupNameList;groupNameList.PushBack(u8"大学好友");auto callback = new Callback{};callback->SetCallback([=]() {// 删除好友分组成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 删除好友分组失败delete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->DeleteFriendGroup(groupNameList, callback);
重命名好友分组
示例代码如下:
V2TIMManager.getFriendshipManager().renameFriendGroup("大学好友", "中学好友", new V2TIMCallback() {@Overridepublic void onSuccess() {// 修改好友分组成功}@Overridepublic void onError(int code, String desc) {// 修改好友分组失败}});
// 修改好友分组[[V2TIMManager sharedInstance] renameFriendGroup:@"大学好友" newName:@"中学好友" 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_;};V2TIMString oldName = u8"大学好友";V2TIMString newName = u8"中学好友";auto callback = new Callback{};callback->SetCallback([=]() {// 修改好友分组成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 修改好友分组失败delete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->RenameFriendGroup(oldName, newName, callback);
获取好友分组
示例代码如下:
List<String> friendGroups = new ArrayList<>();friendGroups.add("大学好友");V2TIMManager.getFriendshipManager().getFriendGroups(friendGroups, new V2TIMValueCallback<List<V2TIMFriendGroup>>() {@Overridepublic void onSuccess(List<V2TIMFriendGroup> v2TIMFriendGroups) {// 获取好友分组成功}@Overridepublic void onError(int code, String desc) {// 获取好友分组失败}});
// 获取好友分组[[V2TIMManager sharedInstance] getFriendGroupList:@[@"大学好友"] succ:^(NSArray<V2TIMFriendGroup *> *groups) {// 获取好友分组成功} 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 groupNameList;groupNameList.PushBack(u8"大学好友");auto callback = new ValueCallback<V2TIMFriendGroupVector>{};callback->SetCallback([=](const V2TIMFriendGroupVector& friendGroupList) {// 获取好友分组成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 获取好友分组失败delete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->GetFriendGroups(groupNameList, callback);
添加好友到一个分组
示例代码如下:
List<String> userIDList = new ArrayList<>();userIDList.add("user1");userIDList.add("user2");V2TIMManager.getFriendshipManager().addFriendsToFriendGroup("大学好友", userIDList, new V2TIMValueCallback<List<V2TIMFriendOperationResult>>() {@Overridepublic void onSuccess(List<V2TIMFriendOperationResult> v2TIMFriendOperationResults) {// 添加成功}@Overridepublic void onError(int code, String desc) {// 添加失败}});
// 添加好友到一个分组[[V2TIMManager sharedInstance] addFriendsToFriendGroup:@"大学好友" userIDList:@[@"user1", @"user2"] succ:^(NSArray<V2TIMFriendOperationResult *> *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_;};V2TIMString groupName = u8"大学好友";V2TIMStringVector userIDList;userIDList.PushBack(u8"user1");userIDList.PushBack(u8"user2");auto callback = new ValueCallback<V2TIMFriendOperationResultVector>{};callback->SetCallback([=](const V2TIMFriendOperationResultVector& friendOperationResultList) {// 添加成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 添加失败delete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->AddFriendsToFriendGroup(groupName, userIDList, callback);
从分组中删除某好友
您可以调用
deleteFriendsFromFriendGroup
(Android / iOS & Mac / Windows) 从分组中删除某好友。从该分组中删除好友,只会将好友移出分组,不会删除好友关系。示例代码如下:
List<String> userIDList = new ArrayList<>();userIDList.add("user1");userIDList.add("user2");V2TIMManager.getFriendshipManager().deleteFriendsFromFriendGroup("大学好友", userIDList, new V2TIMValueCallback<List<V2TIMFriendOperationResult>>() {@Overridepublic void onSuccess(List<V2TIMFriendOperationResult> v2TIMFriendOperationResults) {// 删除成功}@Overridepublic void onError(int code, String desc) {// 删除失败}});
// 从分组中删除某好友[[V2TIMManager sharedInstance] deleteFriendsFromFriendGroup:@"大学好友" userIDList:@[@"user1", @"user2"] succ:^(NSArray<V2TIMFriendOperationResult *> *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_;};V2TIMString groupName = u8"大学好友";V2TIMStringVector userIDList;userIDList.PushBack(u8"user1");userIDList.PushBack(u8"user2");auto callback = new ValueCallback<V2TIMFriendOperationResultVector>{};callback->SetCallback([=](const V2TIMFriendOperationResultVector& friendOperationResultList) {// 删除成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 删除失败delete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->DeleteFriendsFromFriendGroup(groupName, userIDList,callback);
监听好友分组变更通知
示例代码如下:
V2TIMFriendshipListener v2TIMFriendshipListener = new V2TIMFriendshipListener() {@Overridepublic void onFriendGroupCreated(String groupName, List<V2TIMFriendInfo> friendInfoList) {// 收到好友分组被创建通知}@Overridepublic void onFriendGroupDeleted(List<String> groupNameList) {// 收到好友分组被删除通知}@Overridepublic void onFriendGroupNameChanged(String oldGroupName, String newGroupName) {// 收到好友分组重命名通知}@Overridepublic void onFriendsAddedToGroup(String groupName, List<V2TIMFriendInfo> friendInfoList) {// 收到向好友分组添加好友通知}@Overridepublic void onFriendsDeletedFromGroup(String groupName, List<String> friendIDList) {// 收到从好友分组中删除好友通知}};// 添加关系链监听器V2TIMManager.getFriendshipManager().addFriendListener(v2TIMFriendshipListener);
// 添加关系链监听器[[V2TIMManager sharedInstance] addFriendListener:self];- (void)onFriendGroupCreated:(NSString *)groupName friendInfoList:(NSArray<V2TIMFriendInfo *> *)friendInfoList {// 到好友分组被创建通知}- (void)onFriendGroupDeleted:(NSArray<NSString *> *)groupNameList {// 收到好友分组被删除通知}- (void)onFriendGroupNameChanged:(NSString *)oldGroupName newGroupName:(NSString *)newGroupName {// 收到好友分组重命名通知}- (void)onFriendsAddedToGroup:(NSString *)groupName friendInfoList:(NSArray<V2TIMFriendInfo *> *)friendInfoList {// 收到向好友分组添加好友通知}- (void)onFriendsDeletedFromGroup:(NSString *)groupName friendIDList:(NSArray<NSString *> *)friendIDList {// 收到从好友分组中删除好友通知}
class FriendshipListener final : public V2TIMFriendshipListener {public:FriendshipListener() = default;~FriendshipListener() override = default;void OnFriendGroupCreated(const V2TIMString &groupName, const V2TIMFriendInfoVector &friendInfoList) override {// 收到好友分组被创建通知}void OnFriendGroupDeleted(const V2TIMStringVector &groupNameList) override {// 收到好友分组被删除通知}void OnFriendGroupNameChanged(const V2TIMString &oldGroupName, const V2TIMString &newGroupName) override {// 收到好友分组重命名通知}void OnFriendsAddedToGroup(const V2TIMString &groupName, const V2TIMFriendInfoVector &friendInfoList) override {// 收到向好友分组添加好友通知}void OnFriendsDeletedFromGroup(const V2TIMString &groupName, const V2TIMStringVector &friendIDList) override {// 收到从好友分组中删除好友通知}// 其他成员函数 ...};// 添加关系链事件监听器,注意在移除监听器之前需要保持 friendshipListener 的生命期,以免接收不到事件回调FriendshipListener friendshipListener;V2TIMManager::GetInstance()->GetFriendshipManager()->AddFriendListener(&friendshipListener);
说明:
好友分组变更通知从 8.0 及以上版本支持。