0

In a local environment I have some Android devices that are connected with a web socket in the foreground service. This socket connection only listens for messages and pushes them to the push notification. Sometimes you may not receive messages for hours or sometimes days, so this needs to be really stable.

The push notificationworks when the screen is on when the goes in lock mode, the connection is fine for the first few minutes. If a message is pushed after 10-15 minutes, the notification message will not be displayed. As soon as another message comes up, the previous message is suddenly picked up and displayed to. After 60 minutes or more, no messages will be displayed at all. It appears that the connection is no longer intact and the server and app logs do not show that the connection is lost.

I have been working on this for more than 4 intensive days, but to my frustration this did not work. I have found many topics related to this topic, the optimization for the app turned off, send a ping every minute (StayAlive) to the server but all to no avail.

Are there alternative options for establishing a stable connection with the server via foreground service?

Please let me know.

Thank you in advance.

Riccoh
  • 359
  • 1
  • 4
  • 19
  • Unless your devices are continuously plugged into a power source, Doze mode and/or app standby are going to limit your network connectivity. That has nothing to do with websockets versus anything else. – CommonsWare Mar 26 '21 at 15:49
  • @CommonsWare so in theory it is not possible within android to create an app for internal use which is connected to a network server that checks in the background continuously for messages. Or else I have to create my own ROM? – Riccoh Mar 26 '21 at 18:37
  • It depends a lot on your device model and user behavior. There are "battery whitelist" options available in Settings to try to tell Android to leave your app alone. How well they work depends on device model -- see https://dontkillmyapp.com/ for more. And there is no requirement for any user to take those steps to whitelist your app. – CommonsWare Mar 26 '21 at 18:42

1 Answers1

0

Its fixed.

By adding the following permission in my manifest and code the app doesn't doze any more.

<uses-permission 
android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>

Java code:

public void turnOffDozeMode(Context context){  //you can use with or without passing context
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Intent intent = new Intent();
            String packageName = context.getPackageName();
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            if (pm.isIgnoringBatteryOptimizations(packageName)) // if you want to desable doze mode for this package
                intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
            else { // if you want to enable doze mode
                intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:" + packageName));
            }
            context.startActivity(intent);
        }
    }

Source: how to turn off doze mode for specific apps in marshmallow devices programmatically

Riccoh
  • 359
  • 1
  • 4
  • 19