我的简单应用程序有问题。我不明白为什么我的代码不起作用。这是在我的 WatchOS 应用程序中包含通知场景的代码
@main
struct MyApp: App {
@SceneBuilder var body: some Scene {
WindowGroup {
NavigationView {
ContentView()
}
}
WKNotificationScene(
controller: NotificationController.self,
category: LocalNotifications.categoryIdentifier
)
在 LocalNotifications 我有以下代码:
static let categoryIdentifier = "Scala5"
func register() async throws {
print("Register")
let current = UNUserNotificationCenter.current()
try await current.requestAuthorization(options: [.alert, .sound])
current.removeAllPendingNotificationRequests()
let action = UNNotificationAction(
identifier: self.actionIdentifier,
title: "Press me",
options: .foreground)
let category = UNNotificationCategory(
identifier: Self.categoryIdentifier,
actions: [action],
intentIdentifiers: [])
current.setNotificationCategories([category])
current.delegate = self
}
所以我认为当我使用 Xcode 测试通知时,标题为“Press me”的按钮应该出现在通知中,但永远不会调用方法寄存器。在我的文件 file.apns 我有以下几行:
{
"aps": {
"alert": {
"body": "Test message",
"title": "Optional title",
"subtitle": "Optional subtitle"
},
"category": "Scala5"
}
}
根据苹果文档,apns 文件中的类别与最终类 LocalNotifications 中的类别相匹配。那么我的错误是什么?请提供任何帮助,我想要技术解决方案,但了解生命周期更为重要。谢谢