Skip to content

Instantly share code, notes, and snippets.

@jonalter
Created August 31, 2011 22:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonalter/1184855 to your computer and use it in GitHub Desktop.
Save jonalter/1184855 to your computer and use it in GitHub Desktop.
Ti.App.iOS.scheduleLocalNotification example
var win = Ti.UI.createWindow({
backgroundColor:'white'
});
win.open();
var label = Ti.UI.createLabel({
top: 20,
height: 200,
width: 200,
text: "Background the app"
});
win.add(label);
function isiOS4Plus()
{
// add iphone specific tests
if (Titanium.Platform.name == 'iPhone OS')
{
var version = Titanium.Platform.version.split(".");
var major = parseInt(version[0],10);
// can only test this support on a 3.2+ device
if (major >= 4)
{
return true;
}
}
return false;
}
if (isiOS4Plus())
{
// register a background service. this JS will run when the app is backgrounded but screen is OFF!!!
var service = Ti.App.iOS.registerBackgroundService({url:'bg.js'});
Ti.API.info("registered background service = "+service);
// listen for a local notification event
Ti.App.iOS.addEventListener('notification',function(e)
{
Ti.API.info("local notification received: "+JSON.stringify(e));
});
// fired when an app resumes for 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 event determines that the app it was just paused
Ti.App.addEventListener('pause',function(e){
Ti.API.info("app was paused from the foreground");
});
}
//FILE: bg.js
/**
* this is a background service and this code will run *every* time the
* application goes into the foreground
*/
var value = "Hello from Running BG service - bg.js";
Ti.API.info(value);
var notification = Ti.App.iOS.scheduleLocalNotification({
alertBody:"App was put in background",
alertAction:"Re-Launch!",
userInfo:{"hello":"world"},
date:new Date(new Date().getTime() + 3000) // 3 seconds after backgrounding
});
Ti.App.iOS.addEventListener('notification',function(){
Ti.API.info('background event received = '+notification);
//Ti.App.currentService.unregister();
});
// we need to explicitly stop the service or it will continue to run
// you should only stop it if you aren't listening for notifications in the background
// to conserve system resources. you can stop like this:
//Ti.App.currentService.stop();
// you can unregister the service by calling
// Ti.App.currentService.unregister()
// and this service will be unregistered and never invoked again
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment