iOS 报警通知

问题描述

在我的应用中,我需要一个类似于 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) 
}

这可能是我想要的吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)