我的通知代码有什么错误?

注意:下面有一个更新。

在上一个问题中,我已经提出了每小时重复间隔指定一分钟的问题。 不过,我有两个答案要求我尝试date组件,而我做到了。 这里是:

[[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] ; 

代码在我看来是正确的,它应该工作,因为我已经被告知它应该在每个小时的开始发出通知。 但是,它不! 代码是完整的,我已经设置了属性,并合成它,即使NSCalendar在那里。 它build立和运行完美,但是,它不会在我的模拟器每小时发出通知!? 我只是无法得到什么似乎是错的。 请让我知道,如果您需要更多的信息或复制代码的其他部分,以帮助我find缺less的东西。 谢谢。

更新:这是我如何编码的小时,如果这可能是问题..

 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; 

有些东西难倒我:当应用程序在前台时,你不会看到通知。 你需要点击模拟器上的主页button来将应用程序放到后台,否则,在应用程序处于活动状态时看到一些东西,执行应用程序委托协议方法: - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification在您的应用程序,并使该方法显示的东西。

它适用于我复制此代码时,除了我不设置你的声音,你没有显示如何设置'小时',所以我硬编码。 这是输出。

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:00:00 AM GMT-05:17:32,time zone = America / Toronto(EST)offset -18000,repeat interval = NSHourCalendarUnit,repeat count = UILocalNotificationInfiniteRepeatCount,下一个火灾date= 2013年1月9日星期三下午6:17:32东部标准时间,用户信息= {\ n信息= \“某些信息\”; \ n}}“