Include Notification Scene is missing in Xcode Version 14.2

I am new to SwiftUI development so I did the official SwiftUI-Landmark tutorial. In the chapter Creating a watchOS App says that you should check the checkbox "Include Notification Scene" in the template when adding the WatchOSApp target. However, in my Xcode V 14.2 there is no such checkbox. Am I doing something wrong? Is there a possibility to add this scene afterwards?

This are the options i can choose:

Answered by artemsmikh in 743369022

Hey, I have left an answer to your question on SO, but just in case I'll answer here as well.

The checkbox is in fact missing in Xcode 14+ (as of Jan 2023). In Xcode 13 it creates additional files when you selected the checkbox, these files are: NotificationView.swift, NotificationController.swift, ComplicationController.swift, PushNotificationPayload.apns, as well as two schemes to launch the notification and the complication.

Fortunately, you don't need complications to complete the tutorial, so you have only to create 3 files and one scheme before moving to the Section 5 — Create a Custom Notification Interface.

I provided the detailed explanation with screenshots in a blog post: https://artemsmikh.me/2023/01/28/adding-a-notification-scene-to-an-apple-watch-app-in-xcode-14/, but here is a short description of what you have to do to continue with the tutorial:

Step 1: Create a NotificationView.swift

Create a new SwiftUI View file in the WatchLandmarks Watch App folder. Name it NotificationView.swift:

import SwiftUI

struct NotificationView: View {
    var body: some View {
        Text("Hello, World!")
    }
}

struct NotificationView_Previews: PreviewProvider {
    static var previews: some View {
        NotificationView()
    }
}

Do not be confused about its stub content. The file will be modified during the next section of the Apple tutorial. For now you just need these files in a state they were created in Xcode 13.

Step 2: Create a NotificationController.swift

Create a Swift file called NotificationController.swift in the same folder:

import WatchKit
import SwiftUI
import UserNotifications

class NotificationController: WKUserNotificationHostingController<NotificationView> {

    override var body: NotificationView {
        return NotificationView()
    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()
    }

    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }

    override func didReceive(_ notification: UNNotification) {
        // This method is called when a notification needs to be presented.
        // Implement it if you use a dynamic notification interface.
        // Populate your dynamic notification interface as quickly as possible.
    }
}

Step 3: Create a PushNotificationPayload.apns

In the same folder, create an Empty file (bottom of the new file screen) and call it PushNotificationPayload.apns. Paste its contents:

{
    "aps": {
        "alert": {
            "body": "Test message",
            "title": "Optional title",
            "subtitle": "Optional subtitle"
        },
        "category": "myCategory",
        "thread-id": "5280"
    },
    
    "WatchKit Simulator Actions": [
        {
            "title": "First Button",
            "identifier": "firstButtonAction"
        }
    ],
    
    "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."
}

Step 4: Create a Notification scheme

Now you need to create a scheme to run a notification with a custom view files files you just created.

  • Click Schemes — New Scheme….
  • Select the Watch App as the target and add (Notification) to the scheme name for the sake of clarity. Click OK.
  • Click the schemes again, make sure the Notification scheme is selected, and click Edit Scheme.
  • In the popup, select the Run row in the left panel, then change Watch Interface to Dynamic Notification. After this, the Notification Payload option should be automatically switched to PushNotificationPayload.apns.
  • Click Close.

From this state, you can safely continue with the tutorial.

Accepted Answer

Hey, I have left an answer to your question on SO, but just in case I'll answer here as well.

The checkbox is in fact missing in Xcode 14+ (as of Jan 2023). In Xcode 13 it creates additional files when you selected the checkbox, these files are: NotificationView.swift, NotificationController.swift, ComplicationController.swift, PushNotificationPayload.apns, as well as two schemes to launch the notification and the complication.

Fortunately, you don't need complications to complete the tutorial, so you have only to create 3 files and one scheme before moving to the Section 5 — Create a Custom Notification Interface.

I provided the detailed explanation with screenshots in a blog post: https://artemsmikh.me/2023/01/28/adding-a-notification-scene-to-an-apple-watch-app-in-xcode-14/, but here is a short description of what you have to do to continue with the tutorial:

Step 1: Create a NotificationView.swift

Create a new SwiftUI View file in the WatchLandmarks Watch App folder. Name it NotificationView.swift:

import SwiftUI

struct NotificationView: View {
    var body: some View {
        Text("Hello, World!")
    }
}

struct NotificationView_Previews: PreviewProvider {
    static var previews: some View {
        NotificationView()
    }
}

Do not be confused about its stub content. The file will be modified during the next section of the Apple tutorial. For now you just need these files in a state they were created in Xcode 13.

Step 2: Create a NotificationController.swift

Create a Swift file called NotificationController.swift in the same folder:

import WatchKit
import SwiftUI
import UserNotifications

class NotificationController: WKUserNotificationHostingController<NotificationView> {

    override var body: NotificationView {
        return NotificationView()
    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()
    }

    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }

    override func didReceive(_ notification: UNNotification) {
        // This method is called when a notification needs to be presented.
        // Implement it if you use a dynamic notification interface.
        // Populate your dynamic notification interface as quickly as possible.
    }
}

Step 3: Create a PushNotificationPayload.apns

In the same folder, create an Empty file (bottom of the new file screen) and call it PushNotificationPayload.apns. Paste its contents:

{
    "aps": {
        "alert": {
            "body": "Test message",
            "title": "Optional title",
            "subtitle": "Optional subtitle"
        },
        "category": "myCategory",
        "thread-id": "5280"
    },
    
    "WatchKit Simulator Actions": [
        {
            "title": "First Button",
            "identifier": "firstButtonAction"
        }
    ],
    
    "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."
}

Step 4: Create a Notification scheme

Now you need to create a scheme to run a notification with a custom view files files you just created.

  • Click Schemes — New Scheme….
  • Select the Watch App as the target and add (Notification) to the scheme name for the sake of clarity. Click OK.
  • Click the schemes again, make sure the Notification scheme is selected, and click Edit Scheme.
  • In the popup, select the Run row in the left panel, then change Watch Interface to Dynamic Notification. After this, the Notification Payload option should be automatically switched to PushNotificationPayload.apns.
  • Click Close.

From this state, you can safely continue with the tutorial.

Include Notification Scene is missing in Xcode Version 14.2
 
 
Q