{ "id": "172967", "key": "AC-6128", "fields": { "issuetype": { "id": "1", "description": "A problem which impairs or prevents the functions of the product.", "name": "Bug", "subtask": false }, "project": { "id": "12217", "key": "AC", "name": "Appcelerator - INBOX", "projectCategory": { "id": "10000", "description": "", "name": "Customer Service" } }, "resolution": { "id": "8", "description": "", "name": "Needs more info" }, "resolutiondate": "2019-02-23T16:23:28.000+0000", "created": "2019-02-06T14:55:26.000+0000", "labels": [ "android", "battery", "optimization", "permissions" ], "versions": [], "issuelinks": [], "assignee": { "name": "shossain", "key": "shossain", "displayName": "Shak Hossain", "active": false, "timeZone": "America/Los_Angeles" }, "updated": "2019-02-23T16:23:29.000+0000", "status": { "description": "A resolution has been taken, and it is awaiting verification by reporter. From here issues are either reopened, or are closed.", "name": "Resolved", "id": "5", "statusCategory": { "id": 3, "key": "done", "colorName": "green", "name": "Done" } }, "components": [], "description": "In android 8.0 some background processes don't run because the app is no longer in the foreground. I would like to add this line to the tiapp.xml to ignore the battery optimization, but it doesn't work.\r\n\r\n{code:java}\r\n\r\n{code}\r\n", "attachment": [ { "id": "66135", "filename": "tiapp.xml", "author": { "name": "josh.mocek", "key": "josh.mocek", "displayName": "josh.mocek", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2019-02-06T14:46:03.000+0000", "size": 3811, "mimeType": "text/xml" } ], "flagged": false, "summary": "Android - REQUEST_IGNORE_BATTERY_OPTIMIZATIONS doesn't work", "creator": { "name": "josh.mocek", "key": "josh.mocek", "displayName": "josh.mocek", "active": true, "timeZone": "America/Los_Angeles" }, "subtasks": [], "reporter": { "name": "josh.mocek", "key": "josh.mocek", "displayName": "josh.mocek", "active": true, "timeZone": "America/Los_Angeles" }, "environment": "Android 8.0\r\nBattery Optimization", "comment": { "comments": [ { "id": "445877", "author": { "name": "sdarda", "key": "sdarda", "displayName": "Sharif AbuDarda", "active": false, "timeZone": "Asia/Dhaka" }, "body": "Hello [~josh.mocek], Can you please share a sample code/project for us to test the issue? Thanks.", "updateAuthor": { "name": "sdarda", "key": "sdarda", "displayName": "Sharif AbuDarda", "active": false, "timeZone": "Asia/Dhaka" }, "created": "2019-02-06T16:42:24.000+0000", "updated": "2019-02-06T16:42:24.000+0000" }, { "id": "445878", "author": { "name": "josh.mocek", "key": "josh.mocek", "displayName": "josh.mocek", "active": true, "timeZone": "America/Los_Angeles" }, "body": "I can, but the only thing that's going to happen is you make the boiler plate app and replace that tiapp.xml with the one I attached.", "updateAuthor": { "name": "josh.mocek", "key": "josh.mocek", "displayName": "josh.mocek", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2019-02-06T16:52:20.000+0000", "updated": "2019-02-06T16:52:20.000+0000" }, { "id": "445897", "author": { "name": "jquick", "key": "jquick", "displayName": "Joshua Quick", "active": true, "timeZone": "America/Los_Angeles" }, "body": "You should be using a \"foreground\" service. That's what Google prefers because it involves displaying a notification in the top status bar which informs the end-user that your app is doing work in the background.\r\n\r\nWe added foreground service support in Titanium 7.3.0.\r\nHave a look at the below for a code example.\r\nhttps://github.com/appcelerator/titanium_mobile/pull/10076\r\n\r\nAlso note that you'll need to add the following Android permission to use a foreground service.\r\n{code:xml}\r\n\r\n{code}", "updateAuthor": { "name": "jquick", "key": "jquick", "displayName": "Joshua Quick", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2019-02-07T00:35:48.000+0000", "updated": "2019-02-07T00:37:29.000+0000" }, { "id": "445940", "author": { "name": "josh.mocek", "key": "josh.mocek", "displayName": "josh.mocek", "active": true, "timeZone": "America/Los_Angeles" }, "body": "So I have set an interval to create an http request to the server every 10 minutes. If I turn battery optimization off they run. If Android 8 is optimizing the battery it doesn't. I tested that foreground service and it didn't fix the problem.", "updateAuthor": { "name": "josh.mocek", "key": "josh.mocek", "displayName": "josh.mocek", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2019-02-08T17:13:29.000+0000", "updated": "2019-02-08T17:13:29.000+0000" }, { "id": "445947", "author": { "name": "jquick", "key": "jquick", "displayName": "Joshua Quick", "active": true, "timeZone": "America/Los_Angeles" }, "body": "Okay. I took a deeper dive into this. I think you have to worry about doze mode too which kill network communications. And doze mode ignores wake-locks too.\r\nhttps://developer.android.com/training/monitoring-device-state/doze-standby#restrictions\r\n\r\nThe \"REQUEST_IGNORE_BATTERY_OPTIMIZATIONS\" manifest permission isn't enough. You also have to request the end-user's permission as well. Details about this can be found below. It involves displaying a system dialog via a {{startActivity()}} and an {{Intent}}, which you *+can+* do in JavaScript. Just note that the end-user can say \"no\" and ban your app from ever requesting this again.\r\nhttps://commonsware.com/blog/2015/11/11/google-anti-trust-issues.html\r\n\r\nSo, you need to add the permission in your \"tiapp.xml\". Titanium blindly copies whatever permission you add here, so this part was already working.\r\n{code:xml}\r\n\r\n\r\n\t\r\n\t\t\r\n\t\t\t\r\n\t\t\r\n\t\r\n\r\n{code}\r\n\r\nAnd you can request the end-user to give your app permission as follows.\r\n{code:javascript}\r\nfunction requestIgnoreBatteryOptimizations() {\r\n\tif (Ti.Platform.Android.API_LEVEL >= 23) {\r\n\t\tvar intent = Ti.Android.createIntent({\r\n\t\t\taction: \"android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS\",\r\n\t\t\tdata: \"package:\" + Ti.App.Android.launchIntent.packageName,\r\n\t\t});\r\n\t\tTi.Android.currentActivity.startActivity(intent);\r\n\t}\r\n}\r\n{code}\r\n\r\nUnfortunately, Google doesn't appear to provide any feedback on which option (\"Allow\" or \"Deny\") was selected in the request dialog shown by the above code. Google doesn't return a \"result\" when launched via {{startActivityForResult()}}. Nor are there any native listeners. It looks like your only option is to poll the Java [PowerManager.isIgnoringBatteryOptimizations()|https://developer.android.com/reference/android/os/PowerManager#isIgnoringBatteryOptimizations(java.lang.String)] method via hyperloop.\r\n\r\nDoing the above appears to be your only option. And it appears to be frowned upon by Google too and they'll likely question it if they ever review your app to be \"featured\" on Google Play. Google's list of what they think is acceptable for an app to do this can be found below.\r\nhttps://developer.android.com/training/monitoring-device-state/doze-standby#whitelisting-cases\r\n", "updateAuthor": { "name": "jquick", "key": "jquick", "displayName": "Joshua Quick", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2019-02-08T20:12:41.000+0000", "updated": "2019-02-08T20:12:41.000+0000" }, { "id": "446007", "author": { "name": "rmitro", "key": "rmitro", "displayName": "Rakhi Mitro", "active": false, "timeZone": "America/Los_Angeles" }, "body": "Hello [~josh.mocek],\r\n\r\nDid you find our last reply helpful for your progress? Let us know the updates from your end.", "updateAuthor": { "name": "rmitro", "key": "rmitro", "displayName": "Rakhi Mitro", "active": false, "timeZone": "America/Los_Angeles" }, "created": "2019-02-12T11:21:25.000+0000", "updated": "2019-02-12T11:21:25.000+0000" }, { "id": "446014", "author": { "name": "josh.mocek", "key": "josh.mocek", "displayName": "josh.mocek", "active": true, "timeZone": "America/Los_Angeles" }, "body": "Ti.Android.currentActivity.startActivity is always throws a red screen for me", "updateAuthor": { "name": "josh.mocek", "key": "josh.mocek", "displayName": "josh.mocek", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2019-02-12T16:02:45.000+0000", "updated": "2019-02-12T16:02:45.000+0000" }, { "id": "446019", "author": { "name": "sdarda", "key": "sdarda", "displayName": "Sharif AbuDarda", "active": false, "timeZone": "Asia/Dhaka" }, "body": "Hello [~josh.mocek], Did you try the code guide provided by Joshua? Can you share the log of the error? also the error screenshot will be helpful. Thanks.", "updateAuthor": { "name": "sdarda", "key": "sdarda", "displayName": "Sharif AbuDarda", "active": false, "timeZone": "Asia/Dhaka" }, "created": "2019-02-12T16:54:32.000+0000", "updated": "2019-02-12T16:54:32.000+0000" } ], "maxResults": 8, "total": 8, "startAt": 0 } } }