1

I've developed a socket app (realtime) for Android. Everything was working fine until the Android 6 update with brings the "doze" mode. Is there a plugin that prevent from dozing? This renders my app useless since when the app goes in doze mode, the app cannot use the network connection anymore. I'm running a background mode plugin but this isn't enough, doze takes over. Thank you.

edit: following Emanuel's comments, I have found this post about it but no valid answer.

How do I add my app to the whitelist so it doesn't stop by "doze" ? I cannot find any info anywhere... except this doc, but doesn't say how to add to the whitelist. Since my app doesn't rely on GCM, I should be good, if only I can find how to add my app!

Community
  • 1
  • 1
Eric
  • 9,870
  • 14
  • 66
  • 102
  • Make sure the app is on the white list (to allow network connection) and run a foreground service (to prevent app standby)? See http://developer.android.com/training/monitoring-device-state/doze-standby.html – Emanuel Moecklin Nov 27 '15 at 13:46
  • can you be more specific please? – Eric Nov 27 '15 at 13:47
  • @Eric, this is brand new and it is possible that the Cordova team just has not gotten to it. I just stumble on this by searching `whitelist` (as related to network access - which is intended as a general security measure, not power saving.) The best thing to do is talk to the author of the `background mode` plugin. –  Nov 28 '15 at 00:25
  • @Eric, You are asking about plugin, perhaps that should be a tag. I have added it for you. FWIW: I have notes on [Android Doze](https://github.com/jessemonroy650/top-phonegap-mistakes/blob/master/android-doze.md) now. –  Nov 29 '15 at 02:53
  • @JesseMonroy650 well, I was first looking for a plugin until I found out about the whitelist. Now I'm just wondering how to add to that "list" – Eric Nov 29 '15 at 16:56
  • @Eric, you *cannot* add to the `whitelist`. You can ONLY ask the end-user to put you on the `whitelist`. After reading your post, I did some investigation. I then posted to Google Groups and Nitobi. Here is the GG post: [HEADS UP: Android 6.0 SDK-23 blocks background operations.](https://groups.google.com/forum/#!topic/phonegap/iwUAgUdg3TU) Your `whitelist` question is answered in paragraph #3. –  Nov 29 '15 at 21:10
  • @JesseMonroy650 thanks for the additional info. Google/Android should really come up with better explanation and a simpler workflow for the end user to "whitelist" it... – Eric Nov 29 '15 at 21:20
  • @Eric, I'm sure you'll see a new plugin as soon as people start to notice this. FWIW: you have the advantage on other developers. I would make the best of it. Good Luck. –  Nov 29 '15 at 21:37
  • Why are you not using a foreground service with the associated notification for your background task? This should be exempt from Doze, see http://stackoverflow.com/questions/33018306/doze-mode-and-foreground-service/ – sec_aw Dec 02 '15 at 13:10
  • @sec_aw has nothing to do with the issue. The issue is related to socket connection and Doze. – Eric Dec 02 '15 at 19:47
  • Any phonegap plugin will also be just a proxy to some android API. The doc you mentioned tells you (somewhat?) clearly as to how can you avoid doze viz: AlarmManager's new apis : setExactAndAllowWhileIdle and setAndAllowWhileIdle. Alternatively, you can choose a foreground service or High priority gcm message. The last option is to request the user directly to be whitelisted from battery optimizations. This is something that should be used as a last resort. – Rajat Sharma Mar 16 '17 at 10:58
  • For everybody commeting about foreground services (http://stackoverflow.com/questions/33018306/doze-mode-and-foreground-service/) read the comments and IT DOES NOT WORK AT ALL – Eric Mar 16 '17 at 12:36

2 Answers2

4

use this cordova plugin to White listing an Android application programmatically from battery optimize settings

To install

cordova plugin add https://github.com/thomas550i/cordova-plugin-doze-Optimize

Javascript code of usage

cordova.plugins.DozeOptimize.IsIgnoringBatteryOptimizations(function (responce){
  console.log("IsIgnoringBatteryOptimizations: "+responce);
      if(responce=="false")
      {
        cordova.plugins.DozeOptimize.RequestOptimizations(function (responce){
          console.log(responce);
        }, function (error){
        console.error("BatteryOptimizations Request Error"+error);          
        });
      }
      else
      {
        console.log("Application already Ignoring Battery Optimizations");
      }     
}, function (error){
console.error("IsIgnoringBatteryOptimizations Error"+error);    
});
Thom
  • 323
  • 1
  • 3
  • 13
-1

There is no plugin that prevent from dozing

But Users can manually configure the whitelist in Settings > Battery > Battery Optimization. Alternatively, the system provides ways for apps to ask users to whitelist them.

An app can fire the ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS intent to take the user directly to the Battery Optimization, where they can add the app.

check this : https://developer.android.com/training/monitoring-device-state/doze-standby.html

prGD
  • 1,501
  • 2
  • 12
  • 22