UILocalNotification应该在每个工作日重复,但周末也会发生火灾

时间:2012-03-24 20:08:19

标签: ios ios5 uilocalnotification

我有一个UILocalNotification,应该是每周一次,周一到周五,但不是周末。我认为将通知的repeatInterval属性设置为NSWeekdayCalendarUnit可以实现此目的。可悲的是,我的通知仍在周末开火。谁有人建议为什么?这是我的代码:

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

localNotification.alertAction = @"View";
localNotification.alertBody = NSLocalizedString(@"ALERT_MESSAGE", nil);
localNotification.soundName = UILocalNotificationDefaultSoundName;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM-dd-yyyy HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Toronto"]];

// Notification fire times are set by creating a notification whose fire date 
// is an arbitrary weekday at the correct time, and having it repeat every weekday
NSDate *fireDate = [dateFormatter dateFromString:@"01-04-2012 11:00"]; 

localNotification.fireDate = fireDate;
localNotification.repeatInterval = NSWeekdayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
            break;

[localNotification release];

4 个答案:

答案 0 :(得分:15)

iOS中的工作日只意味着一周内的一天。与周末相比,它没有“工作周”的含义。

通过建议您使用1-7作为单位,文档说得更清楚了一些:

  

NSWeekdayCalendarUnit

     

指定工作日单位。

     

对应的值是kCFCalendarUnitSecond。等于kCFCalendarUnitWeekday。工作日单位是数字1到N(公历中N = 7,1表示星期日)。

来源:http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html

要从周一到周五正确设置通知,这里有一些框架代码。你将不得不执行5次,所以将它封装在fireDate参数的方法中是很好的。我已经展示了如何在星期一这样做。

UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];

// Set this to an NSDate that is for the time you want, on Monday
notification.fireDate = fireDate;            

// Repeat every week
notification.repeatInterval = NSWeekCalendarUnit;

答案 1 :(得分:5)

您必须分别为本地通知设置日期,并将本地通知repeatInterval设置为NSWeekCalendar单位。 AS

            [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:2 :tempDict] andRepeatInterval:NSWeekCalendarUnit];
            [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:3 :tempDict]andRepeatInterval:NSWeekCalendarUnit];
            [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:4 :tempDict] andRepeatInterval:NSWeekCalendarUnit];
            [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:5 :tempDict] andRepeatInterval:NSWeekCalendarUnit];
            [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:6 :tempDict] andRepeatInterval:NSWeekCalendarUnit];

这里我创建了方法setDateForAlarmWithWeekday,它返回在本地Notification的fireDate属性和设置Local Notification的notificationWithItem中设置的日期。

-(void) notificationWithItem:(NSDictionary*)tmpdict Date:(NSDate *)date andRepeatInterval:(NSCalendarUnit)CalUnit
{
    UILocalNotification *localNotification =[[UILocalNotification alloc]init];

    if (localNotification==nil) {
        return;
    }

    localNotification.fireDate=date;
    localNotification.timeZone=[NSTimeZone defaultTimeZone];
    localNotification.repeatCalendar=[NSCalendar currentCalendar];
    localNotification.alertBody=[NSString stringWithFormat:@"%@",[tmpdict objectForKey:@"Reminder"]];

    NSDictionary *snoozeDic=[tmpdict objectForKey:@"Snooze"];
    if ([[snoozeDic valueForKey:@"Switch"]intValue]==1) {
        localNotification.alertAction=@"Snooze";
    }else
    {
        localNotification.hasAction=NO;
    }

    localNotification.repeatInterval=CalUnit;
    localNotification.soundName=[NSString stringWithFormat:@"%@.caf",[tmpdict objectForKey:@"Tone"]];
    localNotification.userInfo=[NSDictionary dictionaryWithObject:tmpdict forKey:@"AlarmInfo"];
    localNotification.applicationIconBadgeNumber=1;

    [[UIApplication sharedApplication]scheduleLocalNotification:localNotification];

}


-(NSDate*)SetDateForAlarmWithWeekday:(int)WeekDay:(NSDictionary*)dics
{
    NSLog(@"set date for alarm called");
    NSCalendar *calendar=[NSCalendar currentCalendar];
    [calendar setTimeZone:[NSTimeZone defaultTimeZone]];

    unsigned currentFlag=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSWeekdayCalendarUnit;

    NSDateComponents *comp=[calendar components:currentFlag fromDate:[NSDate date]];

    NSArray *array=[[dics objectForKey:@"Time"] componentsSeparatedByString:@" "];
    NSInteger hour=[[[[array objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:0] intValue];
    NSInteger min=[[[[array objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:1] intValue];

    if ([[array objectAtIndex:1] isEqualToString:@"PM"]) {
        hour=hour+12;
    }
    else
    {
        if (hour==12) {
            hour=0;
        }
    }

    comp.hour=hour;
    comp.minute=min;
    comp.second=0;

    NSLog(@"set date for alarm (%i:%i:%i)",comp.hour,comp.minute,comp.second);
    NSLog(@"weekday :%i ",WeekDay);
    NSLog(@"comp weekday %i",comp.weekday);
    int diff=(WeekDay-comp.weekday);
    NSLog(@"difference :%i",diff);

    int multiplier;
    if (WeekDay==0) {
     multiplier=0;
    }else
    {
     multiplier=diff>0?diff:(diff==0?diff:diff+7);
    }

    NSLog(@"multiplier :%i",multiplier);

    return [[calendar dateFromComponents:comp]dateByAddingTimeInterval:multiplier*secondsInOneDay];
}

我希望这会对你有所帮助。

答案 2 :(得分:1)

我也搜索过它。下面的代码对我有用。将星期日值1到7星期日到星期六,并通知您想要触发并指定日期然后通知的通知机构将在该特定日期到来。 对于周末,在星期六和星期日,使用值7和1调用此函数两次。

希望这对你有所帮助。

- (void) weekEndNotificationOnWeekday: (int)weekday :(UILocalNotification *)notification : (NSDate*) alramDate
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: alramDate];
    [componentsForFireDate setWeekday: weekday] ; //for fixing Sunday
    //    [componentsForFireDate setHour: 20] ; //for fixing 8PM hour
    //    [componentsForFireDate setMinute:0] ;
    //    [componentsForFireDate setSecond:0] ;
    notification.repeatInterval = NSWeekCalendarUnit;
    notification.fireDate=[calendar dateFromComponents:componentsForFireDate];
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

答案 3 :(得分:1)

类似但在Swift中。

请记住,WeekDay从1 =星期日开始,然后转到7.请参阅Apple文档:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/

func scheduleLocalNotification(date:NSDate, weekDay:Int){

        guard let calendar =  NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian),
    let groupName = dateFormatter?.stringFromDate(date) else { return }

        calendar.timeZone = NSTimeZone.defaultTimeZone()

        let dateComponents = NSDateComponents()
        dateComponents.calendar = calendar
        dateComponents.hour = calendar.component(.Hour, fromDate: date)
        dateComponents.minute = calendar.component(.Minute, fromDate: date)
        dateComponents.weekOfYear = calendar.component(.WeekOfYear, fromDate: date)
        dateComponents.weekday = weekDay

        let localNotificaion = UILocalNotification()
        localNotificaion.fireDate = calendar.dateFromComponents(dateComponents)
        localNotificaion.alertBody = "Hey!"
        localNotificaion.timeZone = NSTimeZone.defaultTimeZone()
        localNotificaion.userInfo = ["Time": groupName]
        localNotificaion.repeatInterval = .WeekOfYear
        UIApplication.sharedApplication().scheduleLocalNotification(localNotificaion)

}