Android Question [Resolved] JMQTT /Static receiver

AymanA

Active Member
Licensed User
Hi All,

I have a question that I am unable to implement in an example so seeking assistance.

The problem:
I have an mqtt app that is working perfectly fine; and working good in the background until it is killed, so for sure no other messages are being received or sent since the Starter service is killed by OS if I understand correctly.

The solution
Is the tracker example posted by Erel to keep a foreground service notification to keep the service running.

I was looking around if there are any other way as I don't like to keep a notification kept forever and can see that the static receiver will always awake the service, so I am curious if this is a valid option to use the static intent as a broadcastreceiver and to start/wake my mqtt service once there is a message either arrives or sent? if it is can someone provide an example of this?

https://www.eclipse.org/paho/files/android-javadoc/index.html
https://www.eclipse.org/paho/files/...e/paho/android/service/MqttAndroidClient.html

Static receivers
Each service module in Basic4android is made of two components. The service and a receiver. The receiver responsibility is to delegate broadcast intents to the service. For example when you call StartServiceAt, it is the receiver that actually intercepts the intent and wakes the service.

source: https://www.b4x.com/android/forum/t...cepting-sms-messages-in-the-background.20103/

If it is doable then how can I implement it, if someone can provide an example this will be great and appreciate his/her help.

Also I can see there is another library out there that is really great in which I don't need to edit the app manifest, so if I may be greedy to ask for an example using this library if it is doable :)

https://www.b4x.com/android/forum/threads/broadcastreceiver.12493/

If this is not doable, then I am just curious to know if I published a payload every 5 sec as a keep alive message to the server (and also subscribe to the same topic to receive payload as well) will this be a feasible option to keep my background service running?

My goal:
Is to keep the mqtt background service always running and never stop without having a foreground service (notification).
 

DonManfred

Expert
Licensed User
Longtime User
My goal:
Is to keep the mqtt background service always running and never stop without having a foreground service (notification).
i dont think this is posssible.

MQTT does not provide the possibility of static intent filters. A Client need to connect to the broker.

The only way of doing a long running task is a foreground service (with notification).
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
My goal:
Is to keep the mqtt background service always running and never stop without having a foreground service (notification).

That one is a pickle, your solution would have worked on older Android devices, but these days Android or should I say device makers enjoys killing services. For example Erel's tracker example works perfect on my Pixel 3XL but on my Huawei Y7 2019 it gets killed within a few hours of being started.

A foreground service with notification icon should work though with MQTT.

The only problem with releasing a app like that is what for exampl example Huawei devices, after a few hours a huge red warning triangle appears in the notification area warning users that your app is keeping the phone awake and it recommends shutting down the app. If after a few more hours you do not shut down the app Huawei Android OS will shut it down for you, that for example is what happens on my Y7 2019 with a clock widget.
 
Upvote 0

AymanA

Active Member
Licensed User
I am not sure how to thank you both for your input in this matter! I have learnt a lot already from your input in this forum and will always do!

@DonManfred I have read in one of your post to a question that WhatsApp use the FirebaseMessaging, so if this is the only solution apart from the ongoing foreground notification, then I will give it a try, I just hoped that I can only use MQTT.

@Peter Simpson I have noticed this behavior but was not sure if it is an issue on the code or an OS/vendor related (unfortunately I do not have enough expertise yet to discover such a mystery :) ) , so thank you for providing these details.
 
Upvote 0
Top