4

I'm using OpenFire with aSmack.

I can't seem to get DeliveryReceipts working.

I do the following when I create a connection:

//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);
        }
});

I'm sending a message to another user using:

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

The above does not work although the message gets sent with the ReceiptRequest. The listener does not get fired at the sender's end although the receiver receives the message. (I confirmed it through debugging that a DeliveryReceiptRequest does indeed get attached to the message that is sent).

I've also tried manually sending back the Receipt by using the following in my PacketListener with (MessageTypeFilter(Message.Type.chat)):

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

Still, the client at the receiving end receives the message alright but the listener does not get fired in the sender's system. Anything I'm missing?

Flow
  • 23,572
  • 15
  • 99
  • 156
Bam
  • 1,183
  • 1
  • 9
  • 13
  • check if the server supports this. –  Jan 30 '14 at 15:40
  • 2
    I don't think this is a server related feature. Only the clients need to implement this. – Bam Jan 31 '14 at 05:42
  • 1
    Did you verify that the recipient sent a receipt? If so, use a debugger to trace the way of the received receipt stanza. (a)Smack is open source, so you can easily debug the code. – Flow Jan 31 '14 at 09:42
  • When the receiver receives the message in my PacketListener, there's no receipt extension attached to the message. Which is why I believe the recipient is not sending back a receipt. Should I be checking for incoming receipts where the message arrives (ie. my PacketListener with filter type Message.Type.chat)? – Bam Feb 01 '14 at 06:25
  • Debugging shows that the receiver is indeed receiving a request and sending back a receipt, and the sender is receiving that receipt. But the ReceiptReceivedListener at the sender's end is not catching it. I am adding a ReceiptReceivedListener in my ASyncTask, right after my connection logs in successfully. Should I be writing this elsewhere? – Bam Feb 04 '14 at 07:57
  • Also, I tried manually receiving receipts in my PacketListener and it works. But the packet xml does not show the receipt message id (it shows this in SMACK's debug log). Any idea why the PacketListener is removing this information from the xml? – Bam Feb 04 '14 at 07:57
  • FINALLY figured it out. Turns out it wasn't parsing the receipt message id because DeliveryReceipt extension wasn't registered. – Bam Feb 04 '14 at 13:09
  • DeliveryReceiptManager is never found in asmack-android-16-beem.jar. Should I be using any other jar? – Navya Ramesan Nov 25 '14 at 13:04
  • @Bam i am facing a problem in sending and receiving message on android device from xmpp server using asmack, Can you come over this [link](http://chat.stackoverflow.com/rooms/68853/trying-to-learn) to help me in solving problem, Thanks – nawaab saab Jan 19 '15 at 10:14

3 Answers3

10

Add these lines before you set up your connection and Auto Receipts will work with ReceiptReceivedListener. Seems like the DeliveryReceipt extensions aren't registered by default.

ProviderManager.getInstance().addExtensionProvider(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceipt.Provider());
ProviderManager.getInstance().addExtensionProvider(DeliveryReceiptRequest.ELEMENT, new DeliveryReceiptRequest().getNamespace(), new DeliveryReceiptRequest.Provider());
Bam
  • 1,183
  • 1
  • 9
  • 13
  • dude for me its throwing NPE while chatting in these line connection.sendPacket(msg); – Karthick M Apr 03 '14 at 06:44
  • @Bam simply awesome ! – dharmendra Jun 23 '14 at 14:25
  • NPE = Null Pointer Exception – Punit Jul 12 '14 at 08:37
  • DeliveryReceiptManager.getInstanceFor(connection) gives me NullPointerException !!! – Navya Ramesan Dec 02 '14 at 09:22
  • HI @Navya Ramesan please see my below answer which will help you. I faced the same problem and get rid of the NPE by using my method. – shyam.y Dec 22 '14 at 05:59
  • @sham.y Thanks. I will try that. And I got everything fixed after using the latest XMPP jar. – Navya Ramesan Jan 28 '15 at 12:46
  • I am trying to add DeliveryReceiptManager to get delivery report of sent messages. i used the above [code](http://paste.ofcode.org/36EZWyRnHQxWUVb6R8Zus5s) of yours, but its giving error on *addReceiptReceivedListener* i.e *The method addReceiptReceivedListener(new DeliveryReceiptManager.ReceiptReceivedListener(){}) is undefined for the type DeliveryReceiptManager* – nawaab saab Feb 21 '15 at 11:21
0

Hi the above said answer given me NPE, so I used following code to get message notifications which was working perfectly for me: Don't forget to add this 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.

        }


    };
shyam.y
  • 1,441
  • 1
  • 16
  • 17
  • i am facing a problem in sending and receiving message on android device from xmpp server using asmack, Can you come over this [link](http://chat.stackoverflow.com/rooms/68853/trying-to-learn) to help me in solving problem, Thanks – nawaab saab Jan 19 '15 at 10:15
  • can you please tell me in which file I put that code to get the delivered mesage status @sham.y – Sarbjyot Nov 23 '16 at 08:07
  • @sarbjyot: Sorry for the delay in response. You need to place the above code in the class where you initialize the XMPPConnection object. Let me know if you face any problem. – shyam.y Nov 28 '16 at 22:15
  • Thanks @sham.y. LMC then i will revert you ;-) – Sarbjyot Nov 29 '16 at 07:49
0

It's a bit late but this will help someone, take note of

receiptManager.setAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);

Check below code:

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
            }
        });
mut tony
  • 357
  • 7
  • 9