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 setEventHandler API, you can register callbacks to listen for friend-related events, including: onFriendListChanged, onFriendApplicationListChanged, onFriendApplicationUpdated and onFriendInfoUpdated.

Untitled
// Friend list change callback
public void onFriendListChanged(ZIM zim, ArrayList<ZIMFriendInfo> friendInfoList,
                                    ZIMFriendListChangeAction action) {}

// Friend information change callback
public void onFriendInfoUpdated(ZIM zim, ArrayList<ZIMFriendInfo> friendInfoList) {}

// Friend application list change callback
public void
    onFriendApplicationListChanged(ZIM zim,
                                   ArrayList<ZIMFriendApplicationInfo> friendApplicationInfoList,
                                   ZIMFriendApplicationListChangeAction action) {}

// Friend application information change callback
public void
    onFriendApplicationUpdated(ZIM zim,
                               ArrayList<ZIMFriendApplicationInfo> friendApplicationInfoList) {}
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 ZIMFriendAddedCallback .

Untitled
// Add friends directly
ZIMFriendAddConfig config = new ZIMFriendAddConfig();
config.friendAlias = "zego"
config.friendAttributes.put("k0", "v0");
config.wording = "How are you";

ZIM.getInstance().addFriend("zego", config, new ZIMFriendAddedCallback() {
    @Override
    public void onFriendAddedCallback(ZIMFriendInfo friendInfo, ZIMError zimError) {
    }
);
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 deleting friends
// When the type is set to BOTH: Perform a two-way deletion
// When the type is set to SINGLE: Perform a one-way deletion
ZIMFriendDeleteConfig zimFriendDeleteConfig = new ZIMFriendDeleteConfig();
zimFriendDeleteConfig.type = ZIMFriendDeleteType.BOTH;    
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("zego");                
ZIM.getInstance().deleteFriends(arrayList, zimFriendDeleteConfig, new ZIMFriendsDeletedCallback() {
       @Override
       public void onFriendsDeletedCallback(ArrayList<ZIMErrorUserInfo> errorUserList, ZIMError zimError) {
             // Delete result callback.
       }
});
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 ZIMFriendApplicationSentCallback.

Untitled
// Send friend requests
ZIMFriendApplicationSendConfig config = new ZIMFriendApplicationSendConfig();
config.friendAlias = "zego"
config.wording = "How are you";
ZIM.getInstance().sendFriendApplication("zego", config, new ZIMFriendApplicationSentCallback() {
    @Override
    public void onFriendApplicationSentCallback( ZIMFriendApplicationInfo applicationInfoList, ZIMError errorInfo) {
           // Handling friend request results
    }
});
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.

Untitled
// Received friend request list callback
public void onFriendApplicationListChanged(ZIM zim,
                                   ArrayList<ZIMFriendApplicationInfo> friendApplicationInfoList, ZIMFriendApplicationListChangeAction action) {

}
1
Copied!

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 ZIMFriendApplicationAcceptedCallback.

Untitled
// Accept friend requests
ZIMFriendApplicationAcceptConfig config = new ZIMFriendApplicationAcceptConfig();
config.friendAlias = "zego";
config.friendAttributes = new 
ZIM.getInstance().acceptFriendApplication("zego", config, new ZIMFriendApplicationAcceptedCallback() {
    @Override
    public void onFriendApplicationAccepted(ZIMFriendInfo friendInfo, ZIMError zimError) {
                                       
   }
});
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 ZIMFriendApplicationRejectedCallback.

Untitled
// Reject friend requests
ZIMFriendApplicationRejectConfig config = new ZIMFriendApplicationRejectConfig();
ZIM.getInstance().rejectFriendApplication("zego", config, new ZIMFriendApplicationRejectedCallback() {
    @Override
    public void onFriendApplicationRejected(ZIMUserInfo zimUserInfo, ZIMError zimError) {
          // Rejecting friend request result                  
    }
});
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 ZIMFriendListQueriedCallback, and the returned friends list is sorted in descending order based on the time of friend relationship creation.

Untitled
// Query Friends List
ZIMFriendListQueryConfig config = new ZIMFriendListQueryConfig();
config.count = 3000;
config.nextFlag = 0;
ZIM.getInstance().queryFriendList(config, new ZIMFriendListQueriedCallback() {
            @Override
            public void onFriendListQueried(ArrayList<ZIMFriendInfo> friendList, int nextFlag, ZIMError errorInfo) {
                
}
});
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 ZIMFriendApplicationListQueriedCallback.

Untitled
// Query friend application information list
ZIMFriendApplicationListQueryConfig config = new ZIMFriendApplicationListQueryConfig();
config.count = 3000;
config.nextFlag = 0;
ZIM.getInstance().queryFriendApplicationList(config, new ZIMFriendApplicationListQueriedCallback() {
    @Override
    public void onFriendApplicationListQueried(ArrayList<ZIMFriendApplicationInfo> applicationList, int nextFlag, ZIMError errorInfo) {                                                                                                            
    }                                                
});
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 ZIMFriendAliasUpdatedCallback.

Untitled
// Update friend alias
ZIM.getInstance().updateFriendAlias("Classmate A", "zego", new ZIMFriendAliasUpdatedCallback() {
    @Override
    public void onFriendAliasUpdated(ZIMFriendInfo friendInfo, ZIMError zimError) {

    }
});
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 ZIMFriendAttributesUpdatedCallback.

Untitled
HashMap<String, String> friendAttributes = new HashMap<String, String>();
friendAttributes.put("k0","v0");
// Update friend attributes
zim.updateFriendAttributes(friendAttributes, "zego", new ZIMFriendAttributesUpdatedCallback(){
    
     public void onFriendAttributesUpdated(ZIMFriendInfo friendInfo, ZIMError errorInfo)   {
                   
     }
});

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 ZIMFriendsRelationCheckedCallback.

Untitled
// Check friend relationships with other users
// When the type is set to BOTH: Perform a two-way check
// When the type is set to SINGLE: Perform a one-way check
ArrayList<String> userIDs = new ArrayList<>();
userIDs.add("zego");
ZIMFriendRelationCheckConfig config = new ZIMFriendRelationCheckConfig();
config.type = ZIMFriendRelationCheckType.BOTH;
ZIM.getInstance().checkFriendsRelation(userIDs, config, new ZIMFriendsRelationCheckedCallback() {
    @Override
    public void onFriendsChecked(ArrayList<ZIMFriendRelationInfo> relationInfos, ArrayList<ZIMErrorUserInfo> errorUserList, ZIMError errorInfo) {
                                                                
   }
});
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 ZIMFriendsInfoQueriedCallback.

Untitled
// Batch query friend information
ArrayList<String> userIDs = new ArrayList<>();
userIDs.add("zego");
ZIM.getInstance().queryFriendsInfo(userIDs, new ZIMFriendsInfoQueriedCallback() {
    @Override
    public void onFriendsInfoQueried(ArrayList<ZIMFriendInfo> friendInfos, ArrayList<ZIMErrorUserInfo> errorUserList, ZIMError errorInfo) {

    }
});
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 ZIMFriendsSearchedCallback.

Untitled
ZIMFriendSearchConfig config = new ZIMFriendSearchConfig();
config.count = 100;
config.nextFlag = 0;
config.isAlsoMatchFriendAlias = true;
config.keywords.add("zego");
ZIM.getInstance().searchLocalFriends(config, new ZIMFriendsSearchedCallback() {
    @Override
    public void onFriendsSearched(ArrayList<ZIMFriendInfo> friendInfos, int nextFlag, ZIMError errorInfo) 
    {           
    }
});
1
Copied!

Previous

Blacklist management

Next

Online Status Subscription