Я тестирую свой код, чтобы увидеть, как работает UserNotifications, и у меня есть этот код, который должен отображать три кнопки, и он показывает уведомление, но не показывает ни одну из кнопок. Что я делаю не так?
Я следовал инструкциям на
Этот код находится в классе контроллера представления моего проекта iOS.
let center = UNUserNotificationCenter.current()
var categories = Set<UNNotificationCategory>()
override func viewDidLoad() {
super.viewDidLoad()
center.requestAuthorization(options: [.alert, .badge, .carPlay, .sound]) {
(granted, error) in
guard error == nil else {
print(error!.localizedDescription)
return
}
guard granted else {
print("granted=", granted)
return
}
print("!!!")
}
let actionOK = UNNotificationAction(identifier: "HELLO_OK", title: "OK", options: UNNotificationActionOptions(rawValue: 0))
let actionTwo = UNNotificationAction(identifier: "TWO", title: "Two", options: UNNotificationActionOptions(rawValue: 0))
let actionThree = UNNotificationAction(identifier: "THREE", title: "Three", options: UNNotificationActionOptions(rawValue: 0))
let categoryGreeting = UNNotificationCategory(identifier: "HELLO", actions: [actionOK, actionTwo, actionThree], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: "", options: .customDismissAction)
categories.insert(categoryGreeting)
center.setNotificationCategories(categories)
let content = UNMutableNotificationContent()
content.title = "Hello World!"
content.body = "May the Force be with you."
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (10), repeats: false)
// Create the request
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString,
content: content, trigger: trigger)
// Schedule the request with the system.
center.add(request) { (error) in
if error != nil {
// Handle any errors.
}
}
}
Я просмотрел старые вопросы в stackoverflow, но они относились к более старым версиям iOS и мне не помогли.