1

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.

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?

OshriALM
  • 215
  • 3
  • 12
  • 1
    What have you tried? WE aren't going to write it for you. Create a local notification for each day, use the NSDate/Calander to determine which day it is. If it's sunday then don't schedule that one. – CW0007007 Jul 25 '14 at 09:50
  • EDITED, this is what i dont so far. i dont know how to do that, otherwise i would's ask :| – OshriALM Jul 25 '14 at 10:00
  • Check: http://stackoverflow.com/questions/4269093/how-do-i-get-the-day-of-the-week-in-objective-c – CW0007007 Jul 25 '14 at 10:09

2 Answers2

1

LocalNotifications have only four 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

Geet
  • 2,427
  • 2
  • 21
  • 39
  • I have worked on LocalNotifications, and I have tried hard enough to know that their Intervals cannot be changed, if you need to have the notifications every day, then better schedule them for for every 7 date, and put an exception for the saturday, I had to use such a logic too for this – Geet Jul 25 '14 at 10:19
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.

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];
        }
    }
Elias Ma
  • 106
  • 4