2017-02-01 131 views
0

我想從現在開始在2分鐘內觸發通知,並且還想每分鐘重複一次。下面的代碼的問題是它會在每分鐘內重複,但它會在2分鐘內立即啓動。感謝任何幫助。iOS 10通知觸發並重復每分鐘

UNMutableNotificationContent *content = [UNMutableNotificationContent new]; 
content.title = @"Good morning"; 
content.body = @"Body"; 
content.sound = [UNNotificationSound defaultSound]; 

UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; 

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:120]; 
NSDateComponents *triggerDate = [[NSCalendar currentCalendar] 
           components:NSCalendarUnitSecond fromDate:date]; 

UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate repeats:YES]; 
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"notification.daily" content:content trigger:trigger]; 

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { 
    NSLog(@"Error:%@", error); 
}]; 

回答

0

你爲什麼不使用UNTimeIntervalNotificationTrigger代替UNCalendarNotificationTrigger

UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:120 repeats:YES]; 

希望此回答將有所幫助。

+1

這將在2分鐘內開始,但不會在每分鐘後重復。 – Viraj

0

當你接收到的遠程推送通知,你可以每2分鐘

使用下面的方法來做到這一點後重復。

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
     let action = response.actionIdentifier 
     let request = response.notification.request 
     let content = request.content 
     if action == "snooze.action"{ 
      let snoozeTrigger = UNTimeIntervalNotificationTrigger(
       timeInterval: 120.0, 
       repeats: true) 
      let snoozeRequest = UNNotificationRequest(
       identifier: "snooze", 
       content: content, 
       trigger: snoozeTrigger) 
      center.add(snoozeRequest){ 
       (error) in 
       if error != nil { 
        print("Snooze Request Error: \(String(describing: error?.localizedDescription))") 
       } 
      } 
     } 
     completionHandler() 
    }