Server Session Service
Update time: 2021/12/03 17:04:32
Server sessions are different from the local recent sessions. You can fetch server sessions from the cloud. You cannot sync the server sessions with the local recent sessions.
-
The server stores all the session histories and save the most recent message.
-
A client can get a session list from the server. You can specify the number of sessions that you want to get. The sessions that you fetch from the server do not merge with the local sessions.
-
Unread count is not supported.
The RecentSession prototype of the recent session object contains information about user's recent contacts, such as account, date of the most recent message, extension field, and message content.
RecentSession interface:
Return | Method | Description |
---|---|---|
String | getSessionId() | Get the session ID (friends' accounts and team IDs.) |
long | getUpdateTime() | Get the timestamp of the recent message |
String | getExt() | Get the extension field |
String | getLastMsg() | Get the most recent message in JSON format |
RevokeMsgNotification | getRevokeNotification() | If the final message is "Recall Notification" and returned value is not null , you can get a RevokeMsgNotification example using this interface. The example only contains partial content or is null. |
RecentContact | toRecentContact | Create RecentContact with RecentSession information. |
Pair<SessionTypeEnum, String> | parseSessionId | Separate ID and type from session. |
int | getLastMsgType | Type of recent message: 0 or "missing" represents ordinary message. 1 represents RevokeMsgNotification. |
The RecentSessionList interface records a page of RecentSession data and shows whether earlier messages exist.
RecentSessionList interface:
Return | Method | Description |
---|---|---|
boolean | hasMore() | Is there any early message? |
List<RecentSession> |
getSessionList() | Acquired a page of session list |
Getting the session list
- API prototype
/**
* [Session service] incrementally get sessions list; increment + page turning
*
* @param minTimestamp - Min. timestamp. It indicates to incrementally get Session List as a request parameter. If the value is 0, it indicates to get in full acquisition.
* @param maxTimestamp - Max, timestamp, used for page-turning.
* @param needLastMsg - It determines that lastMsg is needed. The value is 0 or 1 and the default value is 1.
* @param limit Result set limit, with max. value 100 and default value 100.
* @param hasMore It determines that the result set is complete. The value is 0 or 1.
* @return InvocationFuture
* @see com.netease.nimlib.biz.constant.ITalkService.SessionReqTag
*/
InvocationFuture<RecentSessionList> queryMySessionList(long minTimestamp, Long maxTimestamp, Integer needLastMsg, Integer limit, Integer hasMore);
- Parameters
Parameter | Description |
---|---|
minTimestamp | The minimum timestamp; get Session List under increment; 0 represents full acquisition. |
maxTimestamp | The maximum timestamp, used for page turning. |
needLastMsg | Determine that lastMsg is needed (0 or 1; 1 by default) |
limit | Result set limit, The maximum value is 100. The default value is 100. |
hasMore | Determine that results are complete, 0 or 1. |
- Example
NIMClient.getService(MsgService.class).queryMySessionList(startTime, endTime, 1, queryAmount, 1).setCallback(new RequestMySessionListCallback(queryAmount, startTime, this::showSessionList));
Getting a specified session
- API prototype
/**
* Get a certain session
*
* @param sessionId - Including peer-to-peer, team, and superteam, i.e. peer-to-peer|accid, team|tid, super_team|tid
* @return InvocationFuture
* @see com.netease.nimlib.biz.constant.ITalkService.SessionTag
*/
InvocationFuture<RecentSession> queryMySession(@NonNull String sessionId);
- Parameters
Parameter | Description |
---|---|
sessionId | peer-to-peer, team, and superteam |
- Example
NIMClient.getService(MsgService.class).queryMySession(sessionId).setCallback(new RequestCallback<RecentSession>() {
@Override
public void onSuccess(RecentSession param) {
resultTV.setText(sessionToString(param));
}
@Override
public void onFailed(int code) {
Toast.makeText(SessionActivity2.this, "Failed in getting a session", Toast.LENGTH_SHORT).show();
}
@Override
public void onException(Throwable exception) {
exception.printStackTrace();
Toast.makeText(SessionActivity2.this, "Exception in getting a session", Toast.LENGTH_SHORT).show();
}
});
Updating the extension field of server sessions
- API prototype
/**
* [Session service] Update a certain session. It is to set ext field of the session. If there is no such session, it will be created, of which lastMsg cannot be found.
*
* @param sessionId - Including peer-to-peer, team, and superteam, i.e. peer-to-peer|accid, team|tid, super_team|tid
* @param ext - Extension field of a session. It is visible to yourself only.
* @return InvocationFuture
* @see com.netease.nimlib.biz.constant.ITalkService.SessionTag
*/
InvocationFuture<Void> updateMySession(@NonNull String sessionId, @NonNull String ext);
- Parameters
Parameter | Description |
---|---|
sessionId | Peer-to-peer, team, and superteam |
ext | Extension field of the session, visible to developers only. |
- Example
NIMClient.getService(MsgService.class).updateMySession(sessionIdET.getText().toString(), extET.getText().toString()).setCallback(new RequestCallback<Void>() {
@Override
public void onSuccess(Void param) {
Toast.makeText(SessionActivity2.this, "Successfully update a session", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailed(int code) {
Toast.makeText(SessionActivity2.this, "Failed in updating a session", Toast.LENGTH_SHORT).show();
}
@Override
public void onException(Throwable exception) {
Toast.makeText(SessionActivity2.this, "Exception in updating a session", Toast.LENGTH_SHORT).show();
}
});
Deleting a server session
- API prototype
/**
* Delete a session
*
* @param sessionIdArr - Format of each option, including peer-to-peer, team, and superteam, i.e. peer-to-peer|accid, team|tid, super_team|tid
* @return InvocationFuture
* @see com.netease.nimlib.biz.constant.ITalkService.SessionTag
*/
InvocationFuture<Void> deleteMySession(@NonNull String[] sessionIdArr);
- Parameters
Parameter | Description |
---|---|
sessionIdArr | peer-to-peer, team, and superteam |
- Example
NIMClient.getService(MsgService.class).deleteMySession(selectedArr).setCallback(new RequestCallback<Void>() {
@Override
public void onSuccess(Void param) {
Toast.makeText(SessionActivity2.this, "Successfully delete a session", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailed(int code) {
Toast.makeText(SessionActivity2.this, "Failed in deleting a session", Toast.LENGTH_SHORT).show();
}
@Override
public void onException(Throwable exception) {
exception.printStackTrace();
Toast.makeText(SessionActivity2.this, "Exception in deleting a session", Toast.LENGTH_SHORT).show();
}
});