Iphone “本地通知”;每天早上7:00”;不通知
我希望每天7:00发出通知,但它不会发出。我也希望它显示在锁屏。这是我到目前为止所有的代码Iphone “本地通知”;每天早上7:00”;不通知,iphone,ios,xcode,ipad,uilocalnotification,Iphone,Ios,Xcode,Ipad,Uilocalnotification,我希望每天7:00发出通知,但它不会发出。我也希望它显示在锁屏。这是我到目前为止所有的代码 -(void)EveryDayNotify { NSLog(@"Good Morning Working"); UILocalNotification *localNotification = [[UILocalNotification alloc] init]; [localNotification setAlertBody:@"Good Morning! Have a
-(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];
}
你需要打电话
[[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];
在发布localNotification
之前。您的代码完成了所有的设置,但实际上并没有安排它,这就是为什么您永远无法获得它。您需要调用
[[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];
在发布localNotification
之前。你的代码完成了所有的设置,但实际上并没有对其进行计划,这就是为什么你永远无法得到它。尝试使用以下方法:
- (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尝试使用以下方法:
- (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尽管这是一个非常古老的问题,但我刚从谷歌来到这里,是的,每个人的答案都是抄袭别人的答案,这不是很有趣吗 所以,对于所有像我这样来自谷歌的人来说,请笑一笑这个拼写错误,它让你花了一两分钟的时间去思考为什么它不起作用
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
有效,但是
[[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];
尽管这是一个非常古老的问题,我刚从谷歌来到这里,是的,每个人的答案都是抄袭别人的答案,这不是很有趣吗 所以,对于所有像我这样来自谷歌的人来说,请笑一笑这个拼写错误,它让你花了一两分钟的时间去思考为什么它不起作用
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
有效,但是
[[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];
通知不起作用。它起作用了
它起作用了
什么是Components ForReferenceDate?好吧,让我们假设你在11月份调用了firedate,其他人也会调用firedate,我已经为你设置了代码,做你想做的事情好吧,现在只需将整个代码复制并粘贴到你的应用程序代理中,所以我不必担心Components ForReferenceDate?只需设置NSDay压延机至NSMinute压延机,然后它会在你设置的每分每秒弹出什么是组件被引用?好吧,让我们假设你为setMonth设置tipe:11意味着在11月调用firedate,其他人也一样,我已经为你设置了代码,就是做你想要的事情好吧,现在只需将整个代码复制并粘贴到你的应用程序中,所以我不应该担心引用的组件吗?只需将NSDaycalenderunit设置为NSMinuteCalenderunit,它就会在您设置的每分每秒弹出一次