为什么我的 didRecive 和 willPresent 不起作用(操作按钮什么都不做)?

Why my didRecive and willPresent aren’t working(action buttons do nothing)?

我想发出一个可操作的通知。它仅在我的应用程序关闭时出现。当我按下操作按钮时没有任何反应

我的class个通知

class Notfication : NSObject, UNUserNotificationCenterDelegate {

func request(answer : rope, RopesBas : RopesBase){
    let taskCategory = UNNotificationCategory(identifier:  "task", actions: [UNNotificationAction(identifier: "done", title: "Done")], intentIdentifiers: [])
    UNUserNotificationCenter.current().setNotificationCategories([taskCategory])
    print("0 stage")
    
    let content = UNMutableNotificationContent()
    content.title = answer.name
    content.sound =
    UNNotificationSound.default
    content.categoryIdentifier = "task"
    content.userInfo=["name" : answer.name]
    print("1 stage")
    
    // show this notification five seconds from now
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    print("2 stage")
    
    //UNUserNotificationCenter.current().delegate = self
    // choose a random identifier
    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
    print("3 stage")
    
    // add our notification request
    UNUserNotificationCenter.current().add(request){ (error : Error?) in
        if let theError = error {
            print(theError.localizedDescription)
        }
    }
    print("4 stage")
    print(UNUserNotificationCenter.current().delegate)
    print("ok")
}


func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("5 stage")
let userInfo = response.notification.request.content.userInfo
let id = "dad"
print("6 stage")
print(response.actionIdentifier)
switch response.actionIdentifier {
case "done":
    base.ropes.removeAll(where: {[=10=].name == id})
    break
case UNNotificationDefaultActionIdentifier,
UNNotificationDismissActionIdentifier:
    
    break
default :
    break
}
    print(base.ropes.count)
    completionHandler()
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("q")
    completionHandler([.badge,.banner,.sound,.list])
}
}

我在 sheet 视图

上宣布此 class
struct Adding: View {
private let publisher = Notfication()

当我在 sheet 视图中按下按钮时使用它

Button(action: {
                RopesDB.ropes.append(rope(name: answer.answer))
                publisher.request(answer: RopesDB.ropes.last!, RopesBas: RopesDB)
                dismiss()
            }, label: {
                HStack{
                    Spacer()
                    Text(answer.answer)
                    Spacer()
                }

我试过将委托放入

class AppDelegate: NSObject, UIApplicationDelegate{
internal func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    let center = UNUserNotificationCenter.current()
    let options : UNAuthorizationOptions = [.alert, .badge, .sound]
    center.requestAuthorization(options: options) { granted, error in
        if granted{
            UNUserNotificationCenter.current().delegate = Notfication()
        }
            if let error = error {
                print(error.localizedDescription)
            }
    }
    return true
}
//

} 并进入评论的 class 我的错误是什么? IDE Swiftplaygrounds(iPad 9, iOS 15.5)

Apple 在其文档中声明通知仅在应用程序处于后台时显示。当应用程序已经在前台时,通知将直接传递到您的应用程序中: https://developer.apple.com/documentation/usernotifications/handling_notifications_and_notification-related_actions

您需要在委托的 userNotificationCenter(_:willPresent:withCompletionHandler:) 方法中接收通知,然后您的应用可以决定如何处理它。例如。将用户带到某个视图,显示模态等

我发现这个东西不能在 Playgrounds 上运行,我想