通知不触发
Notifications does not fire
我正在尝试从 x a.m 每 5 分钟创建一次相同的通知。到 y p.m.
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)
}
})
在调试中打印有未决通知。
因此,通知会添加到中心并等待时间,但不会触发。
有什么想法吗?
调试显示的内容:
hour: 13 minute: 35 isLeapMonth: false
hour: 13 minute: 36 isLeapMonth: false
hour: 13 minute: 37 isLeapMonth: false
hour: 13 minute: 38 isLeapMonth: false
hour: 13 minute: 39 isLeapMonth: false
Optional(
Hour: 13
Minute: 35, repeats: YES>)
Optional(
Hour: 13
Minute: 36, repeats: YES>)
Optional(
Hour: 13
Minute: 37, repeats: YES>)
Optional(
Hour: 13
Minute: 38, repeats: YES>)
Optional(
Hour: 13
Minute: 39, repeats: YES>)
根据您的信息,应用 运行 在 background state
中,您的应用可能会在一段时间后从操作系统中 suspended
。
背景
The app is in the background and executing code. Most apps enter this
state briefly on their way to being suspended. However, an app that
requests extra execution time may remain in this state for a period of
time. In addition, an app being launched directly into the background
enters this state instead of the inactive state. For information about
how to execute code while in the background, see Background Execution.
已暂停
The app is in the background but is not executing code. The system
moves apps to this state automatically and does not notify them before
doing so. While suspended, an app remains in memory but does not
execute any code. When a low-memory condition occurs, the system may
purge suspended apps without notice to make more space for the
foreground app.
我还需要说,操作系统 suspends
你的应用程序也是如此,即使你 运行 一些代码。有一些例外情况,如果您使用 locations (geofencing)
或 voip
功能
我正在尝试从 x a.m 每 5 分钟创建一次相同的通知。到 y p.m.
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)
}
})
在调试中打印有未决通知。 因此,通知会添加到中心并等待时间,但不会触发。
有什么想法吗?
调试显示的内容:
hour: 13 minute: 35 isLeapMonth: false hour: 13 minute: 36 isLeapMonth: false hour: 13 minute: 37 isLeapMonth: false hour: 13 minute: 38 isLeapMonth: false hour: 13 minute: 39 isLeapMonth: false Optional( Hour: 13 Minute: 35, repeats: YES>) Optional( Hour: 13 Minute: 36, repeats: YES>) Optional( Hour: 13 Minute: 37, repeats: YES>) Optional( Hour: 13 Minute: 38, repeats: YES>) Optional( Hour: 13 Minute: 39, repeats: YES>)
根据您的信息,应用 运行 在 background state
中,您的应用可能会在一段时间后从操作系统中 suspended
。
背景
The app is in the background and executing code. Most apps enter this state briefly on their way to being suspended. However, an app that requests extra execution time may remain in this state for a period of time. In addition, an app being launched directly into the background enters this state instead of the inactive state. For information about how to execute code while in the background, see Background Execution.
已暂停
The app is in the background but is not executing code. The system moves apps to this state automatically and does not notify them before doing so. While suspended, an app remains in memory but does not execute any code. When a low-memory condition occurs, the system may purge suspended apps without notice to make more space for the foreground app.
我还需要说,操作系统 suspends
你的应用程序也是如此,即使你 运行 一些代码。有一些例外情况,如果您使用 locations (geofencing)
或 voip
功能