Push Notifications and Aggressive Power Management on Android

February 20th, 2018 Posted by Embarcadero, Mobile No Comment yet

If you’ve done much mobile development, you’ll know that what happens to your app when you switch away from it is very much at the mercy of the OS. While this is true on the desktop and the server as well, on mobile the OS is very focussed on maximising battery life, and so will be much more restrictive about background activity.

For the most part this is not such a big problem, once you’re aware of it. Pawel wrote an article awhile ago about responding to app lifecycle events, so that you can know when the OS is about to put your app to sleep, wake it back up again, etc. You can also create Services on Android to have longer running processes that survive separately to the app. And of course, this is why we have Push Notifications, so that the OS can start the correct application when it receives a notification, even if that application is not currently running.

Or at least that’s how it’s meant to work.

I say that because recently we had a weird problem with one of our apps. On our test devices (a selection of newer and older Samsung, HTC, Nexus, etc devices) push notifications were all working properly, but we started getting reports from some of our users that push notifications weren’t working. One common detail was that the reports were all coming from users with either Oppo, Huawei or OnePlus phones. I got hold of one of these (an Oppo R11 in this case) and indeed I could replicate this reliably. Close your app, and in most cases, push notifications would stop working.

After a bit of research it turns out that we’ve fallen victim to market competition. Most smartphone features are now a commodity. Fingerprint readers, high-res screens, octa-core CPU’s, are increasingly common, almost table-stakes. That’s why premium phones are having to resort to features like curved screens or face recognition to try and distinguish themselves. So how are you going to compete if you make lower priced, mid-market phones? Well, one big thing is battery life. If you can quote a 2 day battery life while you’re competitors are recharging every day, then you have a big advantage.

Sounds great! Battery life is a big factor for me when I buy a phone. But how do you achieve this amazing battery life?

Well, a number of manufacturers have started swapping out some of the standard Android logic that handles apps in the background and replacing it with custom, much more aggressive logic. Some of the behaviour I’ve observed is suspending apps (including related services) as soon as you switch away from them, throwing them out of memory entirely as soon as the screen is turned off and more. Stock Android will do many of these things if it has to (eg. due to low memory or power) but the Oppo was doing them even with a fully charged battery and plenty of free memory.

Fortunately it seems to be possible to change this, but it’s something that the end user will need to do. As it is customised by each phone vendor the steps will vary, but for Oppo you need to go to Settings | Battery | Energy Saver.

Then select the app in question and turn off the Background Freeze and Doze options. In my testing I needed to do both of these to make Push Notifications work reliably. As mentioned, for the other phone vendors it’ll be different, but from what I’ve seen the necessary settings are usually in amongst the Power Management settings.

It seems some popular apps (like Gmail, WhatsApp and so on) are whitelisted by default, meaning they already have these turned off. In some ways this makes it even harder to track down. You can imagine the bug reports already: “I can get notifications for Gmail, it’s just your app that doesn’t work!”

The positive out of this is that the end user now has much more control over what’s running in the background and what they are going to spend their precious battery on. I just wish there was a way to prompt for permission to automatically disable these settings on install. Or at a minimum, a standard place to locate the settings on different phones. As I said, right now I’ve only seen reports on the phones I mentioned (Oppo, Huawei and OnePlus) but I can easily see this spreading to other brands, as they all need to compete.

I guess the number of devices we need to test on just grew a little larger.


No comments yet. You should be kind and add one!

Leave a Reply