Created
February 24, 2014 09:03
-
-
Save keicoder/9184206 to your computer and use it in GitHub Desktop.
objective-c : basic Scheduling the local notifications
This file contains hidden or 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
//basic Scheduling the local notifications | |
//ChecklistItem.h | |
//principles of Object-Oriented Programming is that objects can do as much as possible themselves | |
//therefore, it makes sense that the ChecklistItem object can schedule its own notifications. | |
- (void)scheduleNotification; | |
//ChecklistItem.m | |
#pragma mark - schedule notifications | |
//ChecklistItem object can schedule its own notifications | |
- (void)scheduleNotification { | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
//if there is a existing notification | |
UILocalNotification *existingNotification = [self notificationForThisItem]; | |
if (existingNotification != nil) { | |
NSLog(@"Found an existing notification %@", existingNotification); | |
[[UIApplication sharedApplication] cancelLocalNotification:existingNotification]; | |
} | |
//compares due date with current date | |
//if due date is in the past, then the NSLog() will not be performed | |
//call this method when the user presses the Done button after adding or editing a to-do item | |
if (self.shouldRemind && [self.dueDate compare:[NSDate date]] != NSOrderedAscending) { | |
NSLog(@"We should schedule a notification"); | |
UILocalNotification *localNotification = [[UILocalNotification alloc] init]; | |
localNotification.fireDate = self.dueDate; | |
localNotification.timeZone = [NSTimeZone defaultTimeZone]; | |
localNotification.alertBody = self.text; | |
localNotification.soundName = UILocalNotificationDefaultSoundName; | |
localNotification.userInfo = @{ @"ItemID" : @(self.itemId) }; | |
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; | |
NSLog(@"Scheduled notification %@ for itemId %d", localNotification, self.itemId); | |
} | |
} | |
#pragma mark - check whether there is a existing notification | |
- (UILocalNotification *)notificationForThisItem { | |
NSArray *allNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; | |
for (UILocalNotification *notification in allNotifications) { | |
NSNumber *number = [notification.userInfo objectForKey:@"ItemID"]; | |
if (number != nil && [number integerValue] == self.itemId) { | |
return notification; | |
} | |
} | |
return nil; | |
} | |
//ItemDetailViewController.m | |
//call scheduleNotification method when the user presses the Done button after adding or editing a to-do item | |
- (IBAction)done | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
[item scheduleNotification]; //ChecklistItem object can schedule its own notifications | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment