Skip to content
Source
Sign inSign up

registrationPayload.ts

58 lines2.2 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 | {
25 readonly platform: "android";
26 readonly androidApiLevel: number;
27 readonly notificationPermissionGranted: boolean;
28 }
29 ),
30): RelayDeviceRegistrationRequest {
31 const pushAvailable = supportsAgentAwarenessPush();
32 const liveActivitiesEnabled =
33 pushAvailable &&
34 input.preferences.liveActivitiesEnabled !== false &&
35 (input.platform !== "android" ||
36 (input.notificationPermissionGranted && Boolean(input.pushToken)));
37 return {
38 deviceId: input.deviceId,
39 label: input.label,
40 platform: input.platform ?? "ios",
41 ...(input.platform === "android"
42 ? { androidApiLevel: input.androidApiLevel }
43 : { iosMajorVersion: input.iosMajorVersion }),
44 appVersion: input.appVersion,
45 ...(input.bundleId ? { bundleId: input.bundleId } : {}),
46 ...(input.apsEnvironment ? { apsEnvironment: input.apsEnvironment } : {}),
47 ...(input.pushToken ? { pushToken: input.pushToken } : {}),
48 ...(input.pushToStartToken ? { pushToStartToken: input.pushToStartToken } : {}),
49 preferences: {
50 liveActivitiesEnabled,
51 notificationsEnabled: pushAvailable && input.notificationsEnabled,
52 notifyOnApproval: true,
53 notifyOnInput: true,
54 notifyOnCompletion: true,
55 notifyOnFailure: true,
56 },
57 };