Created
August 31, 2011 22:03
Ti.App.iOS.scheduleLocalNotification example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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