2017-05-11 41 views
0

我有本地通知,會在特定日期觸發,例如下一個星期一。我想要的是,每個星期一重複一遍。如何實現這一目標?代碼我目前使用:爲特定日期重複本地通知iOS 10

if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")) { 

     /* Trigger date */ 


     NSDate *date = [[NSDate date] mt_dateSecondsAfter:15]; 
     NSDateComponents *triggerDate = [[NSCalendar currentCalendar] 
             components:NSCalendarUnitYear + 
             NSCalendarUnitMonth + NSCalendarUnitDay + 
             NSCalendarUnitHour + NSCalendarUnitMinute + 
             NSCalendarUnitSecond fromDate:date]; 

     UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate 
                              repeats:NO]; 

     /* Set notification */ 

     UNMutableNotificationContent *content = [UNMutableNotificationContent new]; 
     content.body = @"Время вставать с Ретро ФМ"; 
     content.categoryIdentifier = NotificationCategoryIdent; 
     content.sound = [UNNotificationSound defaultSound]; 
     NSString *identifier = @"LocalNotification"; 
     UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier 
                       content:content 
                       trigger:trigger]; 

     UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
     [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { 
      if (error != nil) { 
       NSLog(@"Something went wrong: %@",error); 
      } 
     }]; 
    } 
    else { 

     // Code for old versions 

     UILocalNotification *notification = [[UILocalNotification alloc] init]; 
     [notification setAlertBody:@"Время вставать с Ретро ФМ"]; 
     [notification setCategory:NotificationCategoryIdent]; 
     [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; 
    } 
+0

什麼不與此代碼的工作來代替呢? – Koen

+0

只是重複:否重複:是的 –

+0

@Koen它的工作,我希望它每個星期一/月/日重複 –

回答

1

你需要在你的代碼

UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate 
                              repeats:YES]; 
+0

重複的日期和火災日期到底是什麼?火災日期後的下一個日期? –

1

更改代碼:

if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")) { 

     /* Trigger date */ 


     NSDate *date = [[NSDate date] mt_dateSecondsAfter:15]; 
     NSDateComponents *triggerDate = [[NSCalendar currentCalendar] 
             components:NSCalendarUnitYear + 
             NSCalendarUnitMonth + NSCalendarUnitDay + 
             NSCalendarUnitHour + NSCalendarUnitMinute + 
             NSCalendarUnitSecond fromDate:date]; 

     UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate 
                              repeats:YES]; 

     /* Set notification */ 

     UNMutableNotificationContent *content = [UNMutableNotificationContent new]; 
     content.body = @"Время вставать с Ретро ФМ"; 
     content.categoryIdentifier = NotificationCategoryIdent; 
     content.sound = [UNNotificationSound defaultSound]; 
     NSString *identifier = @"LocalNotification"; 
     UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier 
                       content:content 
                       trigger:trigger]; 

     UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
     [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { 
      if (error != nil) { 
       NSLog(@"Something went wrong: %@",error); 
      } 
     }]; 
    } 
    else { 

     // Code for old versions 

     UILocalNotification *notification = [[UILocalNotification alloc] init]; 
     [notification setAlertBody:@"Время вставать с Ретро ФМ"]; 
     [notification setCategory:NotificationCategoryIdent]; 
     [notification setRepeatCalendar:NSCalendarUnitWeekday]; 
     [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; 
    } 
+1

有什麼區別? –

+0

UNCalendarNotificationTrigger * trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate repeats:YES]; –

+0

和 [notification setRepeatCalendar:NSCalendarUnitWeekday]; –