Reputation: 43
I've set up a local Notification and my problem is that I've set it to saturday and with the repeatinterval of one week. However, I set the correct date up but I still get it every sunday but on the correctly set time. Does anybody see my mistake? Oh and dont to forget, if I set the correct day if its ...wednesday I get immediately a notifi after one minute. Don't know where's my fault.
- (void)applicationDidEnterBackground:(UIApplication *)applicatio
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *componentsForReferenceDate =
[calendar components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[NSDate date]];
//set day (saturday)
[componentsForReferenceDate setDay:1] ;
[componentsForReferenceDate setMonth:12] ;
[componentsForReferenceDate setYear:2012] ;
NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate] ;
// set components for time 18:30.
NSDateComponents *componentsForFireDate =
[calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit ) fromDate: referenceDate];
[componentsForFireDate setHour: 18] ;
[componentsForFireDate setMinute:38] ;
[componentsForFireDate setSecond:0] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
notification.alertAction = @"Back";
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"];
notification.repeatInterval= NSWeekCalendarUnit ;
notification.soundName = @"Appnotifisound.wav";
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
Thanks.
Upvotes: 2
Views: 375
Reputation: 5699
This is because you are creating two different NSDateComponents
. One to set the Date, Month and Year, and a different one to set the Hours, Mins and Secs.
This should work :
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *componentsForReferenceDate = [[NSDateComponents alloc] init];
//set day (saturday)
[componentsForReferenceDate setDay:7] ;
[componentsForReferenceDate setMonth:12] ;
[componentsForReferenceDate setYear:2012] ;
// set time (08:30 PM)
[componentsForReferenceDate setHour: 20] ;
[componentsForReferenceDate setMinute:30] ;
[componentsForReferenceDate setSecond:00] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForReferenceDate];
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
notification.alertAction = @"Back";
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"];
notification.repeatInterval= NSWeekCalendarUnit ;
notification.soundName = @"Appnotifisound.wav";
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
}
Upvotes: 0