UILocalNofication在一周的特定时间开除,除了1天

时间:2021-01-30 20:11:40

in my app i have a LocalNotification. im getting the firing time from DatePicker and i want to LocalNotification to fire all the week at specific time (which i take from the UIPickerView) and in all weekday except Saturday.

在我的应用程序中,我有一个LocalNotification。我从DatePicker获得了开火时间,我希望LocalNotification在特定时间(我从UIPickerView获取)和周六以外的所有工作日开始。

EDIT

编辑

    UIDatePicker *picker = [[UIDatePicker alloc]init];
    [picker setTag:kDatePickerTag];


    for (int i = 0; i < 7; i++) {
        NSDate *today = [NSDate date];

         NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *scheduleDate = [**picker.date** dateByAddingTimeInterval:(i * 24.0f * 3600.0f)];
        NSDateComponents *componentsForEachDay = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:scheduleDate];
        if (componentsForEachDay.weekday != 7) { // To skip Saturday

            UILocalNotification *localNotification = [[UILocalNotification alloc] init];
            localNotification.repeatInterval = NSWeekCalendarUnit;
            localNotification.fireDate = picker.date;
            localNotification.timeZone = [NSTimeZone defaultTimeZone];
            localNotification.alertBody = @"test message";
            localNotification.soundName = UILocalNotificationDefaultSoundName;
            localNotification.applicationIconBadgeNumber = 1;

            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

i have this code so far but now i have to set the firing time and i dont know how to merge between my picker.date which give me the TIME and weedDay which give me the DAY. i need then both is the same NSDate object. any ideas?

我有这个代码到目前为止,但现在我必须设置开火时间,我不知道如何合并我的picker.date给我我的时间和weedDay给我一天。我需要两个是相同的NSDate对象。有任何想法吗?

2 个解决方案

#1


1  

If you want to skip Saturday, you will have to set a notification for each of the other 6 days, and let it repeat by week.

如果您想跳过星期六,则必须为其他6天中的每一天设置通知,并让它按周重复。

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    for (int i = 0; i < 7; i++) {
        NSDate *scheduleDate = [firstScheduledDate dateByAddingTimeInterval:(i * 24.0f * 3600.0f)];
        NSDateComponents *componentsForEachDay = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:scheduleDate];
        if (componentsForEachDay.weekday != 7) { // To skip Saturday

            UILocalNotification *localNotification = [[UILocalNotification alloc] init];
            localNotification.repeatInterval = NSWeekCalendarUnit;
            localNotification.fireDate = FIRE_DATE;
            localNotification.timeZone = [NSTimeZone defaultTimeZone];
            localNotification.alertBody = YOUR_NOTIFICATION_MESSAGE;
            localNotification.soundName = UILocalNotificationDefaultSoundName;

            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        }
    }

#2


1  

LocalNotifications have only four InBuilt Intervals,

LocalNotifications只有四个InBuilt Intervals,

they are for either

他们是为了

case 1:
    notif.repeatInterval = NSMonthCalendarUnit;// monthly
    break;
  case 2:
    notif.repeatInterval = NSYearCalendarUnit;// yearly
    break;
  case 3:
    notif.repeatInterval = NSDayCalendarUnit;//Daily
    break;
  case 4:
    notif.repeatInterval = NSWeekCalendarUnit;// weekly

You can't define Custom Intervals with LocalNotifications, You may have to create your own logic to do that

您无法使用LocalNotifications定义自定义间隔,您可能必须创建自己的逻辑来执行此操作

#1


1  

If you want to skip Saturday, you will have to set a notification for each of the other 6 days, and let it repeat by week.

如果您想跳过星期六,则必须为其他6天中的每一天设置通知,并让它按周重复。

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    for (int i = 0; i < 7; i++) {
        NSDate *scheduleDate = [firstScheduledDate dateByAddingTimeInterval:(i * 24.0f * 3600.0f)];
        NSDateComponents *componentsForEachDay = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:scheduleDate];
        if (componentsForEachDay.weekday != 7) { // To skip Saturday

            UILocalNotification *localNotification = [[UILocalNotification alloc] init];
            localNotification.repeatInterval = NSWeekCalendarUnit;
            localNotification.fireDate = FIRE_DATE;
            localNotification.timeZone = [NSTimeZone defaultTimeZone];
            localNotification.alertBody = YOUR_NOTIFICATION_MESSAGE;
            localNotification.soundName = UILocalNotificationDefaultSoundName;

            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        }
    }

#2


1  

LocalNotifications have only four InBuilt Intervals,

LocalNotifications只有四个InBuilt Intervals,

they are for either

他们是为了

case 1:
    notif.repeatInterval = NSMonthCalendarUnit;// monthly
    break;
  case 2:
    notif.repeatInterval = NSYearCalendarUnit;// yearly
    break;
  case 3:
    notif.repeatInterval = NSDayCalendarUnit;//Daily
    break;
  case 4:
    notif.repeatInterval = NSWeekCalendarUnit;// weekly

You can't define Custom Intervals with LocalNotifications, You may have to create your own logic to do that

您无法使用LocalNotifications定义自定义间隔,您可能必须创建自己的逻辑来执行此操作