watchOS: Complications

Feb 7 2023 · Swift 5.6, watchOS 8.5, Xcode 13

Part 1: Introduction to Complications

08. Update with Push Notifications

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 07. Update with Background URL Downloads Next episode: 09. Tint Complications

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 08. Update with Push Notifications

Note: At the time of writing, watchOS has a bug—verified by Apple—that sometimes prevents your app from registering for PushKit notifications. Apple told me it believes it has determined the root cause. However, there’s no ETA on when the fix will be available.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

While background tasks and background URL downloads work well, they’re not always the best solution.

PushKit registration

Complication push notifications are a bit different from standard remote push notifications.

import Foundation
import PushKit

final class PushNotificationProvider: NSObject {

}
final class PushNotificationProvider🟩: NSObject {

}
  let registry = PKPushRegistry(queue: .main)
  override init() {
    super.init()
    registry.delegate = self
  }
    registry.desiredPushTypes = [.complication]
  }

PKPushRegistryDelegate

Now to start implementing the delegate, open an extension and adopt PKPushRegistryDelegate.

extension PushNotificationProvider: PKPushRegistryDelegate {

}
  func pushRegistry(
    _ registry: PKPushRegistry,
    didUpdate pushCredentials: PKPushCredentials,
    for type: PKPushType
  ) {
  
  }
    let token = pushCredentials.token.reduce("") {
      $0 + String(format: "%02x", $1)
    }
    print(token)

    // Send token to your server.

Receiving a push notification

Now that things are set up, we can handle receiving a PushKit notification with another delegate method:

func pushRegistry(
  _ registry: PKPushRegistry,
  didReceiveIncomingPushWith payload: PKPushPayload,
  for type: PKPushType
) async {

}
  print(payload.dictionaryPayload)
  await ExtensionDelegate.updateActiveComplications()
}

Initialize the provider

The only step left is to initialize the Push Notification Provider, which we can do in UpdatesApp.swift.

private let push = PushNotificationProvider()

apns-topic

Note: There’s one special consideration when sending a PushKit notification. The apns-topic header should be the name of your extension’s bundle identifier with .complication appended to it.

Testing

Other than the extra text you need to add to the apns-topic, there’s nothing special about testing push notifications, here. If you’re able to register your app for PushKit notifications, you can test it the same way you would do with an iPhone.