Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Scheduling Local Notifications

lukeirvinlukeirvin Posts: 388Registered Users @ @
I'm working on a feature that will allow users to schedule days and a time for receiving a notification.

So far, the time and message feature is working great. Where I am stuck at is the repeat interval.

Here's what I have tried:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];

        NSString *formatedDate = [dateFormatter stringFromDate:self.datePicker.date];
        self.currentTimeLabel.text = formatedDate;

        NSDate *date = [dateFormatter dateFromString:self.currentTimeLabel.text];

        NSCalendar *calendar = [NSCalendar currentCalendar];

        NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
        NSInteger hour = [components hour];
        NSInteger minute = [components minute];

        [components setHour:hour];
        [components setMinute:minute];

        if ([self.currentRepeatLabel.text containsString:@"Sun"]) {
            [components setWeekday:0];

            self.notification = [[UILocalNotification alloc] init];
            self.notification.fireDate = [calendar dateFromComponents:components];
            self.notification.repeatInterval = NSCalendarUnitDay;
            [self.notification setAlertBody:[NSString stringWithFormat:@"A friendly reminder: %@", self.titleStringToDisplay]];

            if (self.soundSwitch.isOn == YES) {
                self.notification.soundName = @"soundeffect.wav";
            }

            NSDictionary *infoDict = [NSDictionary dictionaryWithObject:self.titleStringToDisplay forKey:@"title"];
            self.notification.userInfo = infoDict;

            [[UIApplication sharedApplication] scheduleLocalNotification:self.notification];
        }
        if ([self.currentRepeatLabel.text containsString:@"Mon"]) {
            [components setWeekday:1];

            self.notification = [[UILocalNotification alloc] init];
            self.notification.fireDate = [calendar dateFromComponents:components];
            self.notification.repeatInterval = NSCalendarUnitDay;
            [self.notification setAlertBody:[NSString stringWithFormat:@"A friendly reminder: %@", self.titleStringToDisplay]];

            if (self.soundSwitch.isOn == YES) {
                self.notification.soundName = @"soundeffect.wav";
            }

            NSDictionary *infoDict = [NSDictionary dictionaryWithObject:self.titleStringToDisplay forKey:@"title"];
            self.notification.userInfo = infoDict;

            [[UIApplication sharedApplication] scheduleLocalNotification:self.notification];
        }
// I'm doing this for each day.

NSCalendarUnitDay is repeating my notification everyday, not matter what days I have selected. I have tried NSCalendarUnitWeekOfYear but my notifications never fire when using that.

The goal is for the user to set their title, time, and repeat days (much like the native alarm app).

How do I set the repeat interval for this?

Replies

  • RickSDKRickSDK Posts: 1,240Registered Users @ @ @ @
    I can't tell from your post what the problem is. You have set the repeat interval to NSCalendarUnitDay and then are surprised that it is repeating each day?

    you could also use code and a loop to set a certain number of notifications, I suppose as well.
  • lukeirvinlukeirvin Posts: 388Registered Users @ @
    I have changed from NSCalendarUnitDay to NSCalendarUnitWeekOfYear.

    Here's the issue though:

    Notifications are no longer firing when I set them and the repeatInterval is firing on every Monday instead of the day or days that I set it to.
Sign In or Register to comment.