skip to Main Content

I am new to SwiftUI development so I did the official SwiftUI-Landmark tutorial. 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:
Creating WatchOs App

I also posted this Question in the Apple Developer Forum. But till now nobody answered.

2

Answers


  1. Only if you want to customize that scene. Otherwise watchOS will just display your notifications in a default interface.

    When you create a project, you will see a generic ContentView file with your first view and a file named after your app that displays ContentView in a window in a scene.

    You can just create a new scene and window for notifications here and customize the view in a separate file (to stay organized) just like for your application.

    Login or Signup to reply.
  2. 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 on to the Section 5 —
    Create a Custom Notification Interface
    .

    I provided the detailed explanation with screenshots in the blog post, but here is a short description of what you have to do:

    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 you just created.

    • Click the scheme selector in the Xcode toolbar and select New Scheme… in the dropdown.
    • 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 field should be automatically switched to PushNotificationPayload.apns.
    • Click Close.

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search