DelieveryReceipts永远不会处理onReceiptReceived

时间:2016-06-22 03:32:02

标签: android smack

Openfire 4.0.2
smack 4.1.7
Android Studio 2.1.2

我正在尝试使用DeliveryReceipts,以便我知道该消息已被发送。但是,永远不会调用onReceiptReceived。 我做了以下事情:

DeliveryReceiptManager deliveryReceiptManager = DeliveryReceiptManager.getInstanceFor(mClientConnection);  
        deliveryReceiptManager.autoAddDeliveryReceiptRequests();  
        DeliveryReceiptManager.setDefaultAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);  

        deliveryReceiptManager.addReceiptReceivedListener(new ReceiptReceivedListener() {  
            @Override  
            public void onReceiptReceived(String fromJid, String toJid, String receiptId, Stanza receipt) {  
                log.log(Level.INFO, "OnReceiptReceived fromJid: " + fromJid + " toJid: " + toJid + " receiptId: " + receiptId);  
            }  
        }); 

发送消息:

ChatManager chatManager = ChatManager.getInstanceFor(mClientConnection);  
Message msg = new Message(username, Message.Type.chat);  
msg.setBody(message);  
final String msgReceipt = DeliveryReceiptRequest.addTo(msg);  
log.log(Level.INFO, "msgReceiptRequest: " + msgReceipt);          
Chat chat = chatManager.createChat(username, mChatMessageListener);  
chat.sendMessage(message);

从客户端发送

SENT (0): <message to='97.steve@xmpp1.call-genie.com' id='6xS4w-43' type='chat'><body>hello</body><thread>32091ad3-8da9-4758-9656-c06eb72532a2</thread><request xmlns='urn:xmpp:receipts'/></message> 

在接收客户端

<message to='97.steve@xmpp1.call-genie.com' id='6xS4w-43' type='chat'><body>hello</body><thread>32091ad3-8da9-4758-9656-c06eb72532a2</thread><request xmlns='urn:xmpp:receipts'/></message> 
永远不会处理

onReceiptReceived所以我不知道接收客户端是否收到了消息

当客户端收到我想要发回的消息时,我尝试做类似这样的事情,这可能会触发onReceiptReceived事件

chat.addMessageListener(new ChatMessageListener() {  
    @Override  
    public void processMessage(Chat chat, Message msg) {  
        if(!msg.getBody().isEmpty()) {  
            Message ms = new Message();  
            ms.addBody("EN", "delivered");  
            ms.setTo(msg.getFrom());  
            ms.setStanzaId(msg.getStanzaId());  
            DeliveryReceiptRequest.addTo(ms);  
            mClientConnection.sendStanza(ms);  
        }  
        log.log(Level.INFO, "receiveChatMessages Received prompted message: " + msg.getBody());                                  
    }  
}  

非常感谢任何建议,

2 个答案:

答案 0 :(得分:1)

我已设法通过手动发送收据来解决此问题。当客户端如何收到消息时,我已手动将收据发回给发件人。

        mChatMessageListener = new ChatMessageListener() {
            @Override
            public void processMessage(Chat chat, Message message) {
                log.log(Level.INFO, "Received message: " + message.getBody() + " from: " + chat.getParticipant());
                if(message != null || !message.getBody().isEmpty()) {
                    DeliveryReceiptManager deliveryReceiptManager = DeliveryReceiptManager.getInstanceFor(mClientConnection);
                    if(DeliveryReceiptManager.hasDeliveryReceiptRequest(message)) {
                        log.log(Level.INFO, "hasDeliveryReceiptRequest == true");

                        Stanza received = new Message();
                        received.addExtension(new DeliveryReceipt(message.getStanzaId()));
                        received.setTo(message.getFrom());
                        try {
                            mClientConnection.sendStanza(received);
                        }
                        catch(NotConnectedException ex) {
                            log.log(Level.SEVERE, "NotConnectedException: " + ex.getMessage());
                        }
                        catch(Exception ex) {
                            log.log(Level.SEVERE, "Exception: " + ex.getMessage());
                        }
                    }
......

答案 1 :(得分:0)

@Moulesh你可能已经在以后想出来了 只有在调用DeliveryReceiptRequest.addTo(message)时,DeliveryReceiptManager.hasDeliveryReceiptRequest(message)才会为true;在chat.sendMessage(消息)之后。