我试图从上午到下午每隔5分钟创建相同的通知 .
UNUserNotificationCenter.current().requestAuthorization(options: [ .alert, .sound, .badge], completionHandler: {didAllow, error in})
let hours: [Int] = [13]
for hour in hours {
for minute in stride(from: 35, to: 40, by: 1){
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Notification body"
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
print(dateComponents)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: ("\(hour)"+"\(minute)"+"timerDone"), content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: nil)
center.getPendingNotificationRequests(completionHandler: { requests in
for request in requests {
print(request.trigger)
}
})
}
}
它不会在计划的时间显示通知,但是
center.getPendingNotificationRequests(completionHandler: { requests in
for request in requests {
print(request.trigger)
}
})
在调试中打印有待处理的通知 . 因此,通知被添加到中心并等待时间,但不要触发 .
有任何想法吗?
调试显示的内容:
小时:13分钟:35 isLeapMonth:false hour:13分钟:36 isLeapMonth:false hour:13分钟:37 isLeapMonth:false hour:13分钟:38 isLeapMonth:false hour:13分钟:39 isLeapMonth:false可选(小时: 13分钟:35,重复:是>)可选(小时:13分钟:36,重复:是>)可选(小时:13分钟:37,重复:是>)可选(小时:13分钟:38,重复:是>)可选(小时:13分钟:39,重复:是>)
1 回答
鉴于您的应用程序在
background state
中运行的信息,可能是您的应用程序将在一段时间后从操作系统运行suspended
.Background
Suspended
我还要说,操作系统也是你的应用程序,即使你运行了一些代码 . 如果您使用
locations (geofencing)
或voip
功能,则会有一些例外情况请查看以下文档:https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html