| 1 | import Foundation |
| 2 | |
| 3 | #if os(iOS) |
| 4 | import UIKit |
| 5 | #else |
| 6 | import AppKit |
| 7 | #endif |
| 8 | |
| 9 | /// Registering this device with Apple, and then with Calls, so that a |
| 10 | /// knock can reach it when the app is closed. |
| 11 | /// |
| 12 | /// The local half — KnockWatch — only reaches an app that is running. |
| 13 | /// This is the other half, and it is the only one that can help the |
| 14 | /// person whose Mac is asleep or whose phone is in a pocket with Calls |
| 15 | /// not open. |
| 16 | /// |
| 17 | /// Everything here is best effort and nothing is ever shown to anyone. A |
| 18 | /// device that could not register is a device that will not be told |
| 19 | /// about a knock, which is exactly where every device was before this |
| 20 | /// existed. It is never a reason to fail a join, and never a sheet. |
| 21 | @MainActor public enum RemoteKnocks { |
| 22 | /// The platform name the server stores alongside the token. The server |
| 23 | /// holds an APNs topic per platform (CALLS_APNS_TOPIC_IOS and _MACOS) and |
| 24 | /// sends each token under its platform's topic. Both apps are |
| 25 | /// works.oficina.calls since 2026-11-14, so today the two topics are the |
| 26 | /// same; a token sent under any other topic is refused. |
| 27 | public static var platform: String { |
| 28 | #if os(iOS) |
| 29 | return "ios" |
| 30 | #else |
| 31 | return "macos" |
| 32 | #endif |
| 33 | } |
| 34 | |
| 35 | /// Asks Apple for a device token. The answer arrives at the app |
| 36 | /// delegate, asynchronously and possibly never — without the push |
| 37 | /// entitlement, or on a build Apple will not issue one for, the |
| 38 | /// failure callback is what comes back instead. |
| 39 | public static func register() { |
| 40 | #if os(iOS) |
| 41 | UIApplication.shared.registerForRemoteNotifications() |
| 42 | #else |
| 43 | NSApplication.shared.registerForRemoteNotifications() |
| 44 | #endif |
| 45 | } |
| 46 | |
| 47 | /// Apple's token is opaque bytes; the server stores it as lower-case |
| 48 | /// hex, which is the form every APNs provider uses and the form the |
| 49 | /// registration endpoint validates. |
| 50 | public static func hex(_ token: Data) -> String { |
| 51 | token.map { String(format: "%02x", $0) }.joined() |
| 52 | } |
| 53 | } |
| 54 | |