| 1 | import type { RelayDeviceRegistrationRequest } from "@t3tools/contracts/relay"; |
| 2 | |
| 3 | import type { Preferences } from "../../persistence/mobile-preferences"; |
| 4 | import { supportsAgentAwarenessPush } from "./capabilities"; |
| 5 | |
| 6 | // Development builds are Xcode-signed and receive sandbox APNs tokens; |
| 7 | // preview and production builds are distribution-signed and use production |
| 8 | // APNs. The relay routes each device's pushes accordingly. |
| 9 | export function resolveApsEnvironment(appVariant: unknown): "sandbox" | "production" { |
| 10 | return appVariant === "development" ? "sandbox" : "production"; |
| 11 | } |
| 12 | |
| 13 | export function makeRelayDeviceRegistrationRequest( |
| 14 | input: { |
| 15 | readonly deviceId: string; |
| 16 | readonly label: string; |
| 17 | readonly appVersion?: string; |
| 18 | readonly bundleId?: string; |
| 19 | readonly apsEnvironment?: "sandbox" | "production"; |
| 20 | readonly pushToken?: string; |
| 21 | readonly pushToStartToken?: string; |
| 22 | readonly notificationsEnabled: boolean; |
| 23 | readonly preferences: Preferences; |
| 24 | } & ( |
| 25 | | { readonly platform?: "ios"; readonly iosMajorVersion: number } |
| 26 | | { readonly platform: "android"; readonly androidApiLevel: number } |
| 27 | ), |
| 28 | ): RelayDeviceRegistrationRequest { |
| 29 | const pushAvailable = supportsAgentAwarenessPush(); |
| 30 | const liveActivitiesEnabled = pushAvailable && input.preferences.liveActivitiesEnabled !== false; |
| 31 | return { |
| 32 | deviceId: input.deviceId, |
| 33 | label: input.label, |
| 34 | platform: input.platform ?? "ios", |
| 35 | ...(input.platform === "android" |
| 36 | ? { androidApiLevel: input.androidApiLevel } |
| 37 | : { iosMajorVersion: input.iosMajorVersion }), |
| 38 | appVersion: input.appVersion, |
| 39 | ...(input.bundleId ? { bundleId: input.bundleId } : {}), |
| 40 | ...(input.apsEnvironment ? { apsEnvironment: input.apsEnvironment } : {}), |
| 41 | ...(input.pushToken ? { pushToken: input.pushToken } : {}), |
| 42 | ...(input.pushToStartToken ? { pushToStartToken: input.pushToStartToken } : {}), |
| 43 | preferences: { |
| 44 | liveActivitiesEnabled, |
| 45 | notificationsEnabled: pushAvailable && input.notificationsEnabled, |
| 46 | notifyOnApproval: true, |
| 47 | notifyOnInput: true, |
| 48 | notifyOnCompletion: true, |
| 49 | notifyOnFailure: true, |
| 50 | }, |
| 51 | }; |
| 52 | } |