Android Question [Resolved] JMQTT /Static receiver

Discussion in 'Android Questions' started by AymanA, Jul 13, 2019.

  1. AymanA

    AymanA 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).
     
  2. DonManfred

    DonManfred Expert Licensed User

    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).
     
    AymanA likes this.
  3. Peter Simpson

    Peter Simpson Expert Licensed User

    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.
     
    emexes and AymanA like this.
  4. DonManfred

    DonManfred Expert Licensed User

    Alternatively you can switch to firebaseMessaging.
    Here you do not need a running task.

    When a message arrives the App is started automatically (the Service FirebaseMessaging).
     
    AymanA and Johan Hormaza like this.
  5. AymanA

    AymanA 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.
     
  6. AymanA

    AymanA Member Licensed User

    Just thought to post this link for some explanation about MQTT and background services/KeepAlive for future readers ( I do not understand the Java code but it is useful link to read):

    https://dalelane.co.uk/blog/?p=1599
     
  7. Peter Simpson

    Peter Simpson Expert Licensed User

    That link is dated 20111 and everything that is mention in that link has in fact already been mentioned on this forum multiple times in multiple threads.

    Anyway I'm pleased that you managed to sort out your issue...
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice