ios - 我的通知代码有什么问题?

标签 ios objective-c uilocalnotification repeatingalarm

注意:下面有更新。

我在上一个问题中询问过如何为每小时重复间隔指定一分钟。
但是,我有两个答案要求我尝试使用日期组件,我做到了。这里是:

[[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 在那里。它可以完美地构建和运行,但是,它永远不会在我的模拟器上每小时触发一次通知!?我只是无法理解似乎有什么问题。如果您需要更多信息或复制代码的其他部分以帮助我查找缺少的内容,请告诉我。谢谢你。

更新:如果可能是问题,这是我对小时进行编码的方式..
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;

最佳答案

让我难过的事情:当应用程序在前台时,您不会看到通知。您需要点击模拟器上的主页按钮才能将应用程序置于后台,否则,要在应用程序处于事件状态时查看某些内容,请实现应用程序委托(delegate)协议(protocol)方法:- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification在您的应用程序中并使该方法显示一些内容。

关于ios - 我的通知代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14247784/

相关文章:

ios - 如何使用适用于 iOS 的 Gmail 查询在 Gmail 中创建新标签

ios - 调用 viewDidLoad 后,viewcontroller 的属性消失

ios - 如何使用 UIGestureRecognizer 识别字母

ios - 实时重新加载表格 View

iOS 应用程序在我的 iPhone 5s 上以 iPhone 4 分辨率运行?

iOS 为什么将 ToolBar 间隙移至键盘

ios - 如何快速取消特定的 UILocalNotification?

ios - 确定用户是否允许 iOS 7 中的本地通知

ios - 呈现一个导航栏和一个非 Root View 选项卡 Controller

ios - 自引用快速关闭