MQTT with Cordova and PAHO Android Service

126 views
Skip to first unread message

Robert Andrei

unread,
Sep 29, 2017, 2:11:48 PM9/29/17
to MQTT
Hi all,
I'm trying to make a push notification system for android with MQTT to avoid the use of google servers.
My application is made with Cordova, so if you need, you can make plugins in JAVA as a native application, so Cordova should not be a problem.
I use PAHO Android Service library as client and for the plugin I use this project https://github.com/khejing/cordova-plugin-mqtt-service.
I have all that I need, I can make the connection with the server, I can publish, I can subscribe, etc...
But when I close the application, or when I press the power button the connection with the server is lost.
Basically an android service is always running and I can check that the service is running in Android Studio but for some reason I lose the connection.
The PAHO library is widely used so I don't think that's the problem, so I think the problem is in my plugin, but the plugin is so basic that there is no place for error.
This is my connection code:

private void connect(JSONArray args, CallbackContext callbackContext){
String serverURI, clientId;
boolean cleanSession;

try{
serverURI = args.getString(0);
JSONObject opts = args.getJSONObject(1);
clientId = opts.getString("clientId");
cleanSession = opts.getBoolean("clean");
}catch(JSONException e){
Log.e(this.getClass().getCanonicalName(), "JSONException Occured");
callbackContext.error("JSON error");
return;
}

client = new MqttAndroidClient(cordova.getActivity(), serverURI, clientId, new MemoryPersistence());
client.setCallback(callback);
client.setTraceCallback(new MqttTraceCallback());
MqttConnectOptions conOpt = new MqttConnectOptions();
conOpt.setCleanSession(cleanSession);
try {
client.connect(conOpt, callbackContext, new ActionListener());
Log.i("MQTTService", "mqtt connect success return");
} catch (MqttException e) {
Log.e(this.getClass().getCanonicalName(), "MqttException Occured", e);
callbackContext.error(e.toString());
}
}

Can anyone give me some help?
Reply all
Reply to author
Forward
0 new messages