Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Sending keep-alive Messages on Android even if the device is in sleep/doze mode
QtWS25 Last Chance

Sending keep-alive Messages on Android even if the device is in sleep/doze mode

Scheduled Pinned Locked Moved Solved Mobile and Embedded
18 Posts 5 Posters 8.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wowalive
    wrote on last edited by
    #1

    Hi there,

    I have a problem with my QtApp since Android 7. I'm sending every 60 seconds a keep-alive message to a webserver via QNetworkRequest. After that, the app processes the reply from the webservice.

    Now I have the problem, that the app does not send any keep alive messages after the phone/tablet gets into standby mode. Unfortunately i cant use push notification for my purpose. So is there any known solution or approach for that? Maybe a service class which runs in bg?

    Any ideas?

    Greetings,
    wowalive

    raven-worxR 1 Reply Last reply
    0
    • W Offline
      W Offline
      Wowalive
      wrote on last edited by Wowalive
      #17

      Finally got it..
      I've experimented a lot using services in relation to my problem. Noting really helped, because of the doze mode and app standby introduced in API 23.

      The following restrictions apply to your apps while in Doze:

      • Network access is suspended.
      • The system ignores wake locks.
      • Standard AlarmManager alarms (including setExact() and setWindow()) are deferred to the next maintenance window.
        • If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
        • Alarms set with setAlarmClock() continue to fire normally — the system exits Doze shortly before those alarms fire.
      • The system does not perform Wi-Fi scans.
      • The system does not allow sync adapters to run.
      • The system does not allow JobScheduler to run.

      In API 24 and 26 further restrictions, like limited implicit broadcasts and limited gps background behavior, are added.

      Firstly I tried to use a service with a native timer and asynctask for requesting data from a server, but the network connection will cut off after a couple of minutes and app tasks will be deferred to the maintenance window of doze. This affects the app even if I whitelist the app in battery optimization and enable partial wake locks to keep the cpu awake (see https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases).

      With the AlarmManager (using setAndAllowWhileIdle() or setExactAndAllowWhileIdle()) you are able to set alarms that fire methods even if the device is in doze mode , but you cant fire alarms more than once per 9 minutes, per app. So for me this wasn't a solution, because I need to request every minute.

      So referring to the background processing overview and the app standby exceptions I decided to implement a foreground service.
      0_1528880606980_android-backgroundoptimazion.png

      This solution was exactly what I was looking for. Now everything works fine. My Qt main thread does all the processing, and a simple foreground service holds the application awake. If you use that, pay attention to the high battery consumption.

      1 Reply Last reply
      2
      • W Wowalive

        Hi there,

        I have a problem with my QtApp since Android 7. I'm sending every 60 seconds a keep-alive message to a webserver via QNetworkRequest. After that, the app processes the reply from the webservice.

        Now I have the problem, that the app does not send any keep alive messages after the phone/tablet gets into standby mode. Unfortunately i cant use push notification for my purpose. So is there any known solution or approach for that? Maybe a service class which runs in bg?

        Any ideas?

        Greetings,
        wowalive

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #2

        @Wowalive
        This is a normal App lifecycle. If you need your code to run continuously in the background you need to implement an Android service.
        But this has been discussed several times already.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        3
        • W Offline
          W Offline
          Wowalive
          wrote on last edited by
          #3

          okay thanks for the reply.

          I'm just not sure what exactly I need a service for? Only for the NetworkRequest to send the alive-message or for the timer which calls the NetworkRequest? And what about the processing of the server-request? Do I need a service for processing the data too?

          raven-worxR 1 Reply Last reply
          0
          • W Wowalive

            okay thanks for the reply.

            I'm just not sure what exactly I need a service for? Only for the NetworkRequest to send the alive-message or for the timer which calls the NetworkRequest? And what about the processing of the server-request? Do I need a service for processing the data too?

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #4

            @Wowalive
            you need a service for everything which needs to be run permanently in the background.
            If needed your app needs to request data from the service.

            @Wowalive said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

            I'm sending every 60 seconds a keep-alive message to a webserver via QNetworkRequest. After that, the app processes the reply from the webservice.

            and you mean that such an keep-alive message may contain a notification message from the server in it's response?

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            1
            • W Offline
              W Offline
              Wowalive
              wrote on last edited by
              #5

              If the server receives a keep-alive message from the mobile device, it checks if there are any new tasks / operations for the mobile client. If so, the reply (QNetworkReply) will contain these task data, which will be processed by the main thread (for example to create a popup notification).

              Due to security reasons I can't use push notification. Thats why I need to pull data regulary, even if phone is in sleep mode.

              I have read about foreground services. Do I need this for my purpose?

              raven-worxR 1 Reply Last reply
              0
              • W Wowalive

                If the server receives a keep-alive message from the mobile device, it checks if there are any new tasks / operations for the mobile client. If so, the reply (QNetworkReply) will contain these task data, which will be processed by the main thread (for example to create a popup notification).

                Due to security reasons I can't use push notification. Thats why I need to pull data regulary, even if phone is in sleep mode.

                I have read about foreground services. Do I need this for my purpose?

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #6

                @Wowalive said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

                I have read about foreground services. Do I need this for my purpose?

                see this. Not you need a background service.

                Just so you know. Using a background service like you intend to is a battery drainer.

                May i ask what security reasons exactly preventing you from using push notifications?

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                2
                • W Offline
                  W Offline
                  Wowalive
                  wrote on last edited by
                  #7

                  Okay thanks. So I need a native background service / method for sending, receiving and processing data? Did I understand it correctly?

                  @raven-worx said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

                  May i ask what security reasons exactly preventing you from using push notifications?

                  I'm working with sensitive customer data and one of the requirements is not to use privacy critical servers like googles fcm / gcm. I do not know if there is even a possibility to use push notification without accessing these servers.

                  Beyond that I have to send the gps position of the mobile device to the webservice as a payload of the alive-message every minute.

                  J.HilkJ aha_1980A 2 Replies Last reply
                  0
                  • W Wowalive

                    Okay thanks. So I need a native background service / method for sending, receiving and processing data? Did I understand it correctly?

                    @raven-worx said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

                    May i ask what security reasons exactly preventing you from using push notifications?

                    I'm working with sensitive customer data and one of the requirements is not to use privacy critical servers like googles fcm / gcm. I do not know if there is even a possibility to use push notification without accessing these servers.

                    Beyond that I have to send the gps position of the mobile device to the webservice as a payload of the alive-message every minute.

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by J.Hilk
                    #8

                    @Wowalive said in [Sending keep-alive Messages on Android even if the device is in sleep/doze mode]

                    Beyond that I have to send the gps position of the mobile device to the webservice as a payload of the alive-message every minute.

                    constant GPS usage, Background data exchange and computation, your phone battery will be empty in a matter of very few hours!

                    Also I hope this is not intended to be a cross plattform application? Because its almost guaranteed, that the app would be rejected before even getting a background service permission from Apple.


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    2
                    • W Offline
                      W Offline
                      Wowalive
                      wrote on last edited by
                      #9

                      Yes I know, but battery discharge isn't a problem for me because the devices are connected to charging stations as long as they are not in use.

                      raven-worxR 1 Reply Last reply
                      0
                      • W Wowalive

                        Okay thanks. So I need a native background service / method for sending, receiving and processing data? Did I understand it correctly?

                        @raven-worx said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

                        May i ask what security reasons exactly preventing you from using push notifications?

                        I'm working with sensitive customer data and one of the requirements is not to use privacy critical servers like googles fcm / gcm. I do not know if there is even a possibility to use push notification without accessing these servers.

                        Beyond that I have to send the gps position of the mobile device to the webservice as a payload of the alive-message every minute.

                        aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @Wowalive said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

                        I'm working with sensitive customer data and one of the requirements is not to use privacy critical servers like googles fcm / gcm. I do not know if there is even a possibility to use push notification without accessing these servers.

                        Would it be possible to send a push notification "new data is available", which in turn causes your app to request the data on a secure channel?

                        That would avoid the constant polling from phone side, and as the push notification just contains an info it should not be that critical.

                        For me it sounds like a balanced compromise. Am I missing something?

                        Qt has to stay free or it will die.

                        W 1 Reply Last reply
                        2
                        • W Wowalive

                          Yes I know, but battery discharge isn't a problem for me because the devices are connected to charging stations as long as they are not in use.

                          raven-worxR Offline
                          raven-worxR Offline
                          raven-worx
                          Moderators
                          wrote on last edited by
                          #11

                          @Wowalive
                          using a WebSocket approach would be even better.
                          The server just sends the data when there is something to send.

                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                          If you have a question please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          1
                          • aha_1980A aha_1980

                            @Wowalive said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

                            I'm working with sensitive customer data and one of the requirements is not to use privacy critical servers like googles fcm / gcm. I do not know if there is even a possibility to use push notification without accessing these servers.

                            Would it be possible to send a push notification "new data is available", which in turn causes your app to request the data on a secure channel?

                            That would avoid the constant polling from phone side, and as the push notification just contains an info it should not be that critical.

                            For me it sounds like a balanced compromise. Am I missing something?

                            W Offline
                            W Offline
                            Wowalive
                            wrote on last edited by
                            #12

                            @aha_1980

                            Yes I've thought about this approach too. It's a good compromise for updating the data in a secure and efficient way.

                            But what about updating the gps position in a given cycle? It's necessary for me, that the server is informed about the position and the connectivity state (online/offline) of the mobile device every minute or so.

                            Like I already said, battery drain isn't a problem for me though.

                            raven-worxR 1 Reply Last reply
                            0
                            • W Wowalive

                              @aha_1980

                              Yes I've thought about this approach too. It's a good compromise for updating the data in a secure and efficient way.

                              But what about updating the gps position in a given cycle? It's necessary for me, that the server is informed about the position and the connectivity state (online/offline) of the mobile device every minute or so.

                              Like I already said, battery drain isn't a problem for me though.

                              raven-worxR Offline
                              raven-worxR Offline
                              raven-worx
                              Moderators
                              wrote on last edited by raven-worx
                              #13

                              @Wowalive
                              the big advantage of a WebSocket is that only the really needed data is transferred.
                              There is only a single HTTP connection kept open. No HTTP connection negotiation data overhead for each poll request etc.
                              One possibility could be if the connection is closed you can assume that the device is disconnected.

                              With the linke i've posted in my first post you can implement the service with Qt. For the service <-> app communication you can use any IPC mechanismn (e.g. QtRemoteObjects to connect to a signal emitted from your service)

                              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                              If you have a question please use the forum so others can benefit from the solution in the future

                              1 Reply Last reply
                              1
                              • W Offline
                                W Offline
                                Wowalive
                                wrote on last edited by
                                #14

                                I created a Service, following this tutorial. The example from @BogDan-Vatra does not work for me (Program client has been terminated).

                                The example from bbernhard (https://github.com/bbernhard/qtandroidservices_example) works for me. The service starts and keeps running, but somehow I cant call any Methods from service class. I suspect that the onCreate or onStartCommand method from the service will never be called.

                                Isn't there just a service or method which pretends the main qt app from getting into app standby? So the app will keep running, even if the phone is locked?

                                raven-worxR 1 Reply Last reply
                                0
                                • W Wowalive

                                  I created a Service, following this tutorial. The example from @BogDan-Vatra does not work for me (Program client has been terminated).

                                  The example from bbernhard (https://github.com/bbernhard/qtandroidservices_example) works for me. The service starts and keeps running, but somehow I cant call any Methods from service class. I suspect that the onCreate or onStartCommand method from the service will never be called.

                                  Isn't there just a service or method which pretends the main qt app from getting into app standby? So the app will keep running, even if the phone is locked?

                                  raven-worxR Offline
                                  raven-worxR Offline
                                  raven-worx
                                  Moderators
                                  wrote on last edited by
                                  #15

                                  @Wowalive said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

                                  Isn't there just a service or method which pretends the main qt app from getting into app standby? So the app will keep running, even if the phone is locked?

                                  @raven-worx said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

                                  This is a normal App lifecycle.

                                  This is how the Android OS works. The OS decides when the app is closed to free up resources.

                                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                  If you have a question please use the forum so others can benefit from the solution in the future

                                  1 Reply Last reply
                                  4
                                  • W Offline
                                    W Offline
                                    Wowalive
                                    wrote on last edited by
                                    #16

                                    Okay thank you. Then I will try to add an service which sends the network request.

                                    Do you know, if I need a service for all the needed functions? Sending the request, receiving it and processing the data? Or will just the network request wake the app up, so my Qt code can proceed the processing?

                                    1 Reply Last reply
                                    0
                                    • W Offline
                                      W Offline
                                      Wowalive
                                      wrote on last edited by Wowalive
                                      #17

                                      Finally got it..
                                      I've experimented a lot using services in relation to my problem. Noting really helped, because of the doze mode and app standby introduced in API 23.

                                      The following restrictions apply to your apps while in Doze:

                                      • Network access is suspended.
                                      • The system ignores wake locks.
                                      • Standard AlarmManager alarms (including setExact() and setWindow()) are deferred to the next maintenance window.
                                        • If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
                                        • Alarms set with setAlarmClock() continue to fire normally — the system exits Doze shortly before those alarms fire.
                                      • The system does not perform Wi-Fi scans.
                                      • The system does not allow sync adapters to run.
                                      • The system does not allow JobScheduler to run.

                                      In API 24 and 26 further restrictions, like limited implicit broadcasts and limited gps background behavior, are added.

                                      Firstly I tried to use a service with a native timer and asynctask for requesting data from a server, but the network connection will cut off after a couple of minutes and app tasks will be deferred to the maintenance window of doze. This affects the app even if I whitelist the app in battery optimization and enable partial wake locks to keep the cpu awake (see https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases).

                                      With the AlarmManager (using setAndAllowWhileIdle() or setExactAndAllowWhileIdle()) you are able to set alarms that fire methods even if the device is in doze mode , but you cant fire alarms more than once per 9 minutes, per app. So for me this wasn't a solution, because I need to request every minute.

                                      So referring to the background processing overview and the app standby exceptions I decided to implement a foreground service.
                                      0_1528880606980_android-backgroundoptimazion.png

                                      This solution was exactly what I was looking for. Now everything works fine. My Qt main thread does all the processing, and a simple foreground service holds the application awake. If you use that, pay attention to the high battery consumption.

                                      1 Reply Last reply
                                      2
                                      • P Offline
                                        P Offline
                                        pogojotz
                                        wrote on last edited by
                                        #18

                                        @Wowalive and others: Thank you for providing this informative thread and sorry for necro bumping.

                                        I have a similar task ahead and am trying your approach. So far I managed to create a foreground service, but my activity still gets suspended as soon as the device display is off. At least the permanent TCP connection to the server cuts off.

                                        @Wowalive: How did you manage to keep your activity awake?

                                        1 Reply Last reply
                                        0

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved