Mqtt callback not working when activity is paused / stopped (using paho android service client)

I am using the android paho service client to receive messages from the server using Mqtt. the app works fine for both sending and receiving when work is in progress, but all messages posted when the app starts are not accepted and my Callback class is not running.

I believe this could be a problem in context and that my Callback is tied to my activity context, or I need to somehow wake up my application so that it writes a message to the database and then sleeps again.

here's my MqttCallback implementation:

public class Callback implements MqttCallback{

private Context context;
private static final String TAG = "MQTT";

public Callback(Context context) {
    this.context = context;
}

@Override
public void connectionLost(Throwable arg0) {
    MQTTClient.reconnect(); 
}

@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
}

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
    Log.i(TAG,"mesage arrived");
    //update database and store message
}

      

and heres an instance of my client:

    client = newMqttAndroidClient(context,Constants.ADDRESS+Constants.PORT,
    MqttAsyncClient.generateClientId());
    client.setCallback(new Callback(context));

      

the context sent to both is my MainActivity (this)

any guidance would be greatly appreciated.

+3


source to share





All Articles