如何在xamarin.forms中检索pubnub聊天的历史记录

时间:2017-09-15 06:04:14

标签: xamarin xamarin.forms pubnub

我正在尝试使用pubnub在xamarin.forms中的聊天应用程序,我成功地发送和检索消息,但问题是当我关闭应用程序并重新打开时,我没有收到以前的消息。

 {
        PNConfiguration config = new PNConfiguration();
        // never share your keys in public, especially pub-key
        config.PublishKey = "pub-c-6de-redacted";
        config.SubscribeKey = "sub-c-85f-redacted";
        config.Uuid = username;

        config.LogVerbosity = PNLogVerbosity.BODY;
        config.PubnubLog = new Logger();
        _pubnub = new Pubnub(config);

        _pubnub.AddListener(this);

        _pubnub.Subscribe<string>()
            .Channels(new string[] { "my_channel1" })
            .WithPresence()
            .Execute();
        _pubnub.HereNow()
            .Channels(new string[]
            {
                "my_channel1"
            })
            .IncludeUUIDs(true)
            .Async(new HereNowResult());

        _pubnub.History()
            .Channel("my_channel1")
            .IncludeTimetoken(true)
            .Count(100)
            .Async(new DemoHistoryResult());

我的发布方法是:

public void PublishMessage(string text,string selectednametoChat)
    {
        _pubnub.Publish()
            .Channel("my_channel1")
            .ShouldStore(true)
            .Message(text)
            .UsePOST(true)
            .Async(new PubnubResult());

    }`

以下是存储先前消息的方法 [我认为你的意思是检索以前的消息]

 public class DemoHistoryResult : PNCallback<PNHistoryResult>
{
    public static ChatWindowViewModel ChatWindowViewModel { get; set; }

    public override void OnResponse(PNHistoryResult result, PNStatus status)
    {
        try
        {
            if (result.Messages != null && result.Messages.Count > 0)
            {
                foreach (var message in result.Messages)
                {
                    ChatWindowViewModel.MessagesList.Add(message.Entry.ToString());
                }
            }
        }
        catch (Exception ex)
        {

        }

    }

如何将历史记录转到MessagesList

0 个答案:

没有答案