Почему мои кнопки действий не отображаются в моем уведомлении?

Я тестирую свой код, чтобы увидеть, как работает UserNotifications, и у меня есть этот код, который должен отображать три кнопки, и он показывает уведомление, но не показывает ни одну из кнопок. Что я делаю не так?

Я следовал инструкциям на

https://developer.apple.com/documentation/usernotifications/scheduling_a_notification_locally_from_your_app

https://developer.apple.com/documentation/usernotifications/declaring_your_actionable_notification_types

Этот код находится в классе контроллера представления моего проекта 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 и мне не помогли.


person Daniel Brower    schedule 15.06.2018    source источник


Ответы (1)


Это была простая ошибка. Я не установил categoryIdentifier объекта UNMutableNotificationContent. Решение состоит в том, чтобы установить его на «HELLO».

person Daniel Brower    schedule 15.06.2018