每周一重复本地通知

时间:2015-11-14 08:04:44

标签: objective-c xcode notifications uilocalnotification

我尝试每周一重复本地通知。我发现localNotification.repeatInterval = kCFCalendarUnitWeekOfYear;但我不确定它是否有效。如何在将来显示所有本地通知时间?

[[UIApplication sharedApplication] cancelAllLocalNotifications];

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[gregorian setLocale:[NSLocale currentLocale]];

NSDateComponents *nowComponents = [gregorian components:NSCalendarUnitYear | NSCalendarUnitWeekOfYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:today];

[nowComponents setHour:18];
[nowComponents setMinute:05];
[nowComponents setSecond:00];
[nowComponents setWeekday:2];

NSDate * notificationDate = [gregorian dateFromComponents:nowComponents];

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
//localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
localNotification.fireDate = notificationDate;
localNotification.repeatInterval = kCFCalendarUnitWeekOfYear;
localNotification.soundName = @"localNotification_Sound.mp3";
localNotification.alertBody = @"Message?";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

1 个答案:

答案 0 :(得分:3)

我在自己的模拟器中试用了你的代码,它似乎工作正常。

我建议做的唯一改变是:

localNotification.repeatInterval = NSCalendarUnitWeekOfYear;

我找到了in this very related question

此外,如果您使用以下行,编译器会抱怨:

[nowComponents setMinute:08];

(因为它将其解释为八进制数而不是整数)。不要使用前导零。做类似的事情:

[nowComponents setMinute:8];

不要忘记ask your user to enable notifications,否则他们就不会出现。

相关问题