如何在 WatchOS-App 中包含通知场景?XCode V 14.2 中缺少选项

hsc*_*ler 7 xcode ios swift watchos swiftui

我是 SwiftUI 开发新手,所以我学习了官方 SwiftUI-Landmark 教程。创建 watchOS 应用程序一章提到,在添加 WatchOSApp 目标时,应选中模板中的“包含通知场景”复选框。然而,在我的 Xcode V 14.2 中没有这样的复选框。难道我做错了什么?以后有可能添加这个场景吗?

这是我可以选择的选项: 创建 WatchOs 应用程序

我还在苹果开发者论坛中发布了这个问题。但直到现在还没有人回答。

编辑 2024 年 1 月

非常感谢大家的回答。地标教程现在也已更新,不再需要“包含通知场景”复选框。

Art*_*m M 8

事实上,Xcode 14+ 中缺少该复选框(截至 2023 年 1 月)。在 Xcode 13 中,当您选择该复选框时,它会创建其他文件,这些文件是:NotificationView.swiftNotificationController.swiftComplicationController.swiftPushNotificationPayload.apns,以及用于启动通知和复杂功能的两个方案。

\n

幸运的是,完成本教程不需要复杂的操作,因此您只需创建 3 个文件和一个方案,然后再继续第5 节 \xe2\x80\x94\n创建自定义通知界面

\n

我在博客文章中提供了带有屏幕截图的详细说明,但这里是您必须执行的操作的简短描述:

\n

第1步:创建一个NotificationView.swift

\n

在WatchLandmarks Watch App文件夹中创建一个新的 SwiftUI View 文件。命名它NotificationView.swift

\n
import SwiftUI\n\nstruct NotificationView: View {\n    var body: some View {\n        Text("Hello, World!")\n    }\n}\n\nstruct NotificationView_Previews: PreviewProvider {\n    static var previews: some View {\n        NotificationView()\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

不要对其存根内容感到困惑。该文件将在 Apple 教程的下一部分中进行修改。现在,您只需要这些文件处于在 Xcode 13 中创建的状态。

\n

第2步:创建NotificationController.swift

\n

NotificationController.swift在同一文件夹中创建一个名为的 Swift 文件:

\n
import WatchKit\nimport SwiftUI\nimport UserNotifications\n\nclass NotificationController: WKUserNotificationHostingController<NotificationView> {\n\n    override var body: NotificationView {\n        return NotificationView()\n    }\n\n    override func willActivate() {\n        // This method is called when watch view controller is about to be visible to user\n        super.willActivate()\n    }\n\n    override func didDeactivate() {\n        // This method is called when watch view controller is no longer visible\n        super.didDeactivate()\n    }\n\n    override func didReceive(_ notification: UNNotification) {\n        // This method is called when a notification needs to be presented.\n        // Implement it if you use a dynamic notification interface.\n        // Populate your dynamic notification interface as quickly as possible.\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

第3步:创建PushNotificationPayload.apns

\n

在同一文件夹中,创建一个文件(新文件屏幕的底部)并将其命名为PushNotificationPayload.apns. 粘贴其内容:

\n
{\n    "aps": {\n        "alert": {\n            "body": "Test message",\n            "title": "Optional title",\n            "subtitle": "Optional subtitle"\n        },\n        "category": "myCategory",\n        "thread-id": "5280"\n    },\n    \n    "WatchKit Simulator Actions": [\n        {\n            "title": "First Button",\n            "identifier": "firstButtonAction"\n        }\n    ],\n    \n    "customKey": "Use this file to define a testing payload for your notifications. The aps dictionary specifies the category, alert text and title. The WatchKit Simulator Actions array can provide info for one or more action buttons in addition to the standard Dismiss button. Any other top level keys are custom payload. If you have multiple such JSON files in your project, you\'ll be able to select them when choosing to debug the notification interface of your Watch App."\n}\n
Run Code Online (Sandbox Code Playgroud)\n

第 4 步:创建通知方案

\n

现在您需要创建一个方案来使用刚刚创建的自定义视图运行通知。

\n
    \n
  • 单击 Xcode 工具栏中的方案选择器,然后在下拉列表中选择New Scheme\xe2\x80\xa6 。
  • \n
  • 选择 Watch App 作为目标,并添加 (Notification)到方案名称中以便清楚起见。单击“确定”
  • \n
  • 再次单击方案,确保选择了通知方案,然后单击编辑方案
  • \n
  • 在弹出窗口中,选择左侧面板中的“运行”行,然后将“监视界面”更改为“动态通知”。此后,Notification Payload字段应自动切换为PushNotificationPayload.apns
  • \n
  • 单击“关闭”
  • \n
\n

在此状态下,您可以轻松地继续本教程。

\n