iOS警报通知

iOS警报通知,ios,alarm,unnotificationrequest,Ios,Alarm,Unnotificationrequest,在我的应用程序中,我需要一个类似苹果闹钟的通知系统。主要区别在于,我希望警报触发URLSession请求,而不是播放警报声,因此我需要一个通知的完成处理程序。如果使用计时器,该行为将按预期工作,但在后台模式下不会触发 我尝试使用UNNotificationCenter,但无法使其工作。我需要发送通知,即使设备已关闭,且应用程序处于后台模式。我可以接受设定时间之间1-2分钟的时差,但希望尽可能精确。我也无法向它添加完成处理程序 override func viewDidLoad() {

在我的应用程序中,我需要一个类似苹果闹钟的通知系统。主要区别在于,我希望警报触发URLSession请求,而不是播放警报声,因此我需要一个通知的完成处理程序。如果使用计时器,该行为将按预期工作,但在后台模式下不会触发

我尝试使用UNNotificationCenter,但无法使其工作。我需要发送通知,即使设备已关闭,且应用程序处于后台模式。我可以接受设定时间之间1-2分钟的时差,但希望尽可能精确。我也无法向它添加完成处理程序

override func viewDidLoad() {

    let center = UNUserNotificationCenter.current()
    
    let content = UNMutableNotificationContent()
    content.title = "Late wake up call"
    content.body = "The early bird catches the worm, but the second mouse gets the cheese."
    content.categoryIdentifier = "alarm"
    content.userInfo = ["customData": "fizzbuzz"]
    content.sound = UNNotificationSound.default

    var dateComponents = DateComponents()
    dateComponents.hour = 10
    dateComponents.minute = 30
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
    center.add(request) 
}
这就是我想要的吗