由于 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)
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用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")
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1173 次 |
| 最近记录: |