logo
In-app Chat
SDK Error Codes
Powered Byspreading
On this page

Friend management


Overview

ZIM SDK supports friend management, allowing users to directly add and delete friends, view their friends list, send friend requests to other users, accept or reject friend requests, view friend request lists, check their friend relationship with other users, and query or modify friend information.

Each user can have a maximum of 3000 friends.

Basic functions

By calling the ZIMEventHandler API, you can register callbacks to listen for friend-related events, including: onFriendListChanged, onFriendApplicationListChanged, onFriendApplicationUpdated and onFriendInfoUpdated.

Untitled
// Callback for friend list changes
static void Function(ZIM zim, List<ZIMFriendInfo> friendInfoList ,ZIMFriendListChangeAction action)? onFriendListChanged;

// Callback for friend info updates
static void Function(ZIM zim, List<ZIMFriendInfo> friendInfoList)? onFriendInfoUpdated;

// Callback for friend application list changes
static void Function(ZIM zim,List<ZIMFriendApplicationInfo> friendApplicationInfoList ,ZIMFriendApplicationListChangeAction action)? onFriendApplicationListChanged;

// Callback for friend application updates
static void Function(ZIM zim,List<ZIMFriendApplicationInfo> friendApplicationInfoList)? onFriendApplicationUpdated;
1
Copied!

Add friends directly

After logging in to the ZIM SDK, users can use the addFriend API to directly add other users as friends without requiring their consent. This allows users to set friend remarks and attributes for the added user.

Note

You can set up to 5 friend attributes, and the corresponding key values must be k0, k1, k2, k3, and k4. It is recommended that you agree in advance on the actual meanings of each attribute and maintain consistency.

The result of adding a friend is returned through the ZIMFriendAddedResult .

Untitled
// Add friend directly
try{
    ZIMFriendAddConfig config = ZIMFriendAddConfig();
    config.friendAlias = 'zego';
    config.friendAttributes['k0'] = 'v0';
    config.wording = 'Long time no see, old classmate';
    ZIMFriendAddedResult result = await ZIM.getInstance()!.addFriend("zego", config);
    // Handle success logic
} on PlatformException catch (onError){
    // Handle failure logic
    onError.code; // Handle according to the official error code table
    onError.message; // Error message
}
1
Copied!

After successfully adding a friend, the related users will receive the callback onFriendListChanged to notify them that the user has become a friend.

Batch deleting friends

After logging in to the ZIM SDK, users can use the deleteFriends API to batch delete up to 20 friends in a one-way or two-way deletion manner.

In this example, one-way deletion and two-way deletion are explained.

  • One-way deletion: If user A deletes user B in a one-way manner, user B is no longer a friend of user A, but not vice versa.
  • Two-way deletion: If user A deletes user B in a two-way manner, they are no longer friends with each other.

The result of deleting friends is returned through ZIMFriendsDeletedCallback.

Untitled
// Batch delete friends
// type is both: bilateral deletion
// type is single: unilateral deletion
try{
    ZIMFriendDeleteConfig friendDeleteConfig = ZIMFriendDeleteConfig();
    friendDeleteConfig.type = ZIMFriendDeleteType.single;
    List<String> list = [];
    list.add('zego');
    ZIMFriendsDeletedResult result = await ZIM.getInstance()!.deleteFriends(list, friendDeleteConfig);
    // Handle success logic
} on PlatformException catch (onError){
    // Handle failure logic
    onError.code; // Handle according to the official error code table
    onError.message; // Error message
}
1
Copied!

After successfully deleting a friend, depending on the deletion type, the related users will receive the callback onFriendListChanged to notify them that the user is no longer a friend.

Send friend requests

After logging in to the ZIM SDK, users can use the sendFriendApplication API to send friend requests to other users and set friend remarks and attributes.

Note

You can set up to 5 friend attributes, and the corresponding key values must be k0, k1, k2, k3, and k4. It is recommended that you agree in advance on the actual meanings of each attribute and maintain consistency.

The result of sending a friend request is returned through ZIMFriendApplicationSentResult.

Untitled
// Send friend application
try{
    ZIMFriendApplicationSendConfig config = ZIMFriendApplicationSendConfig();
    config.friendAlias = 'zego';
    config.wording = 'Hello old friend';
    ZIMFriendApplicationSentResult result = await ZIM.getInstance()!.sendFriendApplication('zego', config);
    // Handle success logic
} on PlatformException catch (onError){
    // Handle failure logic
    onError.code; // Handle according to the official error code table
    onError.message; // Error message
}
1
Copied!

The target user will receive the onFriendApplicationListChanged callback, indicating that a user has requested to become friends. The user can choose to accept or reject the request within 7 days.

Note

If you need to adjust the friend application validity period, please contact the ZEGOCLOUD technical support team.

Accept friend requests

After logging in to ZIM SDK, users can call the acceptFriendApplication API, passing the ID of the user who initiated the request, to accept a friend request.

The result of accepting a friend request is returned through ZIMFriendApplicationAcceptedResult.

Untitled
// Accept friend application
try{
    ZIMFriendApplicationAcceptConfig acceptConfig = ZIMFriendApplicationAcceptConfig();
    acceptConfig.friendAlias = 'Mark';
    acceptConfig.friendAttributes = {'k0':'SZ'};    
    ZIMFriendApplicationAcceptedResult result = await ZIM.getInstance()!.acceptFriendApplication('zego', config);
    // Handle success logic
} on PlatformException catch (onError){
    // Handle failure logic
    onError.code; // Handle according to the official error code document
    onError.message; // Error message
}
1
Copied!

Both the requesting user and the requested user will not only receive the onFriendApplicationUpdated to know that the other user has become their friend.

Reject friend requests

After logging in to ZIM SDK, users can reject friend requests by using the rejectFriendApplication API.

The result of rejecting a friend request is returned through ZIMFriendApplicationRejectedResult.

Untitled
// Reject friend application
try{
    ZIMFriendApplicationRejectConfig config = ZIMFriendApplicationRejectConfig();
    ZIMFriendApplicationRejectedResult result = await ZIM.getInstance()!.rejectFriendApplication('zego', config);
    // Handle success logic
} on PlatformException catch (onError){
    // Handle failure logic
    onError.code; // Handle according to the official error code table
    onError.message; // Error message
}
1
Copied!

Both the requesting user and the requested user will receive the onFriendApplicationUpdated callback to know that the application has been rejected.

Query friend list

After logging in to the ZIM SDK, users can use the queryFriendList API to retrieve the paginated complete friends list.

The query results are returned through ZIMFriendListQueriedResult, and the returned friends list is sorted in descending order based on the time of friend relationship creation.

Untitled
// Query friend information list
try{
    ZIMFriendListQueryConfig config = ZIMFriendListQueryConfig();
    config.count = 3000;
    config.nextFlag = 0;
    ZIMFriendListQueriedResult result = await ZIM.getInstance()!.queryFriendList(config);
    // Handle success logic
} on PlatformException catch (onError){
    // Handle failure logic
    onError.code; // Handle according to the official error code table
    onError.message; // Error message
}
1
Copied!

Query friend request list

After logging in to the ZIM SDK, users can use the queryFriendApplicationList API to retrieve the friend request list and understand the status of each request. The friend request list includes both the requests initiated by the user to other users and the requests initiated by other users to the user. The query results are returned through ZIMFriendApplicationListQueriedResult.

Untitled
// Query friend application information list
try {
    ZIMFriendApplicationListQueryConfig config = ZIMFriendApplicationListQueryConfig();
    config.count = 3000;
    config.nextFlag = 0;
    ZIMFriendApplicationListQueriedResult result = await ZIM.getInstance()!.queryFriendApplicationList(config);
    // Handle success logic
} on PlatformException catch (onError) {
    // Handle failure logic
    onError.code; // Handle according to the official error code table
    onError.message; // Error message
}
1
Copied!

More functions

Update friend alias

After logging in to the ZIM SDK, users can update the alias they have for a friend using the updateFriendAlias API.

The result of the update is returned through ZIMFriendAliasUpdatedResult.

Untitled
// Update friend alias
try {
    ZIMFriendAliasUpdatedResult result = await ZIM.getInstance()!.updateFriendAlias('A同学', 'zego');
    // Handle success logic
} on PlatformException catch (onError) {
    // Handle failure logic
    onError.code; // Handle according to the official error code table
    onError.message; // Error message
}
1
Copied!

After a successful update, the user can receive the onFriendInfoUpdated callback to know that the friend's information has been updated.

Update friend attributes

After logging in to the ZIM SDK, users can update friend attributes using the updateFriendAttributes API.

Note

You can set up to 5 friend attributes, and the corresponding key values must be k0, k1, k2, k3, and k4. It is recommended that you agree in advance on the actual meanings of each attribute and maintain consistency.

The result of updating friend attributes is returned through ZIMFriendAttributesUpdatedResult.

Untitled
// Update friend attributes
zim.updateFriendAttributes({ k1: 'v1', k2: 'v2' } , 'userID' }).then(res => {
    const friendInfo = res.friendInfo;
});
1
Copied!

After logging in to the ZIM SDK, users can update friend attributes using the onFriendInfoUpdated API.

Check friend relationships

After logging in to the ZIM SDK, users can use the checkFriendsRelation API to batch check their friend relationships with up to 20 other users.

ZEGOCLOUD allows one-way or two-way check of friendships. In this example, the friendship between users A and B is checked.

  • One-way check: Only checks whether user B is in the friend list of user A.
  • Two-way check: Checks whether users A and B are in the friend list of the other.

The result of checking friend relationships is returned through ZIMFriendsRelationCheckedResult.

Untitled
// Check friend relationship with other users
// type is both: bidirectional
// type is single: unidirectional
try {
    ZIMFriendAttributesUpdatedResult result = await ZIM.getInstance()!.updateFriendAttributes(
        {'k1':'v1','k2':'v2'}, 'zego');
    // Handle success logic
} on PlatformException catch (onError) {
    // Handle failure logic
    onError.code; // Handle according to the official error code table
    onError.message; // Error message
}
1
Copied!

Batch query friend information

After logging in to the ZIM SDK, users can use the queryFriendsInfo API to batch query information for up to 20 friends.

The result of batch querying friend information is returned through ZIMFriendsInfoQueriedResult.

Untitled
// Batch query friends info
try {
    List<String> userIDs = ['zego'];
    userIDs.add('zego');
    ZIMFriendsInfoQueriedResult result = await ZIM.getInstance()!.queryFriendsInfo(userIDs);
    // Handle success logic
} on PlatformException catch (onError) {
    // Handle failure logic
    onError.code; // Handle according to the official error code table
    onError.message; // Error message
}
1
Copied!

Search for friends

After logging in to the ZIM SDK, users can use the searchLocalFriends API to search for friends based on their usernames and aliases. Users can provide up to 5 keywords to search for friends who match all the provided keywords.

The search results are returned through ZIMFriendsSearchedResult.

Untitled
try {
    ZIMFriendSearchConfig config = ZIMFriendSearchConfig();
    config.count = 100;
    config.nextFlag = 0;
    config.isAlsoMatchFriendAlias = true;
    config.keywords.add('zego');
    ZIMFriendsSearchedResult result = await ZIM.getInstance()!.searchLocalFriends(config);
    // Handle success logic
} on PlatformException catch (onError) {
    // Handle failure logic
    onError.code; // Handle according to the official error code table
    onError.message; // Error message
}
1
Copied!

Previous

Blacklist management

Next

Online Status Subscription