Contacts
After logging in to Chat, users can start adding contacts and chatting with each other. They can also manage these contacts, for example, by adding, retrieving and removing contacts. They can also add the specified user to the block list to stop receiving messages from that user.
This page shows how to use the Chat SDK to implement contact management.
Understand the tech
The Chat SDK uses ChatContactManager
to add, remove and manage contacts. Followings are the core methods:
addContact
: Adds a contact.acceptInvitation
: Accepts the contact invitation.declineInvitation
: Declines the contact invitation.deleteContact
: Deletes a contact.getAllContactsFromServer
: Retrieves a list of contacts from the server.getAllContactsFromDB
: Retrieves all contacts from the local database.addUserToBlockList
: Adds the specified user to the block list.removeUserFromBlockList
: Removes the specified user from the block list.getBlockListFromServer
: Retrieves a list of blocked users from the server.
Prerequisites
Before proceeding, ensure that you meet the following requirements:
- You have integrated the Chat SDK, initialized the SDK and implemented the functionality of registering accounts and login. For details, see Chat SDK quickstart.
- You understand the API call frequency limits as described in Limitations.
Implementation
This section shows how to manage contacts with the methods provided by the Chat SDK.
Add a contact
This section uses user A and user B as an example to describe the process of adding a peer user as a contact.
User A call addContact
to add user B as a contact:
When user B receives the contact invitation, accept or decline the invitation.
-
To accept the invitation
-
To decline the invitation
User A uses ContactEventHandler
to listen for contact events.
-
If user B accepts the invitation,
onContactInvited
is triggered. -
If user B declines the invitation,
onFriendRequestDeclined
is triggered.
Retrieve the contact list
You can retrieve the contact list from the server and from the local database. Refer to the following sample code:
Delete a contact
Call deleteContact
to delete the specified contact. To prevent mis-operation, we recommend adding a double check process before deleting the contact.
Add a user to the block list
Call addUserToBlockList
to add the specified user to the block list. Once you add a user to the block list, you can no longer receive messages from this user.
Retrieve the block list
To get the block list from the local device, call getBlockListFromDB
.
You can also retrieve the block list from the server by calling getBlockListFromServer
.
Remove a user from the block list
To remove the specified user from the block list, call removeUserFromBlockList
.