送货回执请求不适用于XMPP Android(aSmack)

时间:2014-01-30 13:36:15

标签: android xmpp chat asmack

我使用OpenFire和aSmack。

我似乎无法让DeliveryReceipts正常工作。

创建连接时,我执行以下操作:

//create connection
connection.login(username, password);
DeliveryReceiptManager.getInstanceFor(connection).enableAutoReceipts();
DeliveryReceiptManager.getInstanceFor(connection).addReceiptReceivedListener(new ReceiptReceivedListener()
{
        @Override
        public void onReceiptReceived(String arg0, String arg1, String arg2)
        {
            Log.v("app", arg0 + ", " + arg1 + ", " + arg2);
        }
});

我使用以下方式向其他用户发送消息:

//send chat
Message msg = new Message(id, Message.Type.chat);
msg.setBody(chat);
DeliveryReceiptManager.addDeliveryReceiptRequest(msg);
connection.sendPacket(msg);

虽然邮件是通过ReceiptRequest发送的,但上述操作无效。尽管接收者收到消息,但听众不会在发送者端被解雇。 (我通过调试确认了它确实将DeliveryReceiptRequest附加到发送的消息上。)

我还尝试在PacketListener中使用以下内容手动发送回收据(MessageTypeFilter(Message.Type.chat)):

Packet received = new Message();
received.addExtension(new DeliveryReceipt(packet.getPacketID()));
received.setTo(packet.getFrom());
getConnection().sendPacket(received);

但是,接收端的客户端仍然可以收到消息,但是收件人不会在发件人的系统中被解雇。我失踪了什么?

3 个答案:

答案 0 :(得分:10)

在设置连接之前添加这些行,自动收据将与ReceiptReceivedListener一起使用。似乎默认情况下未注册DeliveryReceipt扩展名。

ProviderManager.getInstance().addExtensionProvider(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceipt.Provider());
ProviderManager.getInstance().addExtensionProvider(DeliveryReceiptRequest.ELEMENT, new DeliveryReceiptRequest().getNamespace(), new DeliveryReceiptRequest.Provider());

答案 1 :(得分:0)

嗨上面给出的答案给了我NPE,所以我使用下面的代码来获取对我来说完美的消息通知: 不要忘记添加此ProviderManager

  ProviderManager pm = ProviderManager.getInstance();
 pm.addExtensionProvider("x", "jabber:x:event",new MessageEventProvider()); 

MessageEventManager messageEventManager = new MessageEventManager(connection);
                        messageEventManager.addMessageEventNotificationListener(m_messageEventNotificationListener);
                        messageEventManager.addMessageEventRequestListener(m_DefaultMessageEventRequestListener);

/*
     * Notifies when message events happend
     * 
     */

    MessageEventNotificationListener m_messageEventNotificationListener = new MessageEventNotificationListener() {

        @Override
        public void deliveredNotification(String fromJID, String messageID) {
            final String _messageID =messageID;
            System.out.println("deliveredNotification");
            UiUtility.printMe("Message("+messageID+") Was Delivered to "+fromJID);
//WRITE YOUR CUSTOM METHOD WHEN MSG DELIVERED NOTFICATIONN RECIEVED.
        }

        @Override
        public void displayedNotification(String string, String string1) {

        }

        @Override
        public void composingNotification(String string, String string1) {


        }

        @Override
        public void offlineNotification(String string, String string1) {


        }

        @Override
        public void cancelledNotification(String string, String string1) {


        }

    };

    /*
     * Send a request when message events occured
     */

    DefaultMessageEventRequestListener m_DefaultMessageEventRequestListener = new DefaultMessageEventRequestListener() {

        @Override
        public void deliveredNotificationRequested(String from, String packetID, MessageEventManager messageEventManager) {
            super.deliveredNotificationRequested(from, packetID, messageEventManager); //To change body of generated methods, choose Tools | Templates.

        }


        @Override
        public void displayedNotificationRequested(String from, String packetID, MessageEventManager messageEventManager) {
            super.displayedNotificationRequested(from, packetID, messageEventManager); //To change body of generated methods, choose Tools | Templates.

        }

        @Override
        public void offlineNotificationRequested(String from, String packetID, MessageEventManager messageEventManager) {
            super.offlineNotificationRequested(from, packetID, messageEventManager); //To change body of generated methods, choose Tools | Templates.

        }

        @Override
        public void composingNotificationRequested(String from, String packetID, MessageEventManager messageEventManager) {
            super.composingNotificationRequested(from, packetID, messageEventManager); //To change body of generated methods, choose Tools | Templates.

        }


    };

答案 2 :(得分:0)

有些晚了,但这对某人有帮助,请注意

receiptManager.setAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);

检查以下代码:

DeliveryReceiptManager receiptManager = DeliveryReceiptManager.getInstanceFor(connection);

        //automatically enable delivery receipts to every message
        receiptManager.autoAddDeliveryReceiptRequests();
        receiptManager.setAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);
        receiptManager.addReceiptReceivedListener(new ReceiptReceivedListener() {
            @Override
            public void onReceiptReceived(Jid fromJid, Jid toJid, String receiptId, Stanza receipt) {

                //handle delivery receipts here
            }
        });