ios - 如何将 UILocalNotification 设置为每月或每周按计划的同一时间运行?

标签 ios uilocalnotification

我的应用程序包含来自服务器的数据。我有 2 种类型的通知,比如每月。现在我将使用(日期时间格式)2-2:30,6-9:00,23-10:00,26-12:00 获取月份数据。其中 2 表示日期后跟冒号 (:) 格式的时间。

现在我想在每个月的相同日期和时间运行通知。我能够运行多个通知。但他们不会每个月都重复。怎么做?以下是我的代码。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
          [UIApplication sharedApplication].idleTimerDisabled = YES;
            UIUserNotificationSettings *notiSett = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:NULL];
            [[UIApplication sharedApplication]registerUserNotificationSettings:notiSett];
                    [self generateLocalNotificationDaily];
    }

   -(void)generateLocalNotificationDaily
{
//  [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [self compareDateDaily];
}

-(void)compareDateDaily
{
    NSString *strTime = [Helper getPREF:PREF_AlARM_DAILY_TIME];
    NSArray *arrtime = [strTime componentsSeparatedByString:@","];

    for(int i=0;i<[arrtime count];i++)
    {
        NSArray *arrfirstTime = [[NSString stringWithFormat:@"%@",[arrtime objectAtIndex:i]]componentsSeparatedByString:@":"];

        int hh = (int)[[arrfirstTime objectAtIndex:0]integerValue];
        int mm = (int)[[arrfirstTime objectAtIndex:1]integerValue];

        NSDate *now = [NSDate date];
        NSTimeInterval secondsPerDay = 1;//24 * 60 * 60;
        NSDate *date = [now dateByAddingTimeInterval:secondsPerDay];
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
        NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
        [components setHour:hh];
        [components setMinute:mm];
        NSDate *todaySpecificDate = [calendar dateFromComponents:components];

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        [formatter setTimeZone:[NSTimeZone systemTimeZone]];
        NSDate *currentDate = [NSDate date];
        NSString *strCurrentDate = [formatter stringFromDate:currentDate];
        NSString *strSystemDate = [formatter stringFromDate:todaySpecificDate];
        currentDate = [formatter dateFromString:strCurrentDate];
        todaySpecificDate = [formatter dateFromString:strSystemDate];

        NSComparisonResult result = [currentDate compare:todaySpecificDate];

        NSString *strAlertMessage = [NSString stringWithFormat:@"%@",[Helper getPREF:PREF_AlARM_MESSAGE]];
        NSString *strUserName = [Helper getPREF:PREF_NAME];
        NSString *strmsg = [strAlertMessage stringByReplacingOccurrencesOfString:@"user_name" withString:strUserName];
        NSString *alertMsg = [strmsg stringByReplacingOccurrencesOfString:@"notification_type" withString:@"daily"];

        //
        switch (result)
        {
            case NSOrderedAscending:
            {
                UILocalNotification* localNotificationDaily = [[UILocalNotification alloc] init];
                localNotificationDaily.fireDate = todaySpecificDate;
                localNotificationDaily.alertBody = alertMsg;
                localNotificationDaily.repeatInterval = NSCalendarUnitDay;

                localNotificationDaily.soundName = UILocalNotificationDefaultSoundName;
                [[UIApplication sharedApplication] scheduleLocalNotification:localNotificationDaily];
                NSLog(@"%@ is in future from %@", todaySpecificDate, currentDate);
            }
                break;
            case NSOrderedDescending:
                NSLog(@"%@ is in past from %@", todaySpecificDate, currentDate);
                break;
            case NSOrderedSame:
                NSLog(@"%@ is the same as %@", todaySpecificDate, currentDate);
                break;
            default:
                NSLog(@"erorr dates %@, %@", todaySpecificDate, currentDate);
                break;
        }
    }
}

最佳答案

uiLocalNotification 中有一个属性“repeatInterval”。您必须像这样将重复间隔设置为月份

localNotificationMonthly.repeatInterval = NSCalendarUnitMonth;

它会根据您为通知设置的日期和时间每月重复您的通知。更多咨询请引用

https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/#//apple_ref/occ/instp/UILocalNotification/repeatInterval

周重复间隔为 NSCalendarUnitWeekOfYear

关于ios - 如何将 UILocalNotification 设置为每月或每周按计划的同一时间运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38306169/

相关文章:

iphone - 如何为 UILocalnotification 增加/减少 application.badge 号码

iphone - 试图找到一种比 RegEX 更快的方法来查找 NSString 中的 URL...有什么想法吗?

ios - 本地通知每 30 秒重复一次

ios - UIButton 图片饱和度变化

ios - 无法快速从 NSURLConnection.sendSynchronousRequest 获取所有 HeaderFields

iOS UiLocalNotification 格式化

ios - switch 语句而不是大量的 if 语句

ios - 尝试针对游戏的特定部分拆分 iPhone 和 iPad 尺寸

iphone - 如何弹出一个 viewController,然后通过委托(delegate)推送一个 viewController