gpt4 book ai didi

ios - UNNotification 的重复间隔

转载 作者:可可西里 更新时间:2023-11-01 00:38:39 25 4
gpt4 key购买 nike

有一个选项可以设置 UILocalNotification 的重复间隔。由于 Apple 已弃用 UILocalNotification 并建议改用 UNNotification,因此我找不到使用 UNNotification 设置具有自定义重复间隔的通知的方法。

    var comp = DateComponents()
comp.year = 2019
comp.month = 1
comp.day = 9
comp.hour = 14
comp.minute = 14
comp.second = 0
let calendar = Calendar.current
let notification: UILocalNotification = UILocalNotification()
notification.category = "Daily Quote"
notification.alertBody = "Body"
notification.alertTitle = "Title"
notification.fireDate = calendar.date(from: comp)
notification.repeatInterval = NSCalendar.Unit.day
UIApplication.shared.scheduleLocalNotification(notification)

那么我可以设置一个类似的通知,在等待初始通知后使用新的 UNNotification 每小时或每天重复吗?

最佳答案

要模仿 UILocalNotification 的 API fireDaterepeatInterval,您可以创建两个触发器,一个非重复触发器,用于 fireDate 启动和其他重复 repeatInterval

这是一个例子:

import UserNotifications

/// Schedules notificaiton to fire at specific date, and then it repeats by specified repeat component
/// (week, day, hour, etc.) and repeat interval. For example to repeat every 20minutes repeatComponent
/// would be .minute and repeatInterval would be 20.
/// - Parameters:
/// - fireDate: Date for initial notification delivery
/// - repeatComponent: Component by which repeating would be performed (week, day, hour, etc.)
/// - repeatInterval: Interval by which repeating by specified component would be performed. Defaults value is 1.
func scheduleNotification(fireDate: Date, repeatComponent: Calendar.Component, repeatInterval: Int = 1) {

let content = UNMutableNotificationContent()
content.title = "Daily Quote"
content.body = "Inspirational quote."
content.categoryIdentifier = "quote.category"

UNUserNotificationCenter.current().requestAuthorization(
options: [.alert,.sound])
{
(granted, error) in

if let error = error {
print("granted, but Error in notification permission:\(error.localizedDescription)")
}

let fireTrigger = UNTimeIntervalNotificationTrigger(timeInterval: fireDate.timeIntervalSinceNow, repeats: false)

let fireDateRequest = UNNotificationRequest(identifier: "quote.starter", content: content, trigger: fireTrigger)

UNUserNotificationCenter.current().add(fireDateRequest) {(error) in
if let error = error {
print("Error adding firing notification: \(error.localizedDescription)")
} else {

if let firstRepeatingDate = Calendar.current.date(byAdding: repeatComponent, value: repeatInterval, to: fireDate) {

let repeatingTrigger = UNTimeIntervalNotificationTrigger(timeInterval: firstRepeatingDate.timeIntervalSinceNow, repeats: true)

let repeatingRequest = UNNotificationRequest(identifier: "quote.repeater", content: content, trigger: repeatingTrigger)

UNUserNotificationCenter.current().add(repeatingRequest) { (error) in
if let error = error {
print("Error adding repeating notification: \(error.localizedDescription)")
} else {
print("Successfully scheduled")
// Successfully scheduled
}
}

}
}
}

UNUserNotificationCenter.current().delegate = self
}
}

委托(delegate)(用于调试):

extension ViewController: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("\(notification.request.identifier): \(Date())")
UNUserNotificationCenter.current().getPendingNotificationRequests { (requests) in
for request in requests {
if let timeIntervalTrigger = request.trigger as? UNTimeIntervalNotificationTrigger {
print(Date(timeIntervalSinceNow: timeIntervalTrigger.timeInterval))
}

}
}
}
}

根据您的要求使用:

let interval = 7 // One week from now
if let fireDate = Calendar.current.date(byAdding: .day, value: interval, to: Date()) {
_ = scheduleNotification(fireDate: fireDate, repeatComponent: .day)
}

注意

指定重复间隔小于 60 秒会导致异常:

'NSInternalInconsistencyException', reason: 'time interval must be at least 60 if repeating'

关于ios - UNNotification 的重复间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54076050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com