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?
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());
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.
}
};