Created
December 20, 2011 12:00
-
-
Save bkenny/1501349 to your computer and use it in GitHub Desktop.
UILocalNotification
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
- (void)applicationDidEnterBackground:(UIApplication *)application | |
{ | |
// Date Formatter to dd/MM/yyyy | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setDateFormat:@"dd/MM/yy"]; | |
NSDateFormatter *dateFormatterWithTime = [[NSDateFormatter alloc] init]; | |
[dateFormatterWithTime setDateFormat:@"dd/MM/yy HH:mm"]; | |
// Instantiate some dates, fool. | |
NSDate *now = [NSDate date]; | |
NSDate *quitDate = [[NSDate alloc] init]; | |
NSDate *whenToAlert = [[NSDate alloc] init]; | |
quitDate = [dateFormatter dateFromString:userConfiguration.quitDate]; | |
// Notify one day after the quit date and 11 hours - otherwise it would go off at 00:00 | |
NSDateComponents *dc = [[NSDateComponents alloc] init]; | |
[dc setDay:1]; | |
[dc setHour:11]; | |
[dc setMinute:38]; | |
whenToAlert = [[NSCalendar currentCalendar] dateByAddingComponents:dc toDate:quitDate options:0]; | |
[dc release]; | |
// Testing goodies | |
//NSString *dateStr = [dateFormatterWithTime stringFromDate:whenToAlert]; | |
//NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:1]; | |
// The alert | |
UIApplication* app = [UIApplication sharedApplication]; | |
UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init]; | |
NSArray* oldNotifications = [app scheduledLocalNotifications]; | |
if ([oldNotifications count] > 0) [app cancelAllLocalNotifications]; | |
if (notifyAlarm) | |
{ | |
notifyAlarm.fireDate = whenToAlert; | |
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone]; | |
notifyAlarm.repeatInterval = 0; | |
notifyAlarm.soundName = @"Glass.aiff"; | |
notifyAlarm.alertBody = @"12 Hours: Your oxygen levels are back to normal. Sweet!"; | |
[app scheduleLocalNotification:notifyAlarm ]; | |
} | |
// Release me, release my boday.. | |
[dateFormatter release]; | |
[dateFormatterWithTime release]; | |
/* | |
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. | |
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment