Recent sessions

Update time: 2024/03/07 11:13:40

Recent sessions

Overview

Recent sessions is the list of recent contacts When a message is received from a new contact, the SDK will automatically generate the local recent session for this message. The session contains the information, such as contact account, contact type, time of the last message, message status, message content, number of unread messages and more.

The most recent session prototype NIMSession:

The question mark ("?") behind a type in the following table, such as "String?" indicates that the parameter is optional.

Type Attribute Description
String sessionId The ID of a recent contact (friend account or group ID)
String? senderAccount Get the account of a sender that sends the most recent message
String? senderNickname Get the nickname of the sender that sends the most recent message
NIMSessionType sessionType Get the session type
String? lastMessageId Get the ID of the most recent message [NIMMessage.uuid]
NIMMessageType? lastMessageType Get the type of the most recent message.
NIMMessageStatus? lastMessageStatus Get the status of the most recent message.
String? lastMessageContent Get the content of the most recent message Return text for text messages. Return a brief description of messages of other types
int? lastMessageTime Get the time when the last message was sent in milliseconds.
NIMMessageAttachment? lastMessageAttachment If the last message is of custom type, get the attachment of the message. In the list of recent messages, your app can custom the description of the recent message list displayed accordingly.
int? unreadCount Get the number of unread messages
Map<String, dynamic>? extension The extension field
int? tag Set a tag used to pin a contact or sort recent sessions. SDK has no specific use of tags. You can plan the use of tags for your app.

Get recent sessions

Get recent sessions displayed in the homepage.

Get recent sessions

  • API prototype
dartclass MessageService {
    ///  Get recent sessions
    ///
    /  Get recent sessions or the list of recent contacts When a message is received from a new contact, the SDK will automatically generate the local recent session for this message.
    /// The session contains information, such as contact account, contact type, time of the last message, message status, message content, number of unread messages and more.
    ///
    /// [limit] the number of local sessions. Up to 100 sessions are allowed; if this value is not set, the full list of sessions will be returned
    Future<NIMResult<List<NIMSession>>> querySessionList([int? limit]) async {
      return _platform.querySessionList(limit);
    }
}
  • Example
dartNIMResult<List<NIMSession>> result = await NimCore.instance.messageService.querySessionList(100);

Get custom recent sessions

Filter specified message types

To filter recent messages of a specified type, invoke the following interface.

If you want the most recent message to be a non-text message, take the most recent non-text message as the most recent message using the following interface..

  • API prototype
dartclass MessageService {
    /// [filterMessageType] type of messages to be filtered
    Future<NIMResult<List<NIMSession>>> querySessionListFiltered(
        List<NIMMessageType> filterMessageTypeList);
}
  • Example
dartNIMResult<List<NIMSession>> result =
await NimCore.instance.messageService.querySessionListFiltered([NIMMessageType.text, NIMMessageType.image]);

Get the specified recent sessions

dartclass MessageService {
    /// [sessionInfo] - Session information
    Future<NIMResult<NIMSession>> querySession(NIMSessionInfo sessionInfo);
}

Create a recent sessions list

Create an empty recent session

dartclass MessageService {
    /// Create an empty session with a specified contact and store the session to the database.
    /// [sessionId] - Session ID. A user account or group ID is used
    /// [sessionType] - Session type
    /// [tag] - Session tag. For example, a pinned tag implemented in UIKit: RECENT_TAG_STICKY. You can use a custom tag or pass 0 if not required.
    /// [time] -  The time of a session in milliseconds.
    /// [linkToLastMessage] - Whether the information about the most recent message is included.
    Future<NIMResult<NIMSession>> createSession({
        required String sessionId,
        required NIMSessionType sessionType,
        int tag = 0,
        required int time,
        bool linkToLastMessage = false,
      });
}

When a message is not included in a corresponding local recent session, the following interface can be used to create a local recent session based on the message.

dartclass MessageService {
    /// Update a session object with messages The object is not supported for Windows and macOS.
    /// [message] message object
    /// [needNotify] specify whether notifications are sent
    Future<NIMResult<void>> updateSessionWithMessage({
      required NIMMessage message,
      bool needNotify = false,
    });
}

Update the recent session list

Update the recent session list

dartclass MessageService {
    /// Update a session object. The class is not supported for Windows and macOS.
    /// [needNotify] specify whether notifications are sent
    /// Only tags and extensions can be edited.
    Future<NIMResult<void>> updateSession({
      required NIMSession session,
      bool needNotify = false,
    });
}

Listen to changes of the recent sessions

When the recent sessions and its attributes change, the SDK will update the recent contact information of the corresponding chats and send a recent contact update notification.

  • API prototype
dartclass MessageService {
    /// Update the recent sessions
    Stream<List<NIMSession>> get onSessionUpdate;
}

Unread count

Get the unread count

  • API prototype
dartclass MessageService {
    /// Get the total number of all unread messages
    /// [queryType] query type
    Future<NIMResult<int>> queryTotalUnreadCount({
        NIMUnreadCountQueryType queryType = NIMUnreadCountQueryType.all
    });
}

Example

dartNIMResult<int> result = await NimCore.instance.messageService.queryTotalUnreadCount();

Delete recent sessions

Delete recent sessions

  • API prototype
dartclass MessageService {
    /// Delete a recent contact record
    /// After calling this interface, the [MessageService.onSessionDelete] notification will be triggered
    ///
    /// [deleteType] The type of messages to be deleted. Whether the message history in the local database and roaming messages are deleted.
    ///
    /// [sendAck] If the parameter is valid, the parameter specifies whether the session is marked as read on other devices.
    Future<NIMResult<void>> deleteSession({
        required NIMSessionInfo sessionInfo,
        required NIMSessionDeleteType deleteType,
        required bool sendAck,
      });
}

Notifications triggered by the observer

dartclass MessageService {
    /// The notification is sent when a session is deleted.
    Stream<NIMSession> get onSessionDelete
}
Was this page helpful?
Yes
No
  • Overview
  • Get recent sessions
  • Get recent sessions
  • Get custom recent sessions
  • Filter specified message types
  • Get the specified recent sessions
  • Create a recent sessions list
  • Create an empty recent session
  • Update the recent session list
  • Update the recent session list
  • Listen to changes of the recent sessions
  • Unread count
  • Get the unread count
  • Delete recent sessions
  • Delete recent sessions
  • Notifications triggered by the observer