MsgGetMsgList
introduction
Get history message list
You can specify msg_getmsglist_param_last_msg to get message list from local message list, and msg_getmsglist_param_count is the count of the message list. You can also set msg_getmsglist_param_last_msg as NULL, and it sets the latest message in the conversation as the LastMsg.
If set kTIMMsgGetMsgListParamIsRamble as true, it will get cloud messages when local messages are undernumbered.
If set kTIMMsgGetMsgListParamIsForward as true, get messages that are newer to msg_getmsglist_param_last_msg, otherwise get messages that are older to msg_getmsglist_param_last_msg.
When get C2C messages, you can only use msg_getmsglist_param_last_msg as the start point, and if you don't specify it, IM SDK sets the latest message in the conversation as the LastMsg.
When get Group messages, you can use msg_getmsglist_param_last_msg or msg_getmsglist_param_last_msgSeq as the start point. The differences are
Use msg_getmsglist_param_last_msg as the start point, returned message list doesn't contain msg_getmsglist_param_last_msg.
Use msg_getmsglist_param_last_msgSeq as the start point, returned message list contains msg_getmsglist_param_last_msgSeq.
When getting Group messages
If specified msg_getmsglist_param_last_msg and msg_getmsglist_param_last_msgSeq, IM SDK prefers msg_getmsglist_param_last_msg as message start point.
If msg_getmsglist_param_last_msg and msg_getmsglist_param_last_msgSeq are all undefined, there are two situations
If set message period, IM SDK will use msg_getmsglist_param_time_begin as the start point.
If doesn't set message period, by default IM SDK will use the latest message in the conversation as the LastMsg.
Only the historical messages of meeting groups (Meeting) can be pulled. For more information on group message limits, see Group System.
This API does not apply to audio-video groups (AVChatRoom), as their messages are not stored on the cloud roaming server or in the local database.
When historical messages are pulled from the cloud and a network exception is detected, the SDK will return the locally stored historical messages.
Locally stored historical messages are not subject to time limits, but those stored in the cloud are subject to the following time limits:
Free edition: The free storage period is 7 days and cannot be extended.
Pro edition: The free storage period is 7 days and can be extended.
Ultimate edition: The free storage period is 30 days and can be extended.
Extending the storage period of historical messages is a value-added service. You can log in to the IM console to modify the relevant configuration. For billing details, see Pricing.
Rich media messages (such as images, files, and audios) have the same storage periods as historical messages.
The SDK has been optimized for message pull. If a lag occurs, you can reduce the number of pulled messages (msg_getmsglist_param_count). If the issue persists, contact us for assistance.
Parameter details
overload1
Parameter name | Parameter type | Required | Description |
---|---|---|---|
conv_id | string | Required | Conversation ID |
conv_type | TIMConvType | Required | Conversation type |
get_message_list_param | MsgGetMsgListParam | Required | Get history message list param |
callback | ValueCallback< List< Message > > | Required | Asynchronous callback The format of json_param in the return value is json |
overload2
Parameter name | Parameter type | Required | Description |
---|---|---|---|
conv_id | string | Required | Conversation ID |
conv_type | TIMConvType | Required | Conversation type |
get_message_list_param | MsgGetMsgListParam | Required | Get history message list param |
callback | ValueCallback< string > | Required | Asynchronous callback The format of json_param in the return value is string |
Returned template
TIMResult
The parameters of the callback function after successfully calling the API:
code:int
desc:string
json_param:
'[
{
"message_client_time" : 1551080111,
"message_conv_id" : "user2",
"message_conv_type" : 1,
"message_elem_array" : [
{
"elem_type" : 0,
"text_elem_content" : "123213213"
}
],
"message_is_from_self" : true,
"message_is_read" : true,
"message_rand" : 2130485001,
"message_sender" : "user1",
"message_seq" : 1,
"message_server_time" : 1551080111,
"message_status" : 2
},
...
]'// For the meaning of Json Key, please refer to [Message]
user_data:string
Return value details
name | type | description |
---|---|---|
TIMResult | TIMResult | Return value of calling API |
code | int | Result error code: Error Codes |
desc | string | The description of the error. |
json_param | string | Json string. Calling different API will get different Json strings. For the meaning of Json Key, please refer to Message |
user_data | string | User-defined data transfered by ImSDK without any processing |
Code example
using UnityEngine;
using UnityEngine.UI;
using com.tencent.im.unity.demo.types;
using com.tencent.imsdk.unity;
using com.tencent.imsdk.unity.types;
using com.tencent.imsdk.unity.enums;
using System;
using com.tencent.im.unity.demo.utils;
using EasyUI.Toast;
using System.Collections;
using System.Text;
using System.Collections.Generic;
public class MsgGetMsgList : MonoBehaviour
{
public Text Header;
public Text LastMessageID;
public Dropdown SelectedFriend;
public Dropdown SelectedGroup;
public Text Result;
public Button Submit;
public Button Copy;
private List<string> GroupList;
private List<string> FriendList;
private Message LastMessage;
string[] Labels = new string[] { "SelectFriendLabel", "SelectGroupLabel" };
void Start()
{
foreach (string label in Labels)
{
GameObject.Find(label).GetComponent<Text>().text = Utils.t(label);
}
GroupGetJoinedGroupListSDK();
FriendshipGetFriendProfileListSDK();
Header = GameObject.Find("HeaderText").GetComponent<Text>();
LastMessageID = GameObject.Find("LastMessageID").GetComponent<Text>();
SelectedFriend = GameObject.Find("Friend").GetComponent<Dropdown>();
SelectedGroup = GameObject.Find("Group").GetComponent<Dropdown>();
SelectedGroup.onValueChanged.AddListener(delegate
{
GroupDropdownValueChanged(SelectedGroup);
});
SelectedFriend.onValueChanged.AddListener(delegate
{
FriendDropdownValueChanged(SelectedFriend);
});
Result = GameObject.Find("ResultText").GetComponent<Text>();
Submit = GameObject.Find("Submit").GetComponent<Button>();
Copy = GameObject.Find("Copy").GetComponent<Button>();
Copy.GetComponentInChildren<Text>().text = Utils.t("Copy");
Submit.onClick.AddListener(MsgGetMsgListSDK);
Copy.onClick.AddListener(CopyText);
if (CurrentSceneInfo.info != null)
{
Header.text = Utils.IsCn() ? CurrentSceneInfo.info.apiText + " " + CurrentSceneInfo.info.apiName : CurrentSceneInfo.info.apiName;
Submit.GetComponentInChildren<Text>().text = CurrentSceneInfo.info.apiName;
}
}
void GroupDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedFriend.value = 0;
}
}
void FriendDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedGroup.value = 0;
}
}
void GetGroupList(params object[] parameters)
{
try
{
GroupList = new List<string>();
SelectedGroup.ClearOptions();
string text = (string)parameters[1];
List<GroupBaseInfo> List = Utils.FromJson<List<GroupBaseInfo>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
GroupList.Add("");
option.text = "";
SelectedGroup.options.Add(option);
foreach (GroupBaseInfo item in List)
{
print(item.group_base_info_group_id);
GroupList.Add(item.group_base_info_group_id);
option = new Dropdown.OptionData();
option.text = item.group_base_info_group_id;
SelectedGroup.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getGroupListFailed"));
}
}
void GetFriendList(params object[] parameters)
{
try
{
FriendList = new List<string>();
SelectedFriend.ClearOptions();
string text = (string)parameters[1];
List<FriendProfile> List = Utils.FromJson<List<FriendProfile>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
FriendList.Add("");
option.text = "";
SelectedFriend.options.Add(option);
foreach (FriendProfile item in List)
{
print(item.friend_profile_identifier);
FriendList.Add(item.friend_profile_identifier);
option = new Dropdown.OptionData();
option.text = item.friend_profile_identifier;
SelectedFriend.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getFriendListFailed"));
}
}
void GroupGetJoinedGroupListSDK()
{
TIMResult res = TencentIMSDK.GroupGetJoinedGroupList(Utils.addAsyncStringDataToScreen(GetGroupList));
print($"GroupGetJoinedGroupListSDK {res}");
}
void FriendshipGetFriendProfileListSDK()
{
TIMResult res = TencentIMSDK.FriendshipGetFriendProfileList(Utils.addAsyncStringDataToScreen(GetFriendList));
print($"FriendshipGetFriendProfileListSDK {res}");
}
void MsgGetMsgListSDK()
{
var get_message_list_param = new MsgGetMsgListParam
{
msg_getmsglist_param_count = 20
};
if (LastMessage != null)
{
get_message_list_param.msg_getmsglist_param_last_msg = LastMessage;
}
if (SelectedGroup.value > 0)
{
print(GroupList[SelectedGroup.value]);
TIMResult res = TencentIMSDK.MsgGetMsgList(GroupList[SelectedGroup.value], TIMConvType.kTIMConv_Group, get_message_list_param, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
else if (SelectedFriend.value > 0)
{
print(FriendList[SelectedFriend.value]);
TIMResult res = TencentIMSDK.MsgGetMsgList(FriendList[SelectedFriend.value], TIMConvType.kTIMConv_C2C, get_message_list_param, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
}
void GetResult(params object[] parameters)
{
Result.text += (string)parameters[0];
List<Message> messages = Utils.FromJson<List<Message>>((string)parameters[1]);
if (messages.Count > 0)
{
LastMessage = messages[messages.Count - 1];
LastMessageID.text = messages[messages.Count - 1].message_msg_id;
print("有lastMsg");
}
else
{
LastMessage = null;
LastMessageID.text = "";
}
}
void CopyText()
{
Utils.Copy(Result.text);
}
void OnApplicationQuit()
{
TencentIMSDK.Uninit();
}
}