Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift UserNotification从将来的日期开始每分钟重复一次_Swift_Calendar_Components_Usernotifications_Unnotificationrequest - Fatal编程技术网

Swift UserNotification从将来的日期开始每分钟重复一次

Swift UserNotification从将来的日期开始每分钟重复一次,swift,calendar,components,usernotifications,unnotificationrequest,Swift,Calendar,Components,Usernotifications,Unnotificationrequest,使用Swift5.2、iOS13.4、 我尝试设置一个本地通知,在特定日期触发,并且每分钟无休止地重复 我尝试了以下方式使用Calendar.components和UserNotification: // create alarmDate in the future let formatter = DateFormatter() formatter.dateFormat = "yyyy/MM/dd HH:mm" let alarmDate = formatter.date(from: "2020

使用Swift5.2、iOS13.4、

我尝试设置一个本地通知,在特定日期触发,并且每分钟无休止地重复

我尝试了以下方式使用Calendar.components和UserNotification:

// create alarmDate in the future
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd HH:mm"
let alarmDate = formatter.date(from: "2020/05/15 20:31") ?? Date()

let repeatesBool = true  // the alarm shall repeat every minute
let calendarComponentSet: Set<Calendar.Component> = [.second]  // mask to repeat every minute

let alarmIdentifier = "myID1234"
let triggerKind = Calendar.current.dateComponents(calendarComponentSet, from: alarmDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerKind, repeats: repeatesBool)                
let request = UNNotificationRequest(identifier: alarmIdentifier, content: content, trigger: trigger)

notificationCenter.add(request) {(error) in 
    if let error = error { print("error: \(error)") } 
}
//将来创建alarmDate
let formatter=DateFormatter()
formatter.dateFormat=“yyyy/MM/dd HH:MM”
让alarmDate=格式化程序日期(从:“2020/05/15 20:31”)??日期(
let RepeatedBool=true//警报应每分钟重复一次
让calendarComponentSet:Set=[.second]//掩码每分钟重复一次
让alarmIdentifier=“myID1234”
让triggerKind=Calendar.current.dateComponents(calendarComponentSet,from:alarmDate)
let trigger=UNCalendarNotificationTrigger(日期匹配:triggerKind,重复:RepeatedBool)
let request=UNNotificationRequest(标识符:alarmIdentifier,内容:content,触发器:trigger)
notificationCenter.add(请求){(错误)在
如果let error=error{print(“error:\(error)”)}
}

问题是:

一旦我在alarmDate的任何时间执行上述代码,警报立即开始触发(而不是在实际达到alarmDate时间,即小时和分钟时)。或者换句话说:当alarmDate应该在20:31时间开始,并且我在16:00执行上述代码时,它已经触发了警报(而不是预期的20:31)。为什么呢

在上面的代码中,如果我按如下方式设置calendarComponentSet,我确实会在正确的日期和时间触发警报,但它不会按预期每分钟重复一次:

let calendarComponentSet: Set<Calendar.Component> = [.day, .month, .year, .hour, .minute, .second]
let calendarComponentSet:Set=[.day、.month、.year、.hour、.minute、.second]
因此,问题是:

如何创建在特定日期和时间发出的本地通知,并在此后每分钟无休止地重复???

(目标是为此只花费一次本地通知)