自定义时间和工作日作为Objective-C中的本地通知

时间:2015-12-07 12:29:32

标签: ios objective-c notifications

我一直在努力处理本地通知以及自定义时间和工作日。我有一个工作日+日期选择器(只是UI时间),需要根据工作日和自定义时间创建重复的每周通知。我们假设用户希望每周一下午1:06提醒。所以我从日期选择器中获取数据并从中获取NSDateComponents,然后创建fireDate并传递方法,这是我的通知。我总是得到第一个通知但是当我在同一时间将时间改为下周时,没有任何反应。如何确保此通知重复?

self.notification.time = self.datePicker.date;

//format hours
NSDateFormatter *hourFormatter = [[NSDateFormatter alloc] init];
hourFormatter.dateFormat = @"HH";

//format minutes
NSDateFormatter *minuteFormatter = [[NSDateFormatter alloc] init];
minuteFormatter.dateFormat = @"mm";

NSString *hour = [hourFormatter stringFromDate:self.notification.time];
NSString *minute = [minuteFormatter stringFromDate:self.notification.time];    

unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitWeekOfYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday;

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponets = [calendar components: unitFlags fromDate:[NSDate date]];

dateComponets.weekday = 2; //Monday
dateComponets.hour = hour.intValue; //from date picker
dateComponets.minute = minute.intValue; //from date picker
dateComponets.second = 00;
dateComponets.timeZone = [NSTimeZone defaultTimeZone];


NSDate *fireDate = [calendar dateFromComponents:dateComponets];

//Create Notification
[self.notification scheduleNotification:@"HabitName" fireDate:fireDate alertBody:@"Alert Body"];

这是我的通知方法

-(void)scheduleNotification:(NSString *)habitName fireDate:(NSDate *)fireDate alertBody:(NSString *)alertBody
{

    self.note = [[UILocalNotification alloc] init];
    self.note.alertBody = alertBody;
    self.note.fireDate = fireDate;
    self.note.repeatInterval = NSCalendarUnitWeekOfYear;
    self.note.timeZone = [NSTimeZone defaultTimeZone];
    self.note.soundName = UILocalNotificationDefaultSoundName;
    self.userInfo = [NSDictionary dictionaryWithObject:@"test1" forKey:@"test"];
    self.note.userInfo = self.userInfo;
    [[UIApplication sharedApplication] scheduleLocalNotification:self.note];

    NSLog(@"Notification set up for: %@", self.note);

}

印刷通知输出:

Notification set up for: <UIConcreteLocalNotification: 0x788d71c0>{fire date = Monday, December 7, 2015 at 1:06:00 PM Central European Standard Time, time zone = Europe/Prague (GMT+1) offset 3600, repeat interval = NSCalendarUnitWeekOfYear, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Monday, December 7, 2015 at 1:06:00 PM Central European Standard Time, user info = {
    test = test1;
}}

0 个答案:

没有答案