Note: there is an update below.

I've asked in the previous question about specifying a minute for an hourly repeat interval. However, I had two answers asking me to try the date component, and I did. Here it is:

[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *componentsForReferenceDate = [[NSDateComponents alloc] init];
[componentsForReferenceDate setHour:hour];
[componentsForReferenceDate setMinute:0];
[componentsForReferenceDate setSecond:0];

 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 = @"View";
 notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"];
 notification.repeatInterval= NSHourCalendarUnit ;
 notification.soundName = @"Appnotifisound.wav";
 notification.applicationIconBadgeNumber = 1;
 [[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

The code seems to me right and it should work as I've been told it should fire a notification at the beginning of every hour. However, it does not! The code is complete, I have set the properties and synthesize it right even the NSCalendar is there. It builds and runs flawlessly, However, it never fires a notification for every hour on my simulator!? I just couldn't get what seems to be wrong. Please let me know if you need more information or copy the other parts of the code to help me finding what's missing. Thank you.

Update: Here is how I coded the Hour if it might be the problem..

NSCalendar *calendar1 = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar1 components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];
hour = [components hour];
min =[components minute];
sec =[components second];
NSLog(@"hour is %i",hour);

NSLog(@"min is %i",min);

NSLog(@"sec is %i",sec);
 if (hour < 24) {
     hour=hour+1;
     } else {
         hour=0;
有帮助吗?

解决方案

Something that stumped me: you won't see the notification while the app is in the foreground. You need to hit the home button on the simulator to put the app into the background or else, to see something while the app is active, implement the app delegate protocol method: - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification in your app and make that method display something.

其他提示

It works for me when i copy this code, except i don't set your sound and you have not shown how you set 'hour' so i hard coded it. Here is the output.

2013-01-09 17:57:17.968 TestStackOverflow[3322:11303] fireDate = 0001-01-01 06:17:32 +0000 2013-01-09 17:57:17.993 TestStackOverflow[3322:11303] +++scheduledLocalNotifications ( "{fire date = Monday, January 1, 1, 1:00:00 AM GMT-05:17:32, time zone = America/Toronto (EST) offset -18000, repeat interval = NSHourCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Wednesday, January 9, 2013, 6:17:32 PM Eastern Standard Time, user info = {\n information = \"Some information\";\n}}"

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top