iOS 报警通知

时间:2021-05-13 15:51:53

标签: ios alarm unnotificationrequest

在我的应用中,我需要一个类似于 Apple 闹钟的通知系统。主要区别在于,我希望警报触发 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) 
}

这可能是我想要的吗?

0 个答案:

没有答案