gpt4 book ai didi

ios - UNNotificationRequest 每天在特定时间发送本地通知

转载 作者:行者123 更新时间:2023-12-03 08:34:49 35 4
gpt4 key购买 nike

由于 UILocalNotification 在 iOS10 中已被弃用,我无法理解如何将以下代码更新到 UNNotificationRequest 框架。我基本上让用户在他们选择的时间安排每日通知。例如,如果他们想每天上午 11:00 收到通知。以下代码适用于 iOS10 以下的 iOS 版本,但由于 UILocalNotification 已弃用,因此它不再有效。非常感谢任何帮助。

    let notification = UILocalNotification()
notification.fireDate = fixedNotificationDate(datePicker.date)
notification.alertBody = "Your daily alert is ready for you!"
notification.timeZone = TimeZone.current
notification.repeatInterval = NSCalendar.Unit.day
notification.applicationIconBadgeNumber = 1
UIApplication.shared.scheduleLocalNotification(notification)

最佳答案

您可以使用 UNCalendarNotificationTrigger 创建使用 UNUserNotificationCenter 重复触发的通知。你可以做这样的事情。诀窍是触发日期中仅包含时间部分。

        let center = UNUserNotificationCenter.current()

let content = UNMutableNotificationContent()
content.title = "Attention!"
content.body = "Your daily alert is ready for you!"
content.sound = UNNotificationSound.default

let identifier = "com.yourdomain.notificationIdentifier"

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

let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

center.add(request, withCompletionHandler: { (error) in
if let error = error {
// Something went wrong
print("Error : \(error.localizedDescription)")
} else {
// Something went right
print("Success")
}
})

关于ios - UNNotificationRequest 每天在特定时间发送本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64212375/

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