如何从android获取smack或xmpp中的消息传递状态

时间:2016-10-14 14:19:59

标签: android xmpp smack

这是我的代码,我在此方法中发送消息但无法在DeliveryReceipt中获取传递消息

    newChat.sendMessage(message);
            newChat.getListeners();
            newChat.addMessageListener(new ChatMessageListener() {
                @Override
                public void processMessage(Chat chat, Message message) {
                    Log.d(TAG,"DeliveryReceipt3:"+"1:"+message.getThread()+"2:"+chat.getThreadID()+"3:"+message.getBody());
                }
            });

上面的代码用于发送消息,下面的代码用于获取DeliveryReceipt

   Message m = new Message();
                m.setType(Message.Type.chat);
                m.setFrom(connection.getUser());
                m.setTo(agentId);
                m.addExtension(new DeliveryReceipt(m.getPacketID()));

                DeliveryReceipt dr = (DeliveryReceipt)m.getExtension(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE);
                Log.d(TAG,"DeliveryReceipt1: "+"1:"+dr.getElementName()+"2:"+dr.getId()+"3:"+dr.getNamespace()+"4:"+dr.toXML()+"4:"+dr.toString());
                Log.d(TAG,"DeliveryReceipt2: "+"1:"+m.getBody()+"2:"+m.getStanzaId()+"3:"+m.getThread());

2 个答案:

答案 0 :(得分:1)

成功登录后需要添加DeliveryReceiptManager

DeliveryReceiptManager dm = DeliveryReceiptManager.getInstanceFor(connection);
                    dm.setAutoReceiptMode(AutoReceiptMode.always);
                    dm.autoAddDeliveryReceiptRequests();
                    dm.addReceiptReceivedListener(new ReceiptReceivedListener() {

                    @Override
                    public void onReceiptReceived(Jid fromJid, Jid toJid, 
                      final String receiptId, Stanza receipt) {
                             // handle delivery receipt here
                            }
                      });

答案 1 :(得分:1)

首先将这些添加到您的配置中 -

DeliveryReceiptManager.setDefaultAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);
ProviderManager.addExtensionProvider(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceipt.Provider());
ProviderManager.addExtensionProvider(DeliveryReceiptRequest.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceiptRequest.Provider());

然后在XMPPTcpConnection建立后添加这些 -

DeliveryReceiptManager deliveryReceiptManager = DeliveryReceiptManager.getInstanceFor(this.connection);
deliveryReceiptManager.setAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);
deliveryReceiptManager.autoAddDeliveryReceiptRequests();

这将为所有邮件启用送达回执。

SENT:
<iq to='chat_host' id='o2huU-440' type='get'><query xmlns='http://jabber.org/protocol/disco#info'></query></iq>

RECV:
<iq from='chat_host'id='o2huU-440' to='userJID'type='result'><query xmlns='http://jabber.org/protocol/disco#info'> <feature var='urn:xmpp:receipts'/></query>

对于自动接收模式,所有消息都将与收据请求扩展一起发送。并且您将收到所有消息的送货回执。