WatchOS,SwiftUI:如何使用';视图';作为主体

WatchOS,SwiftUI:如何使用';视图';作为主体,swiftui,watchkit,watchos,usernotifications,Swiftui,Watchkit,Watchos,Usernotifications,在创建XCode项目时,我选择了Apple Watch应用程序并启用了“包含通知”。 这使我在项目中有了NotificationView.swift和NotificationController.swift 我已在NotificationView.swift中填写了我希望在本地通知中显示的视图内容 在我的常规HostingController.swift中,我现在想安排一个包含NotificationView.swift内容的本地通知 到目前为止,我使用的是当前代码: let userNotif

在创建XCode项目时,我选择了Apple Watch应用程序并启用了“包含通知”。 这使我在项目中有了NotificationView.swift和NotificationController.swift

我已在NotificationView.swift中填写了我希望在本地通知中显示的视图内容

在我的常规HostingController.swift中,我现在想安排一个包含NotificationView.swift内容的本地通知

到目前为止,我使用的是当前代码:

let userNotificationCenter = UNUserNotificationCenter.current()
let notificationContent = UNMutableNotificationContent()
notificationContent.title  = "title"
notificationContent.body = "body"
notificationContent.categoryIdentifier = "categoryNameDummy"

let category = UNNotificationCategory(identifier: "categoryNameDummy", actions: [], intentIdentifiers: [] as? [String] ?? [String](), options: .customDismissAction)
let categories = Set<AnyHashable>([category])

userNotificationCenter.setNotificationCategories(categories as? Set<UNNotificationCategory> ?? Set<UNNotificationCategory>())

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)

let request = UNNotificationRequest(identifier: UUID().uuidString, content: notificationContent, trigger: trigger)
userNotificationCenter.add(request) { (error) in
if let error = error {
    debugPrint(error)
}
}
let userNotificationCenter=UNUserNotificationCenter.current()
让notificationContent=UNMutableNotificationContent()
notificationContent.title=“title”
notificationContent.body=“body”
notificationContent.categoryIdentifier=“categoryNameDummy”
让category=UNNotificationCategory(标识符:“categoryNameDummy”,操作:[],intentIdentifiers:[]作为?[String]?[String](),选项:.customDismissAction)
let categories=Set([类别])
userNotificationCenter.setNotificationCategories(类别为?Set??Set())
let trigger=UNTimeIntervalNotificationTrigger(时间间隔:2,重复:false)
let request=UNNotificationRequest(标识符:UUID().uuidString,内容:notificationContent,触发器:trigger)
userNotificationCenter.add(请求){(错误)在
如果let error=error{
调试打印(错误)
}
}
我没有对Info.plist进行任何关于类别标识符的更改。
现在,我必须在何处向“捕获”此通知添加代码,并用我的自定义NotificationView.swift内容填充它

您是否向用户请求发送通知的权限

    UNUserNotificationCenter.current().requestAuthorization(options: [.sound, .alert]) { success, error in
        if success {
            print("Ok!")
        } else if let error = error {
            print(error.localizedDescription)
        }
    }
它仅在第一次显示权限警报:一旦授予权限,它将不再显示