2015-03-02 28 views
0

我正在構建一個應用程序,允許用戶每個工作日都有一個本地通知重複,因爲蘋果公司沒有重複的間隔時間,我需要爲每週的星期幾創建本地通知。我做了一個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); 
+0

但是...你有在AppDelegate ...註冊發送通知的代碼? – TonyMkenu 2015-03-03 08:36:25

+0

@TonyMkenu是的,我已經有了。 – Jack 2015-03-03 11:47:04

+0

@jack代碼中聲明「_repeatInt」的地方。請讓我知道它的需要。 – annu 2015-03-12 12:04:37

回答

2

好,我是finaly能得到本地的通知重複每個工作日,我加入[setHour][setMinute]以及改變int在for循環中添加到NSUInteger中。以下是我的代碼如下。

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: 

     for (NSUInteger i = 2; i <= 6; i++) { 

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

      [dateComponent setMinute:minute]; 
      [dateComponent setWeekday:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri 
      [dateComponent setHour:hour]; 

      NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent]; 

      UILocalNotification *localNotification2 = [[UILocalNotification alloc] init]; 
      localNotification2.fireDate = fireDate; 
      localNotification2.alertBody = @"Wake up or pay-up!"; 
      localNotification2.timeZone = [NSTimeZone systemTimeZone]; 
      localNotification2.alertAction = @"Wake up or pay-up!"; 
      localNotification2.soundName = @"sound_ring.caf"; 
      localNotification2.repeatInterval = NSCalendarUnitWeekOfYear; 
      [[UIApplication sharedApplication] scheduleLocalNotification:localNotification2]; 
      NSLog(@"%lu",(unsigned long)i); 

     } 
0

他們不火,因爲你給他們的日期是無效的。

下面是如何產生的在未來幾天的X號日期:

 NSCalendar *calendar = [NSCalendar currentCalendar]; 
     NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:[NSDate date]]; 
     NSDate *startDate = [calendar dateFromComponents:components]; 
     [components setMonth:0]; 
     [components setDay:X]; 
     [components setYear:0]; 
     NSDate *endDate = [calendar dateByAddingComponents:components toDate:startDate options:0];