-
-
Save kosso/867404 to your computer and use it in GitHub Desktop.
/* Kosso : March 12th 2011 | |
This the only way I managed to do this without the app crashing on resume. | |
Done slightly differently to the KS example, since they unregister the service and | |
do not use a setInterval timer. | |
*/ | |
//############ in app.js : | |
// test for iOS 4+ | |
function isiOS4Plus(){ | |
if (Titanium.Platform.name == 'iPhone OS'){ | |
var version = Titanium.Platform.version.split("."); | |
var major = parseInt(version[0]); | |
// can only test this support on a 3.2+ device | |
if (major >= 4){ | |
return true; | |
} | |
} | |
return false; | |
} | |
if (isiOS4Plus()){ | |
var service; | |
// Ti.App.iOS.addEventListener('notification',function(e){ | |
// You can use this event to pick up the info of the noticiation. | |
// Also to collect the 'userInfo' property data if any was set | |
// Ti.API.info("local notification received: "+JSON.stringify(e)); | |
// }); | |
// fired when an app resumes from suspension | |
Ti.App.addEventListener('resume',function(e){ | |
Ti.API.info("app is resuming from the background"); | |
}); | |
Ti.App.addEventListener('resumed',function(e){ | |
Ti.API.info("app has resumed from the background"); | |
// this will unregister the service if the user just opened the app | |
// is: not via the notification 'OK' button.. | |
if(service!=null){ | |
service.stop(); | |
service.unregister(); | |
} | |
Titanium.UI.iPhone.appBadge = null; | |
}); | |
Ti.App.addEventListener('pause',function(e){ | |
Ti.API.info("app was paused from the foreground"); | |
service = Ti.App.iOS.registerBackgroundService({url:'bg.js'}); | |
Ti.API.info("registered background service = "+service); | |
}); | |
} | |
//############## end app.js changes | |
//############# in bg.js : | |
Ti.API.info("hello from a background service!"); | |
var alertCount = 0; | |
var notification = null; | |
function notify(resp){ | |
// This creates the notification alert on a 'paused' app | |
notification = Ti.App.iOS.scheduleLocalNotification({ | |
alertBody:resp, | |
alertAction:"OK", | |
userInfo:{"hello":"world"}, | |
badge:alertCount, | |
date:new Date(new Date().getTime() + 10) | |
}); | |
} | |
function checkFeed(){ | |
// silently ignore this if there's no network connection | |
if (Titanium.Network.online == false) { | |
return; | |
} | |
var t = new Date().getTime(); | |
Ti.API.info('checking feed in bg.. '+t); | |
var xhr = Titanium.Network.createHTTPClient(); | |
xhr.timeout = 1000000; | |
xhr.onerror = function(e){ | |
Ti.API.info('IN ERROR ' + e.error); | |
}; | |
xhr.onload = function(){ | |
// demo to increase the badge number... | |
alertCount++; | |
var response = this.responseText; | |
Ti.API.info("the reply was: "+response); | |
// open the notification | |
notify(response); | |
}; | |
xhr.open('GET','http://YOUR_SERVER.COM/service_test.php'); | |
xhr.send(); | |
/* | |
my service_test.php is simply: | |
<?php | |
echo "hello from a server at: ".date("r"); | |
?> | |
*/ | |
} | |
Ti.App.iOS.addEventListener('notification',function(){ | |
Ti.API.info('background event received = '+notification); | |
Ti.App.currentService.stop(); | |
Ti.App.currentService.unregister(); | |
}); | |
// Kick off a timer to trigger a function called 'checkFeed' every 10 seconds (= 10000 ms) | |
var timer = setInterval(checkFeed, 10000); | |
//####### END bg.js |
Hello,
I used those code snippet and noticed that service stops to work in a 10 minutes.
Here is my original post: http://developer.appcelerator.com/question/125271/background-service-stops-after-five-minutes
Is that service long-running in your case? Have you ever make background service to sent http requests while hour or more?
SDK 1.7.0
Hi Kosso,
I'm able to run your code in iOS5, starts the app, make it background, it will start the background service
However, after resume to the app, when I press home key to pause the app, it didn't register the background service until I resume to the app again. do you know why?
e.g
1 goto app, bg serv start
2 resume app, bg serv not start
3 resume app, bg serv start
4 resume app, bg serv not start
...
Hi Kosso,
I use background service Code and it is working fine.
I want to refresh my data to every 10mins.
Is it possible.??
Because i know we have only maximum 10 mins for background service but i need to refresh my data to every 10 mins in titanium.
is it possible.??
Background service runs, when app goes to the background. In foreground, you could write the code in a function and call this function with setTimeout as in this link Check the comment by Mark.
I'm not planning on doing more than a simple check for new content once or twice a day... Anyhow, they're not very happy with the app I've been working on anyway (been kicked twiced already :/ )