0

I want to add UILocalNotification to my app. I want to set the fireDate to take the time and date and do the notification after a specific minutes. for example, if I give it the time as 13:00 and date is 10/03/2013 and I want it to notify the user after this time by 10 mins, 15 mins, 30 mins, 45 mins or 1 hour. how to code this?

Hope anyone got my mean. thanks in advance.

Emy Alsabbagh
  • 1,087
  • 3
  • 13
  • 22
  • You can set the initial fireDate, but there is **no way** of specifying a custom repeat interval, see for example http://stackoverflow.com/questions/6966365/uilocalnotification-repeat-interval-for-custom-alarm-sun-mon-tue-wed-thu-f. – Martin R Mar 09 '13 at 09:38

1 Answers1

0

This is the best answer I got

- (void)scheduleNotificationWithItem:(ToDoItem *)item interval:(int)minutesBefore {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:item.day];
[dateComps setMonth:item.month];
[dateComps setYear:item.year];
[dateComps setHour:item.hour];
[dateComps setMinute:item.minute];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
    return;
localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];
localNotif.timeZone = [NSTimeZone defaultTimeZone];

localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i minutes.", nil),
     item.eventName, minutesBefore];
localNotif.alertAction = NSLocalizedString(@"View Details", nil);

localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;

NSDictionary *infoDict = [NSDictionary dictionaryWithObject:item.eventName forKey:ToDoItemKey];
localNotif.userInfo = infoDict;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
Emy Alsabbagh
  • 1,087
  • 3
  • 13
  • 22