Ios 为什么赢了';我的操作按钮是否显示在我的通知上?

Ios 为什么赢了';我的操作按钮是否显示在我的通知上?,ios,usernotifications,Ios,Usernotifications,我正在测试我的代码,看看UserNotifications是如何工作的,我的代码应该显示三个按钮,它显示了通知,但没有显示任何按钮。我做错了什么 我在办公室里按照指示做了 此代码位于iOS项目的view controller类中 let center = UNUserNotificationCenter.current() var categories = Set<UNNotificationCategory>() override func viewDidLoad(

我正在测试我的代码,看看UserNotifications是如何工作的,我的代码应该显示三个按钮,它显示了通知,但没有显示任何按钮。我做错了什么

我在办公室里按照指示做了

此代码位于iOS项目的view controller类中

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.  
        }  
    }  

}  
let center=UNUserNotificationCenter.current()
var categories=Set()
重写func viewDidLoad(){
super.viewDidLoad()
requestAuthorization(选项:[.alert、.badge、.carPlay、.sound]){
(同意,错误)在
保护错误==nil else{
打印(错误!.localizedDescription)
返回
}  
其他{
打印(“已授予=”,已授予)
返回
}  
打印(!!!)
}  
让actionOK=UNNotificationAction(标识符:“HELLO_OK”,标题:“OK”,选项:UNNotificationActionOptions(rawValue:0))
让actionTwo=UNNotificationAction(标识符:“TWO”,标题:“TWO”,选项:UNNotificationActionOptions(rawValue:0))
让actionThree=UNNotificationAction(标识符:“THREE”,标题:“THREE”,选项:UNNotificationActionOptions(原始值:0))
let categoryGreeting=UNNotificationCategory(标识符:“HELLO”,操作:[actionOK,Action2,Action3],意向标识符:[],HiddenPreviewBodyPlaceholder:],选项:。customDismissAction)
类别.插入(类别浏览)
center.setNotificationCategories(类别)
let content=UNMutableNotificationContent()
content.title=“你好,世界!”
content.body=“愿原力与你同在。”
let trigger=UNTimeIntervalNotificationTrigger(时间间隔:(10),重复:false)
//创建请求
设uuidString=UUID().uuidString
let request=UNNotificationRequest(标识符:uuistring,
内容:内容,触发器:触发器)
//使用系统计划请求。
在中添加(请求){(错误)
如果错误!=nil{
//处理任何错误。
}  
}  
}  

我在stackoverflow上查看了一些老问题,但它们适用于旧版本的iOS,对我没有帮助。

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