gpt4 book ai didi

iphone - 本地通知“每天上午7:00”通知

转载 作者:行者123 更新时间:2023-12-01 17:23:35 25 4
gpt4 key购买 nike

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

-(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

关于iphone - 本地通知“每天上午7:00”通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13296190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com