I have an Android paho MQTT client that listens for sensor data and alerts the user in case of any threshold violation. I have the following in my app.gradle :
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
I register it as a service in the manifest:
<service android:name="org.eclipse.paho.android.service.MqttService" />
The service receives MQTT messages even when the app is not in the foreground.
MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
options.setCleanSession(false);
client.connect(options);
My worry is, the phone battery will drain fast due to constant background syncing with the MQTT broker. So I want to unsubscribe, and disconnect from the server at the end of the day. I would prefer to discard all the messages that were received during the night.
Can someone please point me to a tutorial on how to do this correctly ? Just calling client.disconnect()
results in multiple subscriptions and duplicate messages when I connect the next time. There must be a clean way to shut down and restart the service. Please suggest.