{"action":"created","issue":{"url":"https://api.github.com/repos/appcelerator/titanium_mobile_windows/issues/566","repository_url":"https://api.github.com/repos/appcelerator/titanium_mobile_windows","labels_url":"https://api.github.com/repos/appcelerator/titanium_mobile_windows/issues/566/labels{/name}","comments_url":"https://api.github.com/repos/appcelerator/titanium_mobile_windows/issues/566/comments","events_url":"https://api.github.com/repos/appcelerator/titanium_mobile_windows/issues/566/events","html_url":"https://github.com/appcelerator/titanium_mobile_windows/pull/566","id":135729081,"number":566,"title":"Implement Background Service","user":{"login":"infosia","id":1661068,"avatar_url":"https://avatars.githubusercontent.com/u/1661068?v=3","gravatar_id":"","url":"https://api.github.com/users/infosia","html_url":"https://github.com/infosia","followers_url":"https://api.github.com/users/infosia/followers","following_url":"https://api.github.com/users/infosia/following{/other_user}","gists_url":"https://api.github.com/users/infosia/gists{/gist_id}","starred_url":"https://api.github.com/users/infosia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/infosia/subscriptions","organizations_url":"https://api.github.com/users/infosia/orgs","repos_url":"https://api.github.com/users/infosia/repos","events_url":"https://api.github.com/users/infosia/events{/privacy}","received_events_url":"https://api.github.com/users/infosia/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-02-23T12:53:20Z","updated_at":"2016-03-14T09:07:32Z","closed_at":"2016-02-29T15:15:05Z","pull_request":{"url":"https://api.github.com/repos/appcelerator/titanium_mobile_windows/pulls/566","html_url":"https://github.com/appcelerator/titanium_mobile_windows/pull/566","diff_url":"https://github.com/appcelerator/titanium_mobile_windows/pull/566.diff","patch_url":"https://github.com/appcelerator/titanium_mobile_windows/pull/566.patch"},"body":"First cut for background service implementation. [TIMOB-18952](https://jira.appcelerator.org/browse/TIMOB-18952)\r\n\r\nInitial goal is to enable HTTP access and geolocation in background.\r\nHere's a sample code:\r\n\r\n```javascript\r\nvar win = Ti.UI.createWindow({\r\n    backgroundColor: 'blue'\r\n});\r\nvar view = Ti.UI.createView({ layout: 'vertical' });\r\n\r\n//\r\n// Enable location tracking in background\r\n//\r\nTi.App.Windows.BackgroundService.locationServiceEnabled = true;\r\n\r\n//\r\n// Callback for background task\r\n// Note that you can't use setTimeout/setInterval in background, as well as any UI interaction (of course).\r\n// Also, note that Ti.Geolocation events does not work in background. Use locationServiceEnabled instead.\r\n//\r\nvar callback = function (e) {\r\n    //\r\n    // e.source ... task object that invokes this event\r\n    // e.cancel ... indicate that this task is canceled\r\n    // e.location ... Current location. Available only when locationServiceEnabled is true.\r\n    // \r\n    //\r\n    if (e.canceled) {\r\n        Ti.API.info(\"Task canceled\");\r\n    } else {\r\n        Ti.API.info(\"Do something useful\");\r\n        if (e.location) {\r\n            // location contains latitude, longitude etc.\r\n            // See LocationCoodinates for details (http://docs.appcelerator.com/platform/latest/#!/api/LocationCoordinates)\r\n            Ti.API.info('Current geolocation: ' + JSON.stringify(e.location));\r\n        }\r\n        Ti.API.info(\"Get something via HTTP then\");\r\n        var url = \"http://www.appcelerator.com\";\r\n        var client = Ti.Network.createHTTPClient({\r\n            onload: function (e) {\r\n                Ti.API.info(\"Received data size: \" + this.responseData.size);\r\n            },\r\n            onerror: function (e) {\r\n                Ti.API.info('Error: ' + e.error);\r\n            },\r\n            timeout: 5000\r\n        });\r\n        client.open(\"GET\", url);\r\n        client.send();\r\n    }\r\n};\r\n\r\nvar task;\r\nvar registerBtn = Ti.UI.createButton({\r\n    title: \"register timer task\"\r\n});\r\nvar unregisterBtn = Ti.UI.createButton({\r\n    title: \"unregister timer task\"\r\n});\r\nvar unregisterAllBtn = Ti.UI.createButton({\r\n    title: \"unregister ALL tasks\"\r\n});\r\n\r\nregisterBtn.addEventListener('click', function () {\r\n    //\r\n    // BackgroundService.registerTimerTask(callback, interval_minutes, is_one_shot);\r\n    //\r\n    // Create timer task, that repeats at 15-minute intervals.\r\n    // Note that Windows doesn't accept interval less than 15 minutes.\r\n    //\r\n    task = Ti.App.Windows.BackgroundService.registerTimerTask(callback, 15, false);\r\n});\r\n\r\nunregisterBtn.addEventListener('click', function () {\r\n    //\r\n    // When you unregister the task, task invokes callback with canceled = true.\r\n    //\r\n    task.unregister();\r\n});\r\n\r\nunregisterAllBtn.addEventListener('click', function () {\r\n    //\r\n    // Unregister all tasks that is associated with this application.\r\n    //\r\n    Ti.App.Windows.BackgroundService.unregisterAllTasks();\r\n});\r\n\r\nview.add(registerBtn);\r\nview.add(unregisterBtn);\r\nview.add(unregisterAllBtn);\r\n\r\nwin.add(view);\r\nwin.open();\r\n```\r\n\r\n\r\n##### IMPORTANT NOTE\r\n\r\nTo enable background service, both Windows Phone and Windows Store apps require an `timer` BackgroundTask extension as well as `internetClient` and `location` capabilities in the `tiapp.xml`. (See [Windows-specific configuration](https://wiki.appcelerator.org/display/guides2/tiapp.xml+and+timodule.xml+Reference#tiapp.xmlandtimodule.xmlReference-Windows-specificapplicationproperties) for details) Also, when you use geolocation, make sure your devices enables GPS.\r\n\r\n###### tiapp.xml Background  Task Extension\r\n\r\n```xml\r\n<windows>\r\n  <manifest>\r\n    <Extensions>\r\n        <Extension Category=\"windows.backgroundTasks\" EntryPoint=\"TitaniumWindows_Ti.BackgroundServiceTask\">\r\n          <BackgroundTasks>\r\n            <Task Type=\"timer\" />\r\n          </BackgroundTasks>\r\n        </Extension>\r\n    </Extensions>\r\n    <Capabilities>\r\n      <Capability Name=\"internetClient\" />\r\n      <DeviceCapability Name=\"location\" />\r\n    </Capabilities>\r\n  </manifest>\r\n</windows>\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/appcelerator/titanium_mobile_windows/issues/comments/196213141","html_url":"https://github.com/appcelerator/titanium_mobile_windows/pull/566#issuecomment-196213141","issue_url":"https://api.github.com/repos/appcelerator/titanium_mobile_windows/issues/566","id":196213141,"user":{"login":"Buder","id":599226,"avatar_url":"https://avatars.githubusercontent.com/u/599226?v=3","gravatar_id":"","url":"https://api.github.com/users/Buder","html_url":"https://github.com/Buder","followers_url":"https://api.github.com/users/Buder/followers","following_url":"https://api.github.com/users/Buder/following{/other_user}","gists_url":"https://api.github.com/users/Buder/gists{/gist_id}","starred_url":"https://api.github.com/users/Buder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Buder/subscriptions","organizations_url":"https://api.github.com/users/Buder/orgs","repos_url":"https://api.github.com/users/Buder/repos","events_url":"https://api.github.com/users/Buder/events{/privacy}","received_events_url":"https://api.github.com/users/Buder/received_events","type":"User","site_admin":false},"created_at":"2016-03-14T09:07:32Z","updated_at":"2016-03-14T09:07:32Z","body":"Reading though your comments, just FYI, testing now with Phone 8.1 will let you know. \r\n\r\nBut one thing I will be needing is a notification API, I looked at the parity its from master but I cannot find any way what so ever to create a notification, not local or from push.. (if push I need to register a callback that handles the push from servers), any idea or am I missing something?)\r\nI know that is not part of the ticket but for me to have a working background service I have to be able to notify the user that there is new information that arrived, this is done from the HTTP call where I report the location and get a reply if there is new news to be notified to user.\r\n\r\nthanks"}}