iOS 本地通知的日期、时间范围和次数?
iOS local notification base on day, time frame and number of times?
在 Whosebug 和其他网站上进行了大量研究之后,我能找到的唯一选项是在一周中的特定日期和特定时间重复通知,使用:
dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.weekday = 1 // Sunday
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// 以上会在我设置的每个星期天的任何时间触发。
发送通知的另一个选项是设置时间间隔但选项不多。
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 30, repeats: true)
我找不到任何触发通知的建议或解决方案(特定日期特定时间开始的次数)。例如:我想在每周日早上 8 点到下午 5 点之间触发 5 个本地通知。
有什么建议吗?提前谢谢你。
您需要创建自己的时间或日期;编程并不聪明,只是随心所欲。编码是一门精确的科学,需要这样对待。
var days = [2, 3, 4, 5, 6]
var hours = [10, 11, 12, 13, 14]
var minute = 0
for day in days {
for hour in hours {
dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.weekday = 2
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
}
}
这应该创建一系列触发器,这些触发器将在 10
11
12
13
14
小时的整点交付2
3
4
5
6
的日子。用人类的话说,假设我正确关联了这些数字,它应该在 M-F 的整点从 10-2 开始每小时触发一次你的用户。
Tbh - 从未使用过 UNCalenderNotificationTrigger
,但假设它按照您所说的那样工作,这将为每个时间创建触发器。
如果你想安排多个通知,你需要创建多个通知触发器并注册它们。
类似于:
// Simple extension if you have to create multiple of the same object
extension DateComponents {
static func triggerFor(hour: Int, minute: Int) -> DateComponents {
var component = DateComponents()
component.calendar = Calendar.current
component.hour = hour
component.minute = minute
component.weekday = 1
return component
}
}
// Add each trigger into an array with the correct date component
let triggers = [
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 1, minute: 0), repeats: true),
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 2, minute: 0), repeats: true),
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 3, minute: 0), repeats: true),
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 4, minute: 0), repeats: true),
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 5, minute: 0), repeats: true)
]
// Iterate through the array and register the notification with the system
for trigger in triggers {
// Create the content of the notification
let content = UNMutableNotificationContent()
content.title = "My Notification Title"
content.body = "Body of Notification"
// Create the request
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString,
content: content, trigger: trigger)
// Schedule the request with the system.
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.add(request) { (error) in
if error != nil {
// Handle any errors.
}
}
}
在 Whosebug 和其他网站上进行了大量研究之后,我能找到的唯一选项是在一周中的特定日期和特定时间重复通知,使用:
dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.weekday = 1 // Sunday
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// 以上会在我设置的每个星期天的任何时间触发。
发送通知的另一个选项是设置时间间隔但选项不多。
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 30, repeats: true)
我找不到任何触发通知的建议或解决方案(特定日期特定时间开始的次数)。例如:我想在每周日早上 8 点到下午 5 点之间触发 5 个本地通知。
有什么建议吗?提前谢谢你。
您需要创建自己的时间或日期;编程并不聪明,只是随心所欲。编码是一门精确的科学,需要这样对待。
var days = [2, 3, 4, 5, 6]
var hours = [10, 11, 12, 13, 14]
var minute = 0
for day in days {
for hour in hours {
dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.weekday = 2
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
}
}
这应该创建一系列触发器,这些触发器将在 10
11
12
13
14
小时的整点交付2
3
4
5
6
的日子。用人类的话说,假设我正确关联了这些数字,它应该在 M-F 的整点从 10-2 开始每小时触发一次你的用户。
Tbh - 从未使用过 UNCalenderNotificationTrigger
,但假设它按照您所说的那样工作,这将为每个时间创建触发器。
如果你想安排多个通知,你需要创建多个通知触发器并注册它们。
类似于:
// Simple extension if you have to create multiple of the same object
extension DateComponents {
static func triggerFor(hour: Int, minute: Int) -> DateComponents {
var component = DateComponents()
component.calendar = Calendar.current
component.hour = hour
component.minute = minute
component.weekday = 1
return component
}
}
// Add each trigger into an array with the correct date component
let triggers = [
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 1, minute: 0), repeats: true),
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 2, minute: 0), repeats: true),
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 3, minute: 0), repeats: true),
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 4, minute: 0), repeats: true),
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 5, minute: 0), repeats: true)
]
// Iterate through the array and register the notification with the system
for trigger in triggers {
// Create the content of the notification
let content = UNMutableNotificationContent()
content.title = "My Notification Title"
content.body = "Body of Notification"
// Create the request
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString,
content: content, trigger: trigger)
// Schedule the request with the system.
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.add(request) { (error) in
if error != nil {
// Handle any errors.
}
}
}