本地通知“每天上午7:00”。没有通知

编程入门 行业动态 更新时间:2024-10-18 18:15:39
本文介绍了本地通知“每天上午7:00”。没有通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望每天早上7点发出通知,但不会发生。我也希望它在锁定屏幕中显示。这是我到目前为止的所有代码。

I want a notification to go off everyday at 7:00, but it will not go off. I also want it to show in lock screen. Here is all the code I have so far.

-(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 a.m. 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

Thumbs up if this helped! :D

更多推荐

本地通知“每天上午7:00”。没有通知

本文发布于:2023-11-27 04:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636528.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   上午

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!