Search code examples
c#xamarinsdkquickblox

Quickblox chat setting save_to_history to true in Xamarin C# SDK


I'm using Quickblox C# SDK. I want to send message to a specific dialog. It's not well documented in Xamarin specific documentation. I decided to visit REST API documentation. As I could learn from there

By using Chat 2.0, you are not automatically storing your messages. Also a dialog entity won't be created/updated without saving a message to history.

I can infer if I set save_to_history to 1, chat dialog will be automatically created and message will be stored in the backend. However I couldn't figure out how I should specify that in C# SDK, cause extraParam in this method signature

public void SendMessage(int userId, string body, string extraParams, string dialogId, string subject = null, Quickblox.Sdk.Modules.ChatXmppModule.Models.MessageType messageType = Quickblox.Sdk.Modules.ChatXmppModule.Models.MessageType.Chat)

is just a string. I've dug into disassembled code and after some investigation understood that internally this parameter is used as XML so I tried these two options

var extraParams = "<extraParams> " +
                      "<save_to_history>1</save_to_history> " +
                  "</extraParams>";

And Also

var extraParams = "<save_to_history>1</save_to_history> ";

But none of these worked. Anybody has idea how I should specify the extraParam?

Regards


Solution

  • The issue was simply that I forgot to call connect before I was sending a message. Here is the method to send a message

    public async Task SendMessageAsync(IUser sender, IChatMessage message, string channelID, CancellationToken token)
        {
            await loginIfRequired(sender, token);
            var jsonMessage = JsonConvert.SerializeObject(message);
            var recipientID = await getQuickbloxUserId(message.RecipientID, token);
            var extraParams = "<extraParams> " +
                                "<save_to_history>1</save_to_history> " +
                              "</extraParams>";
            _quickblox.ChatXmppClient.SendMessage(recipientID, jsonMessage, extraParams, channelID);
        }
    

    Inside loginIfRequired I call

    _quickblox.ChatXmppClient.Connect(_currentUserID.Value, password);
    

    And everything worked fine and the dialog was created. Hope this will help someone.