我刚从折旧的asmack切换到Smack 4.1。 Smack 4.1会自动发送附有错误ID的收据,这在我的情况下会导致异常。
Smack 4.1附加2个不同的身份证和收据
以下是聊天消息已收到
RECV (0): <message from='***' to='***' xml:lang='en' id='65' kind='0' type='chat'><body>vhh</body><request xmlns='urn:xmpp:receipts'/></message>
以下是Smack 4.1针对聊天消息
生成的收到的收据SENT (0): <message to='***' id='roZ7C-32' type='chat'><received xmlns='urn:xmpp:receipts' id='65'/></message>
Smack 4.1附加了2个不同的ID,其中收到的收据 id ='roZ7C-32'和 id ='65'
我的问题是:
答案 0 :(得分:0)
对于您的第一个问题,要获取已发送消息的ID,代码将为:
Message ms = new Message();
ms.addBody("EN", Messegeforsend);
DeliveryReceiptRequest.addTo(ms); //tells that you will need delivery for this message
String send_message_id= ms.getStanzaId();
您可以获取已发送邮件的ID,该ID与已发送邮件的ID相同:
deliveryReceiptManager = DeliveryReceiptManager.getInstanceFor(connection);
deliveryReceiptManager.addReceiptReceivedListener(new ReceiptReceivedListener() {
@Override
public void onReceiptReceived(String arg0, String arg1, String arg2,Stanza arg3) {
String delivered_message_id = arg2;
}
});
send_message_id将完全等于deliver_message_id,因此您可以知道哪条消息已经发送
答案 1 :(得分:0)
SMACK 4.1.0 message receive listener to write below code
try {
Message ms = new Message();
ms.addBody("EN", "deleriyed");
ms.setTo(message.getFrom());
ms.setStanzaId(message.getStanzaId());
DeliveryReceiptRequest.addTo(ms);
connection.sendStanza(ms);
} catch (NotConnectedException e) {
e.printStackTrace();
}
答案 2 :(得分:0)
您可以禁用此功能的方法只是在发送邮件时不向邮件添加收据:
最重要的方法是
processPacketExtension
//Creating message in your method
public sendMessage(Jid to, String body){
Message msg = Message(to, body);
//Create and save your deliveryID to SQLite database
deliveryID = DeliveryReceiptRequest.addTo(msg);
ChatManager.getInstanceFor(connection).chatWith(jid).send(msg);
}
//Declare your stanza listener
private MessagePacketListener messagePacketListener;
//In your cotnructor
messagePacketListener = new MessagePacketListener(context);
//Then in your login method register your stanza listener like this
public void login() throws SmackInvocationException, XmppStringprepException {
connect();
try {
if (!con.isAuthenticated()) {
....
con.addSyncStanzaListener(messagePacketListener, new StanzaTypeFilter(Message.class));
....
}
} catch(Exception e) {
....
}
}
//Your StanzaListener where you parse your incoming message and if it is without body it is Delivery receipt
//Now you have your MessagePacketListener that process the incoming messages
public class MessagePacketListener implements StanzaListener{
private Context context;
MessagePacketListener(Context context) {
this.context = context;
}
@Override
public void processStanza(Stanza packet) {
Message msg = (Message)packet;
//Message that have body
if(msg.getBodies().size() > 0){
//Text message
//Do something with message msg.getBody()
}
else{
//This must be sth like delivery receipt or Chat state msg and get your deliveryID
processPacketExtension(msg);
}
}
//processPacketExtension method that receive packet delivery ectension
private void processPacketExtension(Message msg) {
Collection<ExtensionElement> extensions = msg.getExtensions();
if (extensions != null) {
Iterator<ExtensionElement> iterator = extensions.iterator();
if (iterator.hasNext()) {
ExtensionElement extension = iterator.next();
if(extension instanceof DeliveryReceipt){
//Do sth with deliveryID
String deliveryID = ((DeliveryReceipt) extension).getId();
}
}
}
}
}