本地通知“每天早上7点”不通知

我想要通知每天7点起飞,但不会熄灭。 我也希望它在锁屏显示。 这是迄今为止所有的代码。

-(void)EveryDayNotify { NSLog(@"Good Morning Working"); UILocalNotification *localNotification = [[UILocalNotification alloc] init]; [localNotification setAlertBody:@"Good Morning! Have a great day!"]; localNotification.soundName = UILocalNotificationDefaultSoundName; localNotification.applicationIconBadgeNumber = 1; localNotification.repeatInterval = NSDayCalendarUnit; NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date [components setHour:07]; [components setMinute:0]; localNotification.fireDate = [calendar dateFromComponents:components]; [localNotification release]; } 

尝试使用这个:

 - (void)applicationDidEnterBackground:(UIApplication *)application { NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; NSDateComponents *componentsForReferenceDate = [calendar components:(NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth ) fromDate:[NSDate date]]; [componentsForReferenceDate setDay:9]; [componentsForReferenceDate setMonth:11]; [componentsForReferenceDate setYear:2012]; NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate]; // set components for time 7:00 am NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate: referenceDate]; [componentsForFireDate setHour:7]; [componentsForFireDate setMinute:0]; [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: @"Good Morning! Have a great day!"]; notification.alertAction = @"go back"; notification.userInfo= @{@"information": [NSString stringWithFormat:@"Some information"]}; notification.repeatInterval= NSCalendarUnitDay; notification.soundName = UILocalNotificationDefaultSoundName; notification.applicationIconBadgeNumber = 1; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; } 

竖起大拇指,如果这有帮助! :d

你需要打电话

 [[UIApplication sharedApplication] scheduleLocalNotiication:localNotification]; 

在你释放localNotification之前。 你的代码完成所有的设置,但实际上并没有安排它,这就是为什么你永远不会得到它。

即使这是一个非常古老的问题,我刚刚从谷歌来到这里,是不是很有趣,每个人都回答只是复制其他人的答案?

所以对于像我这样从谷歌来的所有人来说,嘲笑这个拼写错误,螨虫会花费你一两分钟的时间,想知道为什么它不起作用。

 [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

作品,但是

 [[UIApplication sharedApplication] scheduleLocalNotiication:localNotification]; 

Notiication不…

它正在工作

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; NSDateComponents *componentsForReferenceDate = [calendar components:(NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth ) fromDate:[NSDate date]]; [[UIApplication sharedApplication] cancelAllLocalNotifications]; [componentsForReferenceDate setDay:27]; [componentsForReferenceDate setMonth:10]; [componentsForReferenceDate setYear:2016]; NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate]; // set components for time 7:00 am NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate: referenceDate]; [componentsForFireDate setHour:8]; [componentsForFireDate setMinute:0]; [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: @"БЛАГОСЛОВИТЕ ПРОРОКА (с.а.в)"]; notification.repeatInterval= NSCalendarUnitDay; notification.soundName = UILocalNotificationDefaultSoundName; [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1]; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; return YES; }