Paho MQTT Android service wake up activity

后端 未结 5 1966
独厮守ぢ
独厮守ぢ 2021-01-02 14:36

I am trying to use Paho Android MQTT library (downloaded from here). The sample service application works fine, the subscribe and the publish meth

相关标签:
5条回答
  • 2021-01-02 14:59

    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

    0 讨论(0)
  • 2021-01-02 15:02

    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

    0 讨论(0)
  • 2021-01-02 15:04

    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.

    0 讨论(0)
  • 2021-01-02 15:08

    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.

    0 讨论(0)
  • 2021-01-02 15:09

    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.

    0 讨论(0)
提交回复
热议问题