问题描述
我正在尝试使用XMPP制作聊天应用程序.为此,我提到了本教程并成功实施.但是现在我无法获得服务器接收到的特定消息并更新我的本地数据库.请给我任何想法如何做到这一点.
I'm trying to make a chat application using XMPP. For this, I referred to this tutorial and successfully implemented it. But now I'm unable to get that particular message is received by the server and update my local DB. Please give me any idea how to do this.
我正在使用chatId保存一条消息.那么我可以通过哪种方式获得收据消息ID?
I'm saving a message with chatId. So from which method of smack do I get this receipt message id?
推荐答案
- 您需要按消息ID保存消息,可以通过message.getStanzaId()获取ID.
-
您必须启用流管理.
static {
XMPPTCPConnection.setUseStreamManagementDefault(true);
XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
}
发送消息时,您将为该消息添加一个ack侦听器,就像这样.
when you send out a mesage, you add an ack listener for that message, like this.
try {
if (mConnection.isSmEnabled()) {
try {
mConnection.addStanzaIdAcknowledgedListener(message.getStanzaId(), new StanzaListener() {
@Override
public void processPacket(Stanza packet) throws NotConnectedException {
updateMessageStatus(packet);
}
});
} catch (StreamManagementException.StreamManagementNotEnabledException e) {
e.printStackTrace();
}
}
mConnection.sendStanza(message);
} catch (NotConnectedException e) {
e.printStackTrace();
}
现在,在updateMessageStatus(packet)方法中,您可以通过id(packet.getStanzaId())在数据库中找到消息,并将状态从待处理"更新为已发送".
Now, inside updateMessageStatus(packet) method, you find message in your database by id (packet.getStanzaId()) and update status from "pending" to "sent".
这篇关于Android smack服务器回执ack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!