重置 UILocationNotification 触发日期设置为每天

Reset UILocationNotification fire date set for everyday

我的应用程序中有一个功能允许最大使用。 3 times/day 给用户 – 一旦 he/she 使用了所有 3 次,他们就不能在第二天再次使用它。 我在我的应用程序中使用 UILocationNotification 来每天显示一条通知。如果用户打开我的应用程序,而他在那一天没有使用这些功能,我会将通知设置为在当天下午 4 点准时触发。但现在我想要的是,当用户正在使用该应用程序时——如果 he/she 使用该功能 3 次,则通知不应触发,并且应同时设置为第二天。

这就是我显示本地通知的方式。

- (void) fireNoteForEachDay {
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    UILocalNotification *notif = [[UILocalNotification alloc] init];
    NSCalendar *gregCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [gregCalendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]];
    [dateComponents setHour:16];
    [dateComponents setMinute:0];
    [dateComponents setSecond:0];
    [notif setFireDate:[gregCalendar dateFromComponents:dateComponents]];
    [notif setAlertBody:@"Come back to the app – to use '<function name>' !"];
    [notif setRepeatInterval:NSDayCalendarUnit];
    [notif setTimeZone:[NSTimeZone defaultTimeZone]];
    [[UIApplication sharedApplication] scheduleLocalNotification:notif];
}

如果我在今天下午 3 点使用所有三个功能 – 那么现在它应该重置为明天下午 4 点的日期。

我该怎么做?

明天取

NSDateComponents *tomorrowComponents = [NSDateComponents new];
tomorrowComponents.day = 1 ;
NSCalendar *gregCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *tomorrow = [gregCalendar dateByAddingComponents:tomorrowComponents toDate:[NSDate date] options:0];

明天加时间

NSDateComponents *tomorrowAtTimeComponent = [gregCalendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:tomorrow];
tomorrowAtTimeComponent.hour = 16;
tomorrowAtTimeComponent.minute = 0;
NSDate *tomorrowAt4pm = [gregCalendar dateFromComponents:tomorrowAtTimeComponent];

重置备注

[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notif = [[UILocalNotification alloc] init];
[notif setFireDate:tomorrowAt4pm];
[notif setAlertBody:@"Come back to the app – to use '<function name>' !"];
[notif setRepeatInterval:NSDayCalendarUnit];
[notif setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notif];