V2TIMManager.getFriendshipManager().getFriendList(new V2TIMValueCallback<List<V2TIMFriendInfo>>() {@Overridepublic void onSuccess(List<V2TIMFriendInfo> v2TIMFriendInfos) {// 获取好友列表成功}@Overridepublic void onError(int code, String desc) {// 获取好友列表失败}});
// 获取好友列表[[V2TIMManager sharedInstance] getFriendList:^(NSArray<V2TIMFriendInfo *> *infoList) {// 获取好友列表成功} 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<V2TIMFriendInfoVector>{};callback->SetCallback([=](const V2TIMFriendInfoVector& friendInfoList) {// 获取好友列表成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 获取好友列表失败delete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->GetFriendList(callback);
// 添加好友V2TIMFriendAddApplication application = new V2TIMFriendAddApplication("userA");application.setAddType(V2TIMFriendInfo.V2TIM_FRIEND_TYPE_BOTH);V2TIMManager.getFriendshipManager().addFriend(application, new V2TIMValueCallback<V2TIMFriendOperationResult>() {@Overridepublic void onSuccess(V2TIMFriendOperationResult v2TIMFriendOperationResult) {// 添加好友请求成功}@Overridepublic void onError(int code, String desc) {// 添加好友失败}});
// 添加好友V2TIMFriendAddApplication *application = [[V2TIMFriendAddApplication alloc] init];application.userID = @"userA";application.addType = V2TIM_FRIEND_TYPE_BOTH;[[V2TIMManager sharedInstance] addFriend:application succ:^(V2TIMFriendOperationResult *result) {// 添加好友请求成功} 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_;};V2TIMFriendAddApplication application;application.userID = u8"userA";application.addType = V2TIMFriendType::V2TIM_FRIEND_TYPE_BOTH;auto callback = new ValueCallback<V2TIMFriendOperationResult>{};callback->SetCallback([=](const V2TIMFriendOperationResult& friendOperationResult) {// 添加好友请求成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 添加好友失败delete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->AddFriend(application, callback);
setFriendListener
设置关系链监听器。setSelfInfo
函数里的 allowType
(Android / iOS & Mac / Windows) 字段设置为 “加好友不需要验证(V2TIM_FRIEND_ALLOW_ANY
)”。addFriend
申请添加 B 为好友即可添加成功。添加成功后,按照申请参数 V2TIMFriendAddApplication
中 addType
的设置有两种情况:V2TIM_FRIEND_TYPE_BOTH
) ,则用户 A 和 B 都会收到 onFriendListAdded
(Android / iOS & Mac / Windows) 回调;V2TIM_FRIEND_TYPE_SINGLE
),则只有用户 A 收到 onFriendListAdded
回调。setFriendListener
设置关系链监听。setSelfInfo
函数里的 allowType
字段设置为 “加好友需要验证(V2TIM_FRIEND_NEED_CONFIRM
)”。 addFriend
申请添加 B 为好友,接口的成功回调参数中 resultCode
返回 30539,表示需要等待用户 B 的验证。同时 A 和 B 都会收到 onFriendApplicationListAdded
(Android / iOS & Mac / Windows) 的回调。onFriendApplicationListAdded
的回调,当参数 V2TIMFriendApplication
中的 type
为 V2TIM_FRIEND_APPLICATION_COME_IN
时,可以选择接受或者拒绝:acceptFriendApplication
(Android / iOS & Mac / Windows) 接受好友请求。如果参数接受类型为仅同意加单向好友(V2TIM_FRIEND_ACCEPT_AGREE
)时:onFriendListAdded
回调,说明单向加好友成功。onFriendApplicationListDeleted
(Android / iOS & Mac / Windows) 回调,此时 B 成为 A 的好友,但 A 仍不是 B 的好友。acceptFriendApplication
接受好友请求,如果参数接受类型为同意加双向好友时(V2TIM_FRIEND_ACCEPT_AGREE_AND_ADD
),A 和 B 都会收到 onFriendListAdded
回调,说明互相加好友成功。refuseFriendApplication
(Android / iOS & Mac / Windows) 拒绝好友请求,双方都会收到 onFriendApplicationListDeleted
回调。List<String> friendIDList = new ArrayList<>();friendIDList.add("userA");V2TIMManager.getFriendshipManager().deleteFromFriendList(friendIDList, V2TIMFriendInfo.V2TIM_FRIEND_TYPE_BOTH, new V2TIMValueCallback<List<V2TIMFriendOperationResult>>() {@Overridepublic void onSuccess(List<V2TIMFriendOperationResult> v2TIMFriendOperationResults) {// 删除好友成功}@Overridepublic void onError(int code, String desc) {// 删除好友失败}});
// 删除好友[[V2TIMManager sharedInstance] deleteFromFriendList:@[@"userA"] deleteType:V2TIM_FRIEND_TYPE_BOTH 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_;};V2TIMStringVector userIDList;userIDList.PushBack(u8"userA");V2TIMFriendType deleteType = V2TIMFriendType::V2TIM_FRIEND_TYPE_BOTH;auto callback = new ValueCallback<V2TIMFriendOperationResultVector>{};callback->SetCallback([=](const V2TIMFriendOperationResultVector& friendOperationResultList) {// 删除好友成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 删除好友失败delete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->DeleteFromFriendList(userIDList, deleteType, callback);
V2TIMManager.getFriendshipManager().checkFriend(userIDList, V2TIMFriendInfo.V2TIM_FRIEND_TYPE_BOTH, new V2TIMValueCallback<List<V2TIMFriendCheckResult>>() {@Overridepublic void onSuccess(List<V2TIMFriendCheckResult> v2TIMFriendCheckResults) {// 检查好友关系成功for (V2TIMFriendCheckResult checkResult : v2TIMFriendCheckResults) {// 用户 IDString userID = checkResult.getUserID();// 用户和自己的好友关系int relationType = checkResult.getResultType();}}@Overridepublic void onError(int code, String desc) {// 检查好友关系失败}});
// 检查 user1 和自己的好友关系[[V2TIMManager sharedInstance] checkFriend:@[@"user1"] checkType:V2TIM_FRIEND_TYPE_BOTH succ:^(NSArray<V2TIMFriendCheckResult *> *resultList) {// 检查好友关系成功for (V2TIMFriendCheckResult *result in resultList) {// 用户 IDNSString *userID = result.userID;// 用户和自己的好友关系V2TIMFriendRelationType relationType = result.relationType;}} 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 userIDList;userIDList.PushBack(u8"userA");V2TIMFriendType checkType = V2TIMFriendType::V2TIM_FRIEND_TYPE_BOTH;auto callback = new ValueCallback<V2TIMFriendCheckResultVector>{};callback->SetCallback([=](const V2TIMFriendCheckResultVector& friendCheckResultList) {// 检查好友关系成功for (size_t i = 0; i < friendCheckResultList.Size(); ++i) {const V2TIMFriendCheckResult& friendCheckResult = friendCheckResultList[i];// 用户 IDV2TIMString userID = friendCheckResult.userID;// 用户 IDV2TIMFriendRelationType relationType = friendCheckResult.relationType;}delete callback;},[=](int error_code, const V2TIMString& error_message) {// 检查好友关系失败delete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->CheckFriend(userIDList, checkType, callback);
V2TIMFriendInfoResult
的 relation
字段可以得到该用户与自己的关系:V2TIMFriendInfoResult .relation | 与自己的关系 |
V2TIM_FRIEND_RELATION_TYPE_NONE | 不是好友。 |
V2TIM_FRIEND_RELATION_TYPE_BOTH_WAY | 互为好友。 |
V2TIM_FRIEND_RELATION_TYPE_IN_MY_FRIEND_LIST | 对方在我的好友列表中。 |
V2TIM_FRIEND_RELATION_TYPE_IN_OTHER_FRIEND_LIST | 我在对方的好友列表中。 |
List<String> userIDList = new ArrayList<>();userIDList.add("userA");V2TIMManager.getFriendshipManager().getFriendsInfo(userIDList, new V2TIMValueCallback<List<V2TIMFriendInfoResult>>() {@Overridepublic void onSuccess(List<V2TIMFriendInfoResult> v2TIMFriendInfoResults) {// 获取好友资料成功}@Overridepublic void onError(int code, String desc) {// 获取好友资料失败}});
// 获取好友资料[[V2TIMManager sharedInstance] getFriendsInfo:@[@"userA"] succ:^(NSArray<V2TIMFriendInfoResult *> *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 userIDList;userIDList.PushBack(u8"userA");auto callback = new ValueCallback<V2TIMFriendInfoResultVector>{};callback->SetCallback([=](const V2TIMFriendInfoResultVector& friendInfoResultList) {// 获取好友资料成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 获取好友资料成功delete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->GetFriendsInfo(userIDList, callback);
setFriendInfo
(Android / iOS & Mac / Windows) 接口修改好友资料,包括好友备注、好友自定义字段、好友分组等字段。详情可参考 V2TIMFriendInfo
(Android / iOS & Mac / Windows) 类定义。V2TIMFriendInfo friendInfo = new V2TIMFriendInfo();// userA 为自己的好友friendInfo.setUserID("userA");friendInfo.setFriendRemark("friendRemark");V2TIMManager.getFriendshipManager().setFriendInfo(friendInfo, new V2TIMCallback() {@Overridepublic void onSuccess() {// 设置好友资料成功}@Overridepublic void onError(int code, String desc) {// 设置好友资料失败}});
// 设置好友资料V2TIMFriendInfo *friendInfo = [[V2TIMFriendInfo alloc] init];friendInfo.userID = @"userA"; // userA 为自己的好友friendInfo.friendRemark = @"friendRemark";[[V2TIMManager sharedInstance] setFriendInfo:friendInfo 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_;};V2TIMFriendInfo info;info.userID = u8"userA";info.friendRemark = u8"friendRemark";info.modifyFlag = V2TIMFriendInfoModifyFlag::V2TIM_FRIEND_INFO_MODIFY_FLAG_REMARK;auto callback = new Callback{};callback->SetCallback([=]() {// 设置好友资料成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 设置好友资料失败delete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->SetFriendInfo(info, callback);
Relationship Check
。开启后,用户只能给好友发送消息,当用户给非好友发消息时,SDK 会报 20009 错误码。配置路径为:Applications > Your App > Chat > Configuration > Login and Message > Relationship Check。
本页内容是否解决了您的问题?