问题
Using Xamarin, I installed the QuickBlox Nuget (1.2.7): https://www.nuget.org/packages/Quickblox.Sdk/
and using this tutorial: http://quickblox.com/developers/Sample-chat-xamarin this tutorial is probably out of date, as some of the code snippets do not work out of the box and some changes to classes names and functions needs to be done, but it's the best source I could find for Xamarin and QuicBlox.
I'm trying to send and receive messages and having some difficulties. While I can send messages successfully and see them in the chat history using the admin panel, i couldn't find a way to receive them with the message text.
The message received with message id, sender id etc, but the messageText field is always null. When I use getMessagesAsync to actively get the message (by its id or as part of the chat dialog history) i get a message with text. So the text is there, the event get called but the text stays null...
I tried different ways of sending and receiving messages, using:
Send Messages - both worked, the message was sent and can be seen using the admin panel:
1.
quickbloxClient.ChatXmppClient.SendMessage(...)
2.
privateChatManager.SendMessage (messageText);
Receive Messages - only the ChatXmppClient worked, the message received with all its details but the message text field is null:
1.
quickbloxClient.ChatXmppClient.MessageReceived += MessageReceived;
public async void MessageReceived (object sender, MessageEventArgs messageEventArgs)
{
if (messageEventArgs.MessageType == Xmpp.Im.MessageType.Chat && messageEventArgs.Message != null)
{
var message = messageEventArgs.Message; // contains message's details
var text = messageEventArgs.Message.messageText; // null
}
}
/// received (the event fired) with message details but without message text (null)
2.
privateChatManager.MessageReceived += MessageReceived;
/// the event doesn't get called
Any help would be appreciated.
回答1:
I know I am late to answer, but hope it might help somebody else. I was facing similar issue with my application. After a lot of efforts I came to know that there was something wrong with Quickblox Nuget I was using, which was 1.2.7. I downgraded it to 1.2.2 in all projects (ie, PCL, droid and ios) then rebuilt my project and bingo! It worked.
回答2:
Workaround code: Sending -
var extraParamsList = new List<System.Xml.Linq.XElement> ();
extraParamsList.Add (new System.Xml.Linq.XElement ((System.Xml.Linq.XName)"save_to_history", 1));
extraParamsList.Add (new System.Xml.Linq.XElement ((System.Xml.Linq.XName)"send_to_chat", 1));
extraParamsList.Add (new System.Xml.Linq.XElement ((System.Xml.Linq.XName)"message_text", messageTextEncoded));
var extraParams = new System.Xml.Linq.XElement ((System.Xml.Linq.XName)EXTRA_PARAMS, extraParamsList.ToArray ());
quickbloxClient.ChatXmppClient.SendMessage (recieverId, messageText, extraParams, dialogId, null, Xmpp.Im.MessageType.Chat);
Receiving -
var messageText = string.Empty;
/// In case there are more needed fields
foreach (var element in responseMessage.ExtraParameters.Elements ())
{
var name = element.Name.LocalName;
var value = element.Value;
if (!string.IsNullOrWhiteSpace (name) && !string.IsNullOrWhiteSpace (value))
{
switch (name)
{
case "message_text":
messageText = value
default:
break;
}
}
}
回答3:
In the official documentation of Quickblox they have mentioned that before start chating in a group dialog you should join this dialog.
But now it's not supporting Xamarin anymore so there is no any documentation available using which one can try to make it work by joining group. but if you downgrade it to 1.2.2 version as suggested by @Dhruv Bhagat it will work.
来源:https://stackoverflow.com/questions/40040098/how-to-get-message-text-from-xmpp-message-streem-using-quickblox-xamarin-nuget