Recent Session
Update time: 2021/09/25 13:57:02
The recent session list is maintained by SDK and SDK provides the interface for querying and monitoring changes. As long as there is any chat with a user or team (you send or receive messages), SDK will automatically update the recent session list and notify the top-level module, and the developer does not need to manually update the session list.
Recent session SessionData
is also called session list or recent contact list, which contains the contact information of recent sessions, including contact account, contact type, time of the latest message, message status, message content, number of unread messages, etc.
SessionData
Key parameters:
Parameter Type | SessionInfo field | Description |
---|---|---|
String | id_ | ID of recent contact (friend ID, team ID, etc.) |
NIMSessionType | type_ | Session type (team session, p2p message) |
int | unread_count_ | Unread count of the current session |
NIMSessionCommand | command_ | Change operation type of the current session (add, update, delete, etc., see NIMSessionCommand for details. |
string | msg_id_ | The unique ID of the latest message in the current session |
string | msg_sender_accid_ | Sender ID of the latest message in the current session |
int64_t | msg_timetag_ | Timestamp of the latest message in the current session (milliseconds) |
NIMMessageType | msg_type_ | Type of the latest message (including text, image, video, audio, etc. See NIMMessageType for details |
string | msg_content_ | Content of the latest message in the current session |
string | msg_attach_ | Attachment content of the latest message in the current session |
NIMMsgLogStatus | msg_status_ | Status of the latest message in the current session (read, unread, etc., see Message status for details. |
NIMMsgLogSubStatus | msg_sub_status_ | Sub-status of the latest message in the current session (played, unplayed, etc., see Message sub-status for details. |
bool | last_updated_msg_ | In batch message notification, specifies whether the message is the most recent one in the notifications when the message is added or updated |
bool | placed_on_top_ | Whether to pin the session (not supported now) |
bool | is_robot_session_ | Whether it is a robot message (not supported now) |
string | extend_data_ | Local extension field, limited to 4096 in length (not supported now) |
NIMSessionType
Enumeration value description:
Enumeration | Value | Description |
---|---|---|
kNIMSessionTypeP2P | 0 | Individual session, i.e. p2p session |
kNIMSessionTypeTeam | 1 | Team session |
NIMSessionCommand
Enumeration value description:
Enumeration | Value | Description |
---|---|---|
kNIMSessionCommandAdd | 0 | Add session items |
kNIMSessionCommandRemove | 1 | Delete a single session item |
kNIMSessionCommandRemoveAll | 2 | Delete all session items |
kNIMSessionCommandRemoveAllP2P | 3 | Delete all p2p session items |
kNIMSessionCommandRemoveAllTeam | 4 | Delete all team session items |
kNIMSessionCommandMsgDeleted | 5 | Delete messages of a single session item |
kNIMSessionCommandAllMsgDeleted | 6 | Delete messages of all session items |
kNIMSessionCommandAllP2PMsgDeleted | 7 | Delete messages of all p2p session items |
kNIMSessionCommandAllTeamMsgDeleted | 8 | Delete messages of all team session items |
kNIMSessionCommandUpdate | 9 | Update session item |
Note: Recent session is a local session with no roaming. Roaming is related to messages, not to recent sessions. In the latest version, when multiple terminals log in at the same time and the Set the unread count zero interface is called, the same session will be set to 0 on other terminals as well.
Get Recent Session List
- API introduction
Get recent session list.
- API prototype
static void QueryAllRecentSessionAsync(const QuerySessionListCallabck& cb, const std::string& json_extension = "");
- Parameter Description
Parameter | Description |
---|---|
cb | Asynchronous callback function for notification query results |
json_extension | Reserved field |
- Example
nim::Session::QueryAllRecentSessionAsync((([&](int total_unread_count, const SessionDataList& sessions)
{
for (auto session : sessions.sessions_)
{
...
}
});
Track the Change of Recent Session
- API introduction
While you are sending and receiving messages, SDK will update the contact information of the other party in the chat. When a message is sent or received, SDK will send out a notification regarding the latest contact update, and you can monitor the session item change by registering the event.
- API prototype
static void RegChangeCb(const ChangeCallback& cb, const std::string& json_extension = "");
CallbackChangeCallback
response parameters:
Type | Parameter | Description |
---|---|---|
NIMResCode | code | Error code (seeNIMResCode for details) |
SessionData | session | Session information, see SessionData for details |
int | total_unread_counts | Sum of unread counts in all sessions |
- Example
//Monitor event notification
nim::Session::RegChangeCb([&](NIMResCode code, const SessionData& session, int total_unread_counts)
{
...
});
//If it is no longer needed, cancel monitoring of the event
nim::Session::RegChangeCb(nullptr);
Set Unread Count Zero
- API introduction
To set the unread count zero in the current session, call the SetUnreadCountZero
interface to do the configuration, in earlier versions, after calling the SetUnreadCountZero
interface, you also need to call the MarkMessagesStatusRead
interface to configure the read status of the message database.
- API prototype
static bool SetUnreadCountZeroAsync(nim::NIMSessionType to_type, const std::string& id, const SetUnreadCountZeroCallback& cb, const std::string& json_extension = "");
- Parameter Description
Parameter | Description |
---|---|
to_type | Session type, see NIMSessionType for details |
id | Session ID (receiver account or team ID) |
cb | Asynchronous notification callback |
json_extension | Reserved extension field |
- Example
//Specify the session type and session ID for setting to read.
NIMSessionType sessionType = kNIMSessionTypeP2P;
std:string sessionId = "test1";
//Set the unread count of the session zero.
nim::Session::SetUnreadCountZeroAsync(sessionType, sessionId, [&](NIMResCode code, const SessionData& session, int total_unread_counts)
{
...
});
//In the latest version, you no longer need to call the nim::Msglog::BatchStatusReadAsync interface
//In earlier versions, you still need to call the interface as follows.
nim::Msglog::BatchStatusReadAsync(sessionId, sessionType, [&]((nim::NIMResCode res_code, const std::string& uid, nim::NIMSessionType to_type)
{
···
});
Delete Specified Recent Contact
Sessions will be deleted from the server and local database when deleting sessions
- API prototype
static bool DeleteRecentSession(nim::NIMSessionType to_type, const std::string& id, const DeleteRecentSessionCallabck& cb, const std::string& json_extension = "");
- Parameter Description
Parameter | Description |
---|---|
to_type | Session type |
id | ID of recent contact (friend ID, team ID, etc.) |
cb | Callback function of current operation |
json_extension | Reserved extension field |
- Example
NIMSessionType toType = kNIMSessionTypeP2P; //Personal session
std::string id = "test_account";//session ID
nim::Session::DeleteRecentSession(toType, ID, [&](NIMResCode code, const SessionData& session, int total_unread_counts)
{
...
});
Delete All Recent Contacts
- API introduction
Delete all session list items without setting the message database to read status. If you need to update the read status of the message database at the same time, please use the interface ofdelete all history.
- API prototype
static void DeleteAllRecentSession(const DeleteAllRecentSessionCallabck& cb, const std::string& json_extension = "");
- Parameter Description
Parameter | Description |
---|---|
cb | Callback function of current operation |
json_extension | Reserved extension field |
- Example
nim::Session::DeleteAllRecentSession([&](NIMResCode code, const SessionData& session, int total_unread_counts)
{
...
});