简体   繁体   中英

Scheduling Local Notifications

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?

Update and new issue:

I am using NSCalendarUnitWeekOfYear now.

Here's my issue ... the notification is no longer firing now and the repeatInterval is always set to Monday instead of the day of the week that it steps through in code.

There are several ways to do that . You can use NScalender to find the weekday and than calculate that date on which you want to be notifications fire.

-(void) calculateweeknumber
{

    NSCalendar *numberCalendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [numberCalendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
    [comps setSecond:0.0];
    weekday = [comps weekday];
    NSLog(@"number is %d",weekday);
}

Your code has many problems.

Your date comes from a UIDatePicker obviously. You should use this date. Converting it back and forth is nonsense.

Also, get rid of all that duplicated code. It makes understanding (and fixing) the code more difficult. It hides the logic.

The real problem though is that you just set date components as if it were a date. It is not. You are starting with some date thrown into components, and then setting a weekday. This will not give another valid date - why should it? How could it? What should it adjust? The day? The month? The year? Into the future or into the past? The weekday seems to be simply ignored when converting to a date.

You need to calculate a proper date ( this answer describes calculating the next monday).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM