我正在構建一個應用程序,允許用戶每個工作日都有一個本地通知重複,因爲蘋果公司沒有重複的間隔時間,我需要爲每週的星期幾創建本地通知。我做了一個for循環循環5倍於工作週中的天數。但它似乎並沒有被解僱。但是,當我刪除通知時,我看到所有五個,只是不會開火。這是我的代碼看起來像。平日本地通知不成功
NSDate *today = [NSDate date];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = _alarmTime;
localNotification.alertBody = @"Wake up or pay-up!";
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.alertAction = @"Wake up or pay-up!";
localNotification.soundName = @"sound_ring.caf";
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:_alarmTime];
switch (_repeatInt) {
case 1:
localNotification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
_repeatString = @"Everyday";
break;
case 2:
//Attempting to create 5 local notifications with different days but same time.
for (int i = 2; i <= 6; i++){
[dateComponent setWeekday:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
UILocalNotification *localNotification2 = [localNotification copy];
localNotification2.fireDate = fireDate;
localNotification2.repeatInterval = NSWeekCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(@"%D",i);
}
如果有人有任何想法,請讓我知道!提前致謝。
更新:
NSDate *startDate = [gregCalendar dateFromComponents:dateComponent];
[dateComponent setMonth:0];
[dateComponent setDay:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
[dateComponent setYear:0];
NSDate *fireDate = [gregCalendar dateByAddingComponents:dateComponent toDate:startDate options:0];
UILocalNotification *localNotification2 = [localNotification copy];
localNotification2.fireDate = fireDate;
localNotification2.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(@"%D",i);
但是...你有在AppDelegate ...註冊發送通知的代碼? – TonyMkenu 2015-03-03 08:36:25
@TonyMkenu是的,我已經有了。 – Jack 2015-03-03 11:47:04
@jack代碼中聲明「_repeatInt」的地方。請讓我知道它的需要。 – annu 2015-03-12 12:04:37