为什么我的操作按钮不会出现在我的通知中?

时间:2018-06-15 02:33:22

标签: ios usernotifications

我正在测试我的代码,看看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,但它们对我没用。

1 个答案:

答案 0 :(得分:0)

这是一个简单的错误。我没有设置UNMutableNotificationContent对象的categoryIdentifier。解决方案是将其设置为“HELLO”。