![](/img/trans.png)
[英]How to set repeatInterval as Every two days for UILocalNotification?
[英]UILocalNotification repeatInterval on 20 days
我想创建具有自定义间隔重复的本地通知(例如,每20天)。 我知道我们有NSDayCalendarUnit,kCFCalendarUnitMonth ...,但我希望将重复间隔设置为20天。 我不想每天都创建一个通知。
我真正的需要是连续21天重复一次通知,然后在7天后不启动它,然后再进行21天有通知的工作,而在7天没有通知的情况下等。我应该安排所有这些天,即使申请是无效。
为此,我决定自发射日期起使用repeatInterval = 28days创建21条通知(这是问题所在)
尝试此操作,可以通过选择setDay
, setMonth
,....来更改时间间隔:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:3];
NSDate *date3Days = [calendar dateByAddingComponents:components
toDate:[NSDate date]
options:0];
UIApplication* app = [UIApplication sharedApplication];
NSArray* oldNotifications = [app scheduledLocalNotifications];
if ( oldNotifications ) {
[app cancelAllLocalNotifications];
app.applicationIconBadgeNumber = 0;
}
UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];
if (notifyAlarm) {
notifyAlarm.fireDate = date3Days;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.alertBody = NSLocalizedString( @"Push message", @"");
notifyAlarm.soundName = @"sound.wav";
[app scheduleLocalNotification:notifyAlarm];
}
如果要设置UILocalNotifications
出现的特定时间,则可以创建上述解决方案的方法,并遍历表示您希望显示通知的日期的数组:
NSArray *arrayNumbers = @[@5, @7, @14, @21, @30, @60, @90, @120];
NSDictionary *dictNotifications =
[[NSUserDefaults standardUserDefaults] objectForKey:kUserDefAppStarts];
for ( NSNumber *bla in arrayNumbers ){
NSString *strKey = [NSString stringWithFormat:@"%@%@", kUserDefAppStarts, bla];
NSDictionary *dict = dictNotifications[strKey];
NSString *strMessageQuestion = dict[kKeyMessage];
[self createNotificationWithNumberOfDays:[bla integerValue]
andMessage:strMessageQuestion
userInfo:dict];
}
这是您必须调用的方法
+ (void)createNotificationWithNumberOfDays:(int)days
andMessage:(NSString *)message
userInfo:(NSDictionary *)dict{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];
NSDate *dateAlert = [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notifyAlarm = [[UILocalNotification alloc] init];
if( notifyAlarm ){
[notifyAlarm setFireDate:dateAlert];
[notifyAlarm setTimeZone:[NSTimeZone defaultTimeZone]];
[notifyAlarm setSoundName:@"soundname"];
[notifyAlarm setAlertBody:message];
[notifyAlarm setUserInfo:dict];
[app scheduleLocalNotification:notifyAlarm];
}
}
如果您希望从当前时间开始将UILocalNotification安排在未来的20天,请执行以下操作:
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *dateComp = [[NSDateComponents alloc] init];
int desiredAmountOfMonths = 4;
for (int month = 0; month < desiredAmountOfMonths; month++)
{
dateComp.month = month;
dateComp.day = 20;
NSDate *fireDate = [currentCalendar dateByAddingComponents:dateComp toDate:[NSDate date] options:NSCalendarMatchNextTimePreservingSmallerUnits];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDate;
notification.timeZone = [NSTimeZone defaultTimeZone];
}
您将需要修改UILocalNotification的消息,声音等内容,然后在自定义完成后触发通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.