Sending Push Notifications to two Apple Watches paired to one iPhone

Photo by Michał Kubalczyk on Unsplash

The problem

It is possible to pair multiple Apple Watches to one single phone. However, this is not the same as being connected to that device. Only one Apple Watch can be connected to an iPhone at the same time.

  1. If you turn off your iPhone, Apple Watch is still able to receive iMessages
  2. Family setup in WatchOS 7 (which is only available on cellular models) sort of uses the same setup we would like to achieve
  3. On this Apple Developer page, Apple states:

Devices and Frameworks

The devices we used in this experiment were all able to run the latest versions of their operating systems:

  • iPhone 12 Pro Max, running iOS 14.3
  • Apple Watch S5, running WatchOS 7.2
  • Apple Watch SE, running WatchOS 7.2
  • Leaf, a templating framework to generate a little front end
  • Fluent, an ORM framework
  • SQLite, to store the push tokens from the Watch
  • Vapor-APNS, an extension for Vapor to send Push Notifications via APNs

Developing

I would advice you to read the Install and Getting Started page on Vapor docs first, on order to get your machine ready.

Configuring middleware

With that done, we need to configure these frameworks in the configure.swift file:

app.views.use(.leaf)
app.databases.use(.sqlite(.memory), as: DatabaseID.sqlite)app.migrations.add(CreateDevice(), to: .sqlite)try app.autoMigrate().wait()
try app.configurePush()

Configuring push

To support push notifications in your Apple Watch app, you need to do some extra stuff, compared to an iOS app. First, you’ll need to get a push notifications key. In the Ray Wenderlich article, it is explained how you can do this. You can only download your .p8 file once, so be careful with it!

Creating the Apple Watch app
let appleECP8PrivateKey ="""-----BEGIN PRIVATE KEY-----<KEY GOES HERE>-----END PRIVATE KEY-----"""

API endpoints

We need and endpoint to receive the token on the server and to show which devices are registered. We need some way to send the push notifications to APNs. We will use a controller for that, which is a collection of routes. This controller will have three functions:

  • list, which returns a list of all registered devices
  • put, which saves the token to the in-memory database
  • sendTestPush, which sends a push notification based to a specified device
let apiRoutes = app.grouped("api")try apiRoutes.grouped("push").register(collection: DeviceController())

A simple webpage

To display the registered devices and the buttons we make a very simple webpage, using leaf:

Let's play!

With everything configured, we should now be able to register your Apple Watches to the server and send push notifications to it. Both watches are paired to the iPhone beforehand and the apps are loaded onto the devices with Xcode. They need to at least be launched once in order to accept push notifications. Take a close look to the console, because sometimes the registration of the token might fail the first time. Usually when you force close the app on the watch and open it again, it should work fine.

Apple Watch 44mm Series 5 on the left and Apple Watch SE 40mm on the right.
The Id and the tokens are anonymized.
Sending notifications to your Apple Watches!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dennis Vermeulen

Dennis Vermeulen

💼iOS Developer // 🇳🇱The Netherlands // 👨‍🎓Apple Developer Academy Alumnus