Test your notification interfaces on watchOS using a notification scheme and payload file.
When you are ready to test your dynamic interface in the simulator, create a custom build scheme for running your notification interface. To configure the scheme, specify the JSON data file containing the test data you want delivered to your interface.
When you create a new WatchKit App project or add a WatchKit App template to an existing project, Xcode adds notification scenes by default. This includes setting up the scheme and providing
Push, a file with test JSON data. This file contains most of the keys you need to simulate a remote notification; however, you can add more keys.
If Xcode added the notification scenes for you, just modify the
Push file so that it contains test data appropriate for your app. If you added the notification scenes yourself, you need to set up the scheme and payload file as well.
Add a Scheme
If you add notifications to your app, you also need to add a scheme to test the notifications. To test multiple notification categories, add a scheme for each category.
To add a scheme, click the current scheme in Xcode’s toolbar, and select New Scheme.
In the new scheme sheet, set the Target to your WatchKit app, and give the scheme a meaningful name.
Click OK. Xcode adds and selects the scheme. To configure the new scheme, click the current scheme again, and select Edit Scheme.
In the edit scheme sheet, set the Watch Interface to Static Notification or Dynamic Notification, depending on which interface you want to test. Also set the Notification Payload to the desired payload file. Then click Close.
The scheme is now ready to run.
Create a Payload File
To test notification interfaces, you can specify a payload file that contains the test data for your notification. Payload files are text files with an
.apns extension. They contain the JSON payload data for your notification. The default
Push contents are shown below:
To add a new payload file:
Select File > New > File.
In the file template sheet, make sure the iOS tab is selected, then scroll down to the Apple Watch group. Select the Notification Simulation File template and click Next.
Name the payload file and click Create.
The template creates a new file with the default JSON payload data. To use this file, select it in the Scheme editor when configuring the build scheme for a notification interface. You can include multiple payload files in your project and either change the build scheme or create multiple build schemes to simplify your testing.
Edit the JSON Payload
Most of the JSON data is packaged into a dictionary and delivered to your app at runtime. You can modify the contents of the payload file to match the actual content of typical notifications sent to your app. At a minimum, you should change the value of the
category key to match the name of your notification category. For more information on creating a notification payload, see Payload Key Reference.
Note that the payload file includes a
Watch key. This key is not part of a normal notification’s payload. The Apple Watch simulator doesn’t have access to your iOS app’s registered actions, so you must define the actions in the payload file. The
Watch key contains an array of dictionaries, each of which represents an action button.
Each dictionary can contain the following keys:
The title of the action button.
A string that identifies the action selected by the user. When the user taps the button, the system calls your notification center delegate’s
usermethod. The system assigns the value of this key to the
Notification Center: did Receive Notification Response: with Completion Handler:
actionproperty of the
UNNotificationobject passed to this method.
The only valid value for this key is the string textInput. If this key is present, the resulting button triggers text input.
1causes the resulting button to be rendered in a way that indicates it performs a destructive action. The value
0causes the button to be rendered normally.
1causes the button to launch the app in the background.
Send Test Notifications to the Watch
Because the system automatically determines whether to deliver notifications to Apple Watch, when testing on the device, you may need to set the devices' states to make sure the notification is routed to Apple Watch as expected. To test your notification interfaces while the device is not on your wrist:
Disable Wrist Detection on Apple Watch. You can set this in the Watch app on the companion iPhone or in the Settings app on the watch. The option is under Passcode > Wrist Detection.
Make sure your Apple Watch is not on a charger.
Make sure your iPhone is locked.