Попробуйте это, вы можете изменить Intervall, выбрав 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];
}
}
Вы не можете использовать 86400 в течение одного дня. – Fogmeister
Разве это не количество секунд в солнечный день? Или вы могли бы объяснить, почему вы не можете использовать это? –
Дни не всегда 86 400 секунд. Вы должны использовать функции NSCalendar для вычисления временных периодов. – Fogmeister