How can I tell an ACK corresponds to which publish message on MQTT?
<p>I'm struggling with Mqtt paho driver...</p>

<p>I am using IMqttDeliveryToken to get an Acknowledge from the server whenever my publish has been received. </p>

<p>To compare it with the actual publish message, I set up an ID on the MqttMessage in order to retrieve it from the IMqttDeliveryToken... But it doesn't work... The IMqttDeliveryToken.getMessageId() returns an incorrect ID and when I try to get the ID after a IMqttDeliveryToken.getMessage() when the QoS is different from 0, it returns a NPE.</p>

<p>After reading the Javadoc, I read that it's the usual behavior :</p>

<blockquote>
  <p>Until the message has been delivered, the message being delivered will be returned. Once the message has been delivered null will be returned.</p>
</blockquote>

<p>Which lead me to another question... Is the deliveryComplete() method really called after an Broker sent an Acknowledgement ?</p>

<p>Here is my code :</p>

<pre><code>client.setCallback(new MqttCallback() {
    @Override
    public void connectionLost(Throwable thrwbl) { }

    @Override
    public void messageArrived(String string, MqttMessage mm) throws Exception { }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        try {
            System.out.println("Message ID from getMessageId() method : " + token.getMessageId());
            MqttMessage message = token.getMessage();
            System.out.println("Message ID from getMessage() method : " + message.getId());
        } catch (MqttException ex) {
            System.out.println(ex);
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
});

MqttMessage message = new MqttMessage();
message.setId(76);
message.setPayload("pouet".getBytes());
message.setQos(0);

client.publish("TEST", message);
</code></pre>

<p>With QoS to 0 :</p>

<pre><code>Message ID from getMessageId() method : 1
Message ID from getMessage() method : 76
</code></pre>

<p>With QoS to 1 :</p>

<pre><code>Message ID from getMessageId() method : 1
java.lang.NullPointerException
</code></pre>