powered by Jive Software

Detect failed messages

I making a android chat app. Im use the api level 23, the Xmpp Smack lib version 4.1.8, and on server I use ejabberd, version 16.06.

I use this code to build a XMPP connection.

(TIMEOUT = 5000)

private void buildConnection() {

XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder()

.setServiceName(mServiceName)

.setHost(mServiceName)

.setConnectTimeout(TIMEOUT)

.setResource(“Smack”)

.setUsernameAndPassword(mUsername, mPassword)

.setSendPresence(true)

.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);

mConnection = new XMPPTCPConnection(builder.build());

mConnection.setPacketReplyTimeout(TIMEOUT);

chatManager = ChatManager.getInstanceFor(mConnection);

chatManager.addChatListener(this);

PingManager pingManager = PingManager.getInstanceFor(mConnection);

pingManager.setPingInterval(5);

pingManager.registerPingFailedListener(this);

ReconnectionManager reconnectionManager = ReconnectionManager.getInstanceFor(mConnection);

reconnectionManager.enableAutomaticReconnection();

mConnection.addConnectionListener(this);

if (mConnection != null) {

DeliveryReceiptManager.getInstanceFor(mConnection).addReceiptReceivedListener(n ew ReceiptReceivedListener() {

@Override

public void onReceiptReceived(String fromJid, String toJid, String receiptId, Stanza receipt) {

updateMessageStatusReceived(receipt);

Log.i(TAG, "Confirmation stanza Got: " + receipt);

}

});

}

}

(the updateMessageStatusReceived(receipt) method is only to refresh the view).

after that, I connect and authenticate.

Everthing is normal, but when the connection is down, I need to resend the failed messages when the connection is restored. I didn’t find any solution or event in Smack to detected if a message fails. Sometimes the delay of confirmation server stanza is high, and my app assumes that message fails but don’t, and the other side receive many times the same message.

I build a Message object and send using this code:

try {

Chat chat = chatManager.createChat(chatMessage.getJid());

chat.sendMessage(message);

} catch (SmackException.NotConnectedException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

When the connection is lost and I send message, no one exception is throw.

my question is: How I know the message realy fail, to try again?

Thanks, and sorry about the english.