【问题标题】:UILocalNotification repeatInterval on 20 daysUILocalNotification repeatInterval on 20 days
【发布时间】:2014-12-11 13:28:05
【问题描述】:

我想使用自定义间隔重复(例如每 20 天)创建本地通知。 我知道我们有 NSDayCalendarUnit、kCFCalendarUnitMonth ...但我希望将重复间隔设置为 20 天。 我不想每天都创建通知。

我真正需要的是连续 21 天重复一个通知,然后在 7 天后不启动它,然后是一个新的 21 天有通知和 7 天没有......等等。 即使应用程序处于非活动状态,我也应该安排所有这些天。

为此,我决定创建一个自触发日期以来的 21 条通知,其中 repeatInterval = 28 天(这是问题所在)

【问题讨论】:

    标签: ios uilocalnotification


    【解决方案1】:

    试试这个,你可以通过选择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];
    }
    

    }

    【讨论】:

    • 是的,正要说这个。
    • 这是您问题的答案吗?不要忘记将其标记为正确,以便人们知道哪个是正确答案。
    • 这样做很简单。但我只想创建一个每 20 天启动一次通知的通知。
    • 我已经测试过这样的解决方案,但是当应用程序不活动时它不起作用!
    • 应用必须打开一次才能设置UILocalNotification。没有其他方法可以创建通知。即使对于远程通知,用户也必须打开应用程序一次并同意弹出窗口。你到底有什么问题?
    【解决方案2】:

    如果您想从当前时间开始在未来 20 天内安排 UILocalNotification,您可以这样做:

    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 的消息、声音等,然后在完成自定义后触发通知。

    【讨论】:

    • 一天用不了86400。
    • 这不是一个太阳日的秒数吗?或者您能解释一下为什么不能使用它吗?
    • 天并不总是 86,400 秒长。您应该使用 NSCalendar 函数来计算时间段。
    • 我只想创建一个每 20 天启动一次的通知(例如 20/12、10/01、30/01、20/02 ... 的通知)
    • 更新了我的代码。您需要创建一个 for 循环,然后将月份增加一。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多