8

I am trying to use Paho Android MQTT library (downloaded from here). The sample service application works fine, the subscribe and the publish methods work fine too when the sample application is running. When I close it, the device doesn't receive message notifications anymore.

Does anyone has an experience how to modify/implement the service correctly to receive message even if the application isn't running?

frogatto
  • 28,539
  • 11
  • 83
  • 129
wyzard
  • 543
  • 5
  • 17
  • 1
    Were you ever able to solve your issue? I am running into a similar issue trying to keep the service running even after the application has been closed out. – Coova Feb 16 '15 at 14:15
  • 1
    Not yet. Unfortunately I'm too busy now (at work) to deal with this hobby project, but I'll post to you my solution, If I'll able to make it work. – wyzard Feb 17 '15 at 12:13
  • I've made some progress. I've implemented a service (START_STICKY), which contains MqttClient object (and setCallback, connect, subscribe etc.) methods. This way I achieved, that my application receive callback notifications even if my activity isn't running. Right now my problem is: on Xiaomi devices I can't implement 'start service on boot' feature, because MIUI has an in-built Security component, which denies it, but this is another issue. – wyzard Jul 25 '15 at 13:44

5 Answers5

6

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

kdoteu
  • 1,527
  • 1
  • 20
  • 26
  • 2
    If you downvote answers pls tell why. Especially when you downvote all my answers. I'm here to learn, so when there is something I can do better, tell me! – kdoteu Jul 08 '15 at 08:57
  • Thanks. I have some free time again, so I'll continue this project soon. :) – wyzard Jul 14 '15 at 08:36
  • Where did you start the service from? If the service is started from an activity then the service should be destroyed with the activity. You can, however, reconnect but like other posts have said this doesn't guarantee connection or the callbacks. – Clocker Jul 25 '15 at 23:01
  • The service should started from the Android OS as aspected from @Kane. So its independend from the activity. – kdoteu Jul 26 '15 at 07:36
  • Could you provide a sample for us please ? If I understand correctly: there is MqttService, which connects to the broker and subscribes specified topics, publishes messages etc ..., and there should be **another** own service which allows my app to get messages in background even if my activity has destroyed ? If i am right what should my background service consists of? – wyzard Jul 26 '15 at 17:18
3

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.

Kane
  • 8,035
  • 7
  • 46
  • 75
  • 1
    You may be right but it is weird that on paho official [site](https://eclipse.org/paho/clients/android/) stated "provides a long running service for handling sending and receiving messages on behalf of Android client applications when the applications main Activity may not be running" – user2814778 May 03 '15 at 10:34
1

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.

Teixi
  • 1,077
  • 8
  • 21
  • See the MQTT Paho Android Service sample here: http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.java.git/tree/org.eclipse.paho.android.service/org.eclipse.paho.android.service.sample – Teixi Jul 06 '15 at 15:18
1

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 :-

  1. 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.

  2. 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

Amey Kshirsagar
  • 241
  • 2
  • 5
0

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.

Community
  • 1
  • 1
Pravin Sonawane
  • 1,803
  • 4
  • 18
  • 32