如何解决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 举报,一经查实,本站将立刻删除。