如何在每隔一天或每隔一天安排一次 UILocalNotification?
Posted
技术标签:
【中文标题】如何在每隔一天或每隔一天安排一次 UILocalNotification?【英文标题】:How to schedule UILocalNotification at Every Other Day or Every Third Day? 【发布时间】:2013-10-11 10:28:17 【问题描述】:我每天上午 10:00 在我的应用程序中安排 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 。 那么该怎么做呢?
【问题讨论】:
看看***.com/questions/18014438/… 【参考方案1】:你不能自定义 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,所以如果您有计划,要为自定义重复时间间隔创建多个通知,请小心。
【讨论】:
【参考方案2】:您不能简单地使用repeatInterval
做到这一点,因为只有the following 间隔可用。
我能想到的解决方案之一是为每天设置单独的通知(不带repeatInterval
)。但是通过这种方式,您可能会遇到 64 个通知的限制。
【讨论】:
以上是关于如何在每隔一天或每隔一天安排一次 UILocalNotification?的主要内容,如果未能解决你的问题,请参考以下文章
在 UILocalNotification 中创建周间隔以及周期