2017-02-01 18 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() 
    }