Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

registrationPayload.ts

50 lines2.0 KB

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