我的通知代码有什么问题? [英] What's Wrong with my notification code?

查看:94
本文介绍了我的通知代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:下面有一个更新。

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

代码在我看来是正确的,它应该有效,因为我被告知它应该发出通知在每个小时的开始。但是,它没有!代码是完整的,我已经设置了属性并合成它甚至NSCalendar就在那里。它构建和运行完美无缺,然而,它永远不会在我的模拟器上每小时发出一次通知!?我只是无法得到似乎错误的东西。如果您需要更多信息或复制代码的其他部分以帮助我找到遗漏的内容,请告诉我。谢谢。

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;


推荐答案

让我难过的东西:你不会看到应用程序位于前台时发出通知。您需要点击模拟器上的主页按钮将应用程序置于后台,否则,在应用程序处于活动状态时查看内容,实现应用程序委托协议方法: - (void)application:(UIApplication *)应用程序中的应用didReceiveLocalNotification :( UILocalNotification *)通知并使该方法显示一些内容。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆