I am trying to use Paho Android MQTT library (downloaded from here). The sample service application works fine, the subscribe and the publish meth
As far as I understand the topic Paho brings all you need. In my application the paho mqtt-service never stops until I want it. I registered the Paho-service in the manifest to Android with the following snippet.
‹!-- Mqtt Service --›
‹service android:name="com.ibm.android.service.MqttService" /›
This should do the trick.
More information in the description of http://www.eclipse.org/paho/files/android-javadoc/index.html
Paho's Android Library works fine. Its just difficult to maintain the connection state, once the app goes in background. To provide any other functionality like storing the connection options, topics to pub/sub during background state will need some rewrite at library level.
My observations :-
Once the connection is established, MqttService keeps the connection alive even if the app goes background. Unless the system's service manager kills the app & its services. All this is possible unless the network stays uninterrupted.
The service has its own Alarm Manager to keep itself alive.
I did an experiment and I have made a small solution that may help keep connection object accessible throughout the app's activities. Unless you get disconnected.
PS :- The solution is damn simple. But it just works like a charm.
https://github.com/ameykshirsagar/mqttconnectionpersistence
I am yet to make an implementation that keeps the MqttService alive even after the app's activity gets destroyed
Mqtt service is somehow library than general service. It just connects the given broker and subscribes specified topics by your application. The stock MqttAndroidService does not know the broker and topics at all after it's started by Android OS.
So the correct approach is having a backend service for your application, which knows the detail about broker and topics. And your service is responsible for [re]connecting Mqtt broker and subscribing topics in proper time.
If by 'close' you mean 'force stop' the app, then yes that completely stops the app and you won't receive messages (but that's probably what the user wanted anyways).
If however, by 'close' you mean the user/android killing the app, then you are not receiving notifications because you are not 'connected' the broker anymore.
A solution would be to create a custom Service
to manage connections.
Checkout my answer here for a detailed solution of what worked for me.
You should implement a background service see: http://developer.android.com/training/run-background-service/index.html
Or depending on the use case you can implement push notifications in the server side and receive them once the client is not subscribed to the MQTT broker.