After logging in to Agora 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 Agora Chat SDK to implement contact management.
The Agora Chat SDK uses IContactManager
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.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.Before proceeding, ensure that you meet the following requirements:
This section shows how to manage contacts with the methods provided by the Agora Chat SDK.
Use this section to understand how to send a contact invitation, listen for contact events, and accept or decline the contact invitation.
Call AddContact
to add the specified user as a contact:
SDKClient.Instance.ContactManager.AddContact(username, reason, handle: new CallBack(
onSuccess: () =>
{
},
onError: (code, desc) =>
{
}
));
Use IContactManagerDelegate
to add the following delegates. When a user receives a contact invitation, you can accept or decline the invitation.
// Inherit and instantiate IContactManagerDelegate。
public class ContactManagerDelegate : IContactManagerDelegate {
// Occurs when a contact is added.
public void OnContactAdded(string username)
{
}
// Occurs when the contact is removed.
public void OnContactDeleted(string username)
{
}
// Occurs when a contact invitation is received.
public void OnContactInvited(string username, string reason)
{
}
// Occurs when the contact invitation is accepted.
public void OnFriendRequestAccepted(string username)
{
}
// Occurs when the contact invitation is declined.
public void OnFriendRequestDeclined(string username)
{
}
}
// Call AddContactManagerDelegate to listen for contact events.
ContactManagerDelegate adelegate = new ContactManagerDelegate();
SDKClient.Instance.ContactManager.AddContactManagerDelegate(adelegate);
// Call RemoveContactManagerDelegate to remove the delegate.
SDKClient.Instance.ContactManager.RemoveContactManagerDelegate(adelegate);
After receiving OnContactInvited
, call AcceptInvitation
or DeclineInvitation
to accept or decline the invitation.
// Accept the contact invitation. Once you accept the invitation, the sender receives the OnFriendRequestAccepted callback.
SDKClient.Instance.ContactManager.AcceptInvitation(username, handle: new CallBack(
onSuccess: () =>
{
},
onError: (code, desc) =>
{
}
));
// Decline the contact invitation. Once you decline the invitation, the sender receives the OnFriendRequestDeclined callback.
SDKClient.Instance.ContactManager.DeclineInvitation(username, handle: new CallBack(
onSuccess: () =>
{
},
onError: (code, desc) =>
{
}
));
Call DeleteContact
to delete the specified contact. The deleted user receives the OnContactDeleted
callback.
SDKClient.Instance.ContactManager.DeleteContact(username, handle: new CallBack(
onSuccess: () =>
{
},
onError: (code, desc) =>
{
}
));
To get the contact list, you can call GetAllContactsFromServer
to retrieve contacts from the server. After that, you can also call GetAllContactsFromDB
to retrieve contacts from the local database.
// Retrieve a list of contacts from the server.
SDKClient.Instance.ContactManager.GetAllContactsFromServer(new ValueCallBack<List<string>>(
onSuccess: (list) =>
{
},
onError: (code, desc) =>
{
}
));
// After retrieving the contact list from the server, you can call `GetAllContactsFromDB` to get the list of contacts from the local database.
List<string>list = SDKClient.Instance.ContactManager.GetAllContactsFromDB();
You can add a specified user to your block list. Once you do that, you can still send chat messages to that user, but you cannot receive messages from them.
Call AddUserToBlockList
to add the specified user to the block list.
SDKClient.Instance.ContactManager.AddUserToBlockList(username, handle: new CallBack(
onSuccess: () =>
{
},
onError: (code, desc) =>
{
}
));
To remove the specified user from the block list, call RemoveUserFromBlockList
.
SDKClient.Instance.ContactManager.RemoveUserFromBlockList(username, handle: new CallBack(
onSuccess: () =>
{
},
onError: (code, desc) =>
{
}
));
To get the block list, call GetBlockListFromServer
to retrieve a list of blocked users from the server.
// Call `GetBlockListFromServer` to get the block list from the server.
SDKClient.Instance.ContactManager.GetBlockListFromServer(new ValueCallBack<List<string>>(
onSuccess: (list) =>
{
},
onError: (code, desc) =>
{
}
));