DEV Community

HarmonyOS
HarmonyOS

Posted on

How to solve the problem that background applications cannot receive server messages?

Read the original article:How to solve the problem that background applications cannot receive server messages?

Problem Description

When the application switches to the background, the WebSocket interface cannot receive information from the server, resulting in the inability to continue synchronizing itinerary orders and related information with the server.

Background Knowledge

WebSocket is a network communication protocol that allows a client and server to establish a persistent connection and perform full-duplex communication over that connection. Once connected, both the client and server can actively send data simultaneously. For more information, see: WebSocket Guide.

After the app is retired to the background, it needs to run user-perceived tasks for a long time in the background, such as playing music and navigating. To prevent the app process from being suspended and causing corresponding function abnormalities, you can apply for long-term tasks to keep the app running in the background for a long time. Please refer to: Long-term Task Guide.

Troubleshooting Process

After the application is switched to the background, the WebSocket interface cannot receive information. This may be caused by the application being killed in the background. To prevent the application from being killed after switching to the background, consider the following two aspects:

  1. Apply for background running permission to ensure that the app can run in the background.

  2. WebSocket itself is a long connection. If the application is killed when it exits to the background process, you can try to use a long-term task to keep it alive.

Analysis Conclusion

After analysis, we determined that the root cause of the issue was not using long-duration tasks, which caused background tasks to be killed. This modification resolved the issue, and the app can still maintain normal communication with the service even after it moves to the background.

Solution

According to the positioning idea, first apply for background operation permission, and then declare the corresponding type of long-term task. The specific steps are as follows:

1.Apply for background operation permission in module.json5:

{
     "name": "ohos.permission.KEEP_BACKGROUND_RUNNING",
     "reason": "$string:module_desc",
     "usedScene": {
         "abilities": [
             "EntryAbility"
         ],
         "when": "always"
     }
}Copy codeCopy code
Enter fullscreen mode Exit fullscreen mode

2.In module.json5, declare the corresponding long-time task type for the UIAbility that needs to use the long-time task:

"module": {
      "abilities": [
          {
              "backgroundModes": [
                  // Configuration items for long-term task types (multiple can be specified) 
                  "audioRecording",
                  "location"
              ], 
          }
      ],
      ...
}Copy codeCopy code
Enter fullscreen mode Exit fullscreen mode

3.If the 'location' type is declared in a long-duration task type, continuous positioning needs to be used in the code:

geoLocationManager.on('locationChange', requestInfo, locationChange);
Copy codeCopy code
Enter fullscreen mode Exit fullscreen mode

It is a very common scenario for an application to run in the background. If the application needs to stay alive in the background, it must first apply for background running permission.

If there is still a need for data communication with the server after entering the background, it is necessary to declare a long-term task for the Ability scene and set its task type correctly. Otherwise, when the system detects that the current scene of the application does not match the scene corresponding to the declared long-term task type, there is still a risk of the application process being killed.

Currently, long-term tasks support the following types:

cke_5414.png

Verification Result

After applying background operation permission and declaring the corresponding long-term task types, the application can maintain normal WebSocket communication even when running in the background.

Written by Sefa Koyuncu

Top comments (0)