Last active
August 29, 2015 14:27
-
-
Save jungchris/e223275b02c38c7f423b to your computer and use it in GitHub Desktop.
Set Repeating Notifications
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)addLocalNotificationWithRepeatCount:(NSUInteger)repeatCountDesired startOffset:(NSUInteger)daysOffset { | |
// ask for permission if necessary | |
[CCJNotificationEngine requestPermissionToSetNotifications]; | |
// NSLog(@"notifications %@", [UIApplication sharedApplication].scheduledLocalNotifications ); | |
// let's set the time to 9:00 am | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setDateFormat:@"yyyy"]; | |
int year = [[dateFormatter stringFromDate:[NSDate date]] intValue]; | |
[dateFormatter setDateFormat:@"MM"]; | |
int month = [[dateFormatter stringFromDate:[NSDate date]] intValue]; | |
[dateFormatter setDateFormat:@"dd"]; | |
int day = [[dateFormatter stringFromDate:[NSDate date]] intValue]; | |
[dateFormatter setDateFormat:@"HH"]; | |
int hour = [[dateFormatter stringFromDate:[NSDate date]] intValue]; | |
[dateFormatter setDateFormat:@"mm"]; | |
int minute = [[dateFormatter stringFromDate:[NSDate date]] intValue]; | |
// use NSDateComponenets to properly add a day | |
int daysToAdd = 0; | |
// logic to check wether we're adding this same day, or next day | |
if ((hour > 9) || (hour == 8 && minute > 59)) { | |
// set for tomorrow | |
daysToAdd = 1; | |
} | |
// set up date components | |
NSDateComponents *componentsTwo = [[NSDateComponents alloc] init]; | |
[componentsTwo setYear:year]; | |
[componentsTwo setMonth:month]; | |
[componentsTwo setHour:9]; | |
[componentsTwo setMinute:0]; | |
[componentsTwo setSecond:0]; | |
// create a calendar | |
NSCalendar *gregorianFuture = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; | |
NSDate *futureTime = [gregorianFuture dateFromComponents:componentsTwo]; | |
// set local notification | |
UILocalNotification *notify = [[UILocalNotification alloc] init]; | |
notify.fireDate = futureTime; | |
notify.soundName = UILocalNotificationDefaultSoundName; | |
// | |
NSString *buildMessage = @"Check Your Stuff"; | |
// loop using repeatCountDesired to add for each day | |
for (NSUInteger iCount = 0; iCount < repeatCountDesired; ++iCount) { | |
// create a random notification message | |
notify.alertBody = [NSString stringWithString:buildMessage]; | |
NSDictionary *userInfoDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"colors", @"ID", | |
buildMessage, messageKey, nil]; | |
notify.userInfo = userInfoDict; | |
// set day of NSDate component | |
[componentsTwo setDay:day + daysToAdd + daysOffset + iCount]; | |
// create a calendar | |
NSCalendar *gregorianFuture = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; | |
NSDate *futureTime = [gregorianFuture dateFromComponents:componentsTwo]; | |
notify.fireDate = futureTime; | |
[[UIApplication sharedApplication] scheduleLocalNotification:notify]; | |
} | |
} | |
+ (void)requestPermissionToSetNotifications { | |
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { | |
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]]; | |
} else { | |
NSLog(@"requestPermissionToSetNotifications: does not respond to 'registerUserNotificationSettings'"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment