import UserNotifications
let trigger = UNCalendarNotificationTrigger(dateMatching: D
ateComponents(hour: 20, minute: 00), repeats: true)
print(trigger.nextTriggerDate() ?? "nil")
let content = UNMutableNotificationContent()
content.title = "title"
content.body = "body"
// make sure you give each request a unique identifier. (nextTriggerDate
description)
let request = UNNotificationRequest(identifier: "identify", content: content,
trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print(error)
return
}
print("scheduled")
}
Не забудьте спросить пользователя о разрешении планировать уведомления в AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UNUserNotificationCenter.current().requestAuthorization(options:[.badge,
.alert, .sound]) { granted, error in
// your code
}
return true
}