我每天上午10点在我的应用程序中安排UILocalNotification。
我使用了以下代码
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *componentsForReferenceDate =
[calendar components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[NSDate date]];
[componentsForReferenceDate setDay:10] ;
[componentsForReferenceDate setMonth:10] ;
[componentsForReferenceDate setYear:2013] ;
NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate] ;
// set components for time 10:00 a.m.
NSDateComponents *componentsForFireDate =
[calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit ) fromDate: referenceDate];
[componentsForFireDate setHour:10] ;
[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= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"];
notification.repeatInterval = NSDayCalendarUnit ;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
它的工作非常好。 但现在我想每隔一天和每隔三天安排一次UILocalNotification。 那怎么办?
答案 0 :(得分:2)
你不能自定义UILocalNotification的repeatTimeInterval ..有很多问题像你一样,而且前几天我有同样的问题,但我找不到任何解决方案所以请更好的是停止与它斗争。
您必须使用
中的重复时间间隔日历单位 指定日历和月份等日历单位。
enum {
NSEraCalendarUnit = kCFCalendarUnitEra,
NSYearCalendarUnit = kCFCalendarUnitYear,
NSMonthCalendarUnit = kCFCalendarUnitMonth,
NSDayCalendarUnit = kCFCalendarUnitDay,
NSHourCalendarUnit = kCFCalendarUnitHour,
NSMinuteCalendarUnit = kCFCalendarUnitMinute,
NSSecondCalendarUnit = kCFCalendarUnitSecond,
NSWeekCalendarUnit = kCFCalendarUnitWeek,
NSWeekdayCalendarUnit = kCFCalendarUnitWeekday,
NSWeekdayOrdinalCalendarUnit = kCFCalendarUnitWeekdayOrdinal,
NSQuarterCalendarUnit = kCFCalendarUnitQuarter,
NSWeekOfMonthCalendarUnit = kCFCalendarUnitWeekOfMonth,
NSWeekOfYearCalendarUnit = kCFCalendarUnitWeekOfYear,
NSYearForWeekOfYearCalendarUnit = kCFCalendarUnitYearForWeekOfYear
NSCalendarCalendarUnit = (1 << 20),
NSTimeZoneCalendarUnit = (1 << 21),
};
typedef NSUInteger NSCalendarUnit;
您可以从Apple的文档中找到。还读了这个问题......
How to set Local Notification repeat interval to custom time interval?
有关详细信息,您只安排了最大 64 的本地通知,因此请注意,如果您想要计划,请为自定义重复时间间隔创建多个通知。
答案 1 :(得分:0)
您只能使用repeatInterval
执行此操作,因为只有the following个时间间隔可用。
我能想到的解决方案之一是为每天设置单独的通知(没有repeatInterval
)。但通过这种方式,你可以偶然发现64个通知限制。