Skip to content

Instantly share code, notes, and snippets.

@jungchris
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jungchris/e223275b02c38c7f423b to your computer and use it in GitHub Desktop.
Save jungchris/e223275b02c38c7f423b to your computer and use it in GitHub Desktop.
Set Repeating Notifications
+ (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