Recent Session

Update time: 2021/09/25 14:29:59

In relation to the recent session list, SDK maintains and provides the interface for querying and monitoring changes. As long as there is any chat with a user or team ( sends or receives messages), SDK will automatically update the recent session list and notify the upper level, and the developer is not required to manually update the session list.

Recent session SessionInfo , aka session list or recent contact list, 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.

SessionInfoDescription of key parameters:

Parameter Type SessionInfo field Description
String Id ID of recent contact (friend ID, team ID, etc.)
NIMSessionType SessionType Session type (team session, p2p message)
int UnreadCount Unread count of the current session
NIMSessionCommand Command Change operation type of the current session (add, update, delete, etc., see NIMSessionCommand for details)
String MsgId The unique Id of the latest message in the current session
String Sender Sender ID of the latest message in the current session
long TimeTag Timestamp of the latest message in the current session (milliseconds)
NIMMessageType MsgType Type of the latest message (including text, image, video, audio, etc. See NIMMessageType for details)
String Content Content of the latest message in the current session
String Attach Attachment content of the latest message in the current session
object Attachment Attachment content of the recent message in current session, parsed from the field Attach
NIMMsgLogStatus Status Status of the latest message in the current session (read, unread, etc., see Message status for details)
NIMMsgLogSubStatus SubStatus) Sub-status of the latest message in the current session (played, unplayed, etc., see Message sub-status for details)
bool IsLast In batch message notification, whether it is the last change information when the message is changed or added
bool IsPinnedOnTop Whether to stick the session on top (not supported for now)
bool IsRobot Whether it is a robot message (not supported for now)
String ExtendString Local extension field, limited to 4096 in length (not supported for now)

NIMSessionTypeEnumeration value description:

Enumeration Value Description
kNIMSessionTypeP2P 0 Individual session, i.e. p2p session
kNIMSessionTypeTeam 1 Team session

NIMSessionCommandEnumeration 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 Deleted messages of a single session item
kNIMSessionCommandAllMsgDeleted 6 Deleted messages of all session items
kNIMSessionCommandAllP2PMsgDeleted 7 Deleted messages of all p2p session items
kNIMSessionCommandAllTeamMsgDeleted 8 Deleted 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 session. In the latest version, when multiple terminals log in at the same time and clear session interfaceis called, the same session will be set to 0 on other terminals as well.

Get recent session list

Get recent session list.

  • API prototype
public static void QueryAllRecentSession(QueryRecentHandler handler);
  • Parameter Description
Parameter Description
handler Asynchronous callback function for notification query results
  • Example
 NIM.Session.SessionAPI.QueryAllRecentSession((a, b) =>
    {
        if (b == null || b.SessionList == null) return;
        foreach (var item in b.SessionList)
        {
            ···
        }
    });

Monitor the change of recent session

While sending and receiving messages, SDK will update the contact information of the other party in the chat. When a message is sent or received, the 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
public static EventHandler<SessionChangedEventArgs> RecentSessionChangedHandler;

Description for the parameter SessionChangedEventArgs:

Parameter Type The field SessionChangedEventArgs Description
ResponseCode ResCode Error code (seeResponseCodefor details)
SessionInfo Info For session information, see SessionInfo
int TotalUnreadCount Sum of unread counts in all sessions
  • Example
void OnRecentSessionChangedHandler(object sender,SessionChangedEventArgs args)
{
    if (args != null)
    {
    	...
    }
}
//Monitor event notification
SessionAPI.RecentSessionChangedHandler += OnRecentSessionChangedHandler;

//If it is no longer needed, cancel monitoring of the event
SessionAPI.RecentSessionChangedHandler -= OnRecentSessionChangedHandler;

Reset unread count

To clear the unread count of the current session, callSetUnreadCountZerointerface to do the configuration, in earlier versions, after callingSetUnreadCountZerointerface, you also need to call MarkMessagesStatusReadinterface to configure the read status of message database.

  • API prototype
public static void SetUnreadCountZero(Session.NIMSessionType toType, string id, SessionChangeHandler handler)
  • Parameter Description
Parameter Description
toType Session type
id Session id (receiver account or group id)
handler Asynchronous notification callback
  • Example
//Specify the session type and session id for setting to read;

NIMSessionType sessionType = NIMSessionType.kNIMSessionTypeP2P;
string sessionId = "test1";

//Set clearing unread count of the session
NIM.Session.SessionAPI.SetUnreadCountZero(sessionType, sessionId, (a, b, c) =>
{
    ···
});

//Invoking the interface NIM.Messagelog.MessagelogAPI.MarkMessagesStatusRead is not required for the latest version.
NIM.Messagelog.MessagelogAPI.MarkMessagesStatusRead(sessionId, sessionType, (a, b, c) =>
{
    ···
});

Delete specified recent contact

Both local database and server will realize synchronization when deleting a session option!

  • API prototype
public static void DeleteRecentSession(Session.NIMSessionType toType, string id, SessionChangeHandler handler)
  • Parameter Description
Parameter Description
toType Session type
id ID of recent contact (friend ID, team ID, etc.)
handler Callback function of current operation
  • Example
NIMSessionType toType = NIMSessionType.kNIMSessionTypeP2P; //Personal session
string id = "test_account";//Session id
NIM.Session.SessionAPI.DeleteRecentSession(toType, id, (a, b, c) =>
{
    ···
});

Delete all recent contacts

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 delete all history interface

  • API prototype
public static void DeleteAllRecentSession(SessionChangeHandler handler)
  • Example
java
SessionAPI.DeleteAllRecentSession((rescode, result, totalUnreadCounts)=>
{
	...
});
Was this page helpful?
Yes
No
  • Get recent session list
  • Monitor the change of recent session
  • Reset unread count
  • Delete specified recent contact
  • Delete all recent contacts