{ "id": "175521", "key": "TIMOB-28120", "fields": { "issuetype": { "id": "4", "description": "An improvement or enhancement to an existing feature or task.", "name": "Improvement", "subtask": false }, "project": { "id": "10153", "key": "TIMOB", "name": "Titanium SDK/CLI", "projectCategory": { "id": "10100", "description": "Titanium and related SDKs used in application development", "name": "Client" } }, "fixVersions": [], "resolution": null, "resolutiondate": null, "created": "2020-09-10T05:28:12.000+0000", "epic": { "id": 175512, "key": "TIMOB-28114", "name": "TiAPI: Geolocation Parity", "summary": "TiAPI: Geolocation Parity", "color": { "key": "color_1" }, "done": false }, "priority": { "name": "Low", "id": "4" }, "labels": [ "android", "background", "geolocation", "parity", "permission" ], "versions": [], "issuelinks": [], "assignee": { "name": "jquick", "key": "jquick", "displayName": "Joshua Quick", "active": true, "timeZone": "America/Los_Angeles" }, "updated": "2021-02-22T19:01:03.000+0000", "status": { "description": "The issue is open and ready for the assignee to start work on it.", "name": "Open", "id": "1", "statusCategory": { "id": 2, "key": "new", "colorName": "blue-gray", "name": "To Do" } }, "components": [ { "id": "10202", "name": "Android", "description": "Android Platform" } ], "description": "*Summary:*\r\nWe currently already support location data collection in the background, but you have to request the end-user for {{\"android.permission.ACCESS_BACKGROUND_LOCATION\"}} on Android 10 and above or else it'll stop reporting data. This can only be done via the [Ti.Android.hasPermission()|https://docs.appcelerator.com/platform/latest/#!/api/Titanium.Android-method-hasPermission] and [Ti.Android.requestPermissions()|https://docs.appcelerator.com/platform/latest/#!/api/Titanium.Android-method-requestPermissions] methods, which makes it more difficult to write cross-platform code between Android and iOS.\r\n\r\n*Current Solution:*\r\nThis is how you would request background permission today on Android 10 and above.\r\n{code:javascript}\r\nconst ACCESS_COARSE_LOCATION = 'android.permission.ACCESS_COARSE_LOCATION';\r\nconst ACCESS_FINE_LOCATION = 'android.permission.ACCESS_FINE_LOCATION';\r\nconst ACCESS_BACKGROUND_LOCATION = 'android.permission.ACCESS_BACKGROUND_LOCATION';\r\nconst permissionArray = [ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION];\r\nif (Ti.Platform.Android.API_LEVEL >= 29) {\r\n\tpermissionArray.push('android.permission.ACCESS_BACKGROUND_LOCATION');\r\n}\r\nif (!Ti.Android.hasPermission(permissionArray)) {\r\n\tTi.Android.requestPermissions(permissionArray, (e) => {\r\n\t\t// Android 11 allows you to deny background permission and grant other location permissions separately.\r\n\t\t// Event object's \"success\" property will be false in this case. So, we must check individual permissions.\r\n\t\tif (!Ti.Android.hasPermission([ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION])) {\r\n\t\t\talert('Location permission denied.');\r\n\t\t} else if ((Ti.Platform.Android.API_LEVEL >= 29) && !Ti.Android.hasPermission(ACCESS_BACKGROUND_LOCATION)) {\r\n\t\t\talert('Background location access denied.');\r\n\t\t} else {\r\n\t\t\tTi.Geolocation.addEventListener('location', onLocationDataReceived);\r\n\t\t}\r\n\t});\r\n} else {\r\n\tTi.Geolocation.addEventListener('location', onLocationDataReceived);\r\n}\r\n{code}\r\n\r\n*Better Solution:*\r\nAdd support for the [Ti.Geolocation.allowsBackgroundLocationUpdates|https://docs.appcelerator.com/platform/latest/#!/api/Titanium.Geolocation-property-allowsBackgroundLocationUpdates] property. When set {{true}}, then the next time you call the [Ti.Geolocation.hasLocationPermissions()|https://docs.appcelerator.com/platform/latest/#!/api/Titanium.Geolocation-method-hasLocationPermissions] and [Ti.Geolocation.requestLocationPermissions()|https://docs.appcelerator.com/platform/latest/#!/api/Titanium.Geolocation-method-requestLocationPermissions] methods, they will automatically add the {{\"android.permission.ACCESS_BACKGROUND_LOCATION\"}} permission to the check/request for you, making the code much more portable.\r\n{code:javascript}\r\nTi.Geolocation.allowsBackgroundLocationUpdates = true;\r\nif (!Ti.Geolocation.hasLocationPermissions(Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE)) {\r\n\tTi.Geolocation.requestLocationPermissions(Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE, () => {\r\n\t\tif (e.success) {\r\n\t\t\tTi.Geolocation.addEventListener('location', onLocationDataReceived);\r\n\t\t}\r\n\t});\r\n} else {\r\n\tTi.Geolocation.addEventListener('location', onLocationDataReceived);\r\n}\r\n{code}\r\n\r\n*Notes:*\r\n# You will still need to start an Android foreground service set up for {{FOREGROUND_SERVICE_TYPE_LOCATION}} to make this work. There is no way to make this cross-platform since it involves posting a notification to the status bar.\r\n# On iOS, the \"allowsBackgroundLocationUpdates\" property only applies the first time you add your \"location\" event... versus on Android it only applies when doing the location permission checks.\r\n", "attachment": [], "flagged": false, "summary": "Android: Add Ti.Geolocation.allowsBackgroundLocationUpdates support", "creator": { "name": "jquick", "key": "jquick", "displayName": "Joshua Quick", "active": true, "timeZone": "America/Los_Angeles" }, "subtasks": [], "reporter": { "name": "jquick", "key": "jquick", "displayName": "Joshua Quick", "active": true, "timeZone": "America/Los_Angeles" }, "environment": null, "comment": { "comments": [], "maxResults": 0, "total": 0, "startAt": 0 } } }