1import Constants from "expo-constants";
2import { requireOptionalNativeModule } from "expo";
3import { Linking, Platform } from "react-native";
4
5interface AndroidAgentNotifications {
6 configure(deviceId: string, userId: string, scheme: string, ongoingEnabled: boolean): void;
7 clear(): void;
8 setThreadOnScreen?(path: string | null): void;
9 openLiveUpdateSettings?(): boolean;
10 showShowcaseActivity?(scheme: string, data: Record<string, string>): void;
11}
12
13const native =
14 Platform.OS === "android"
15 ? requireOptionalNativeModule<AndroidAgentNotifications>("T3AgentNotifications")
16 : null;
17
18export function supportsAndroidAgentNotifications(): boolean {
19 return typeof native?.configure === "function" && typeof native?.clear === "function";
20}
21
22function appScheme(): string {
23 const scheme = Constants.expoConfig?.scheme;
24 return (Array.isArray(scheme) ? scheme[0] : scheme) ?? "t3code";
25}
26
27export function configureAndroidAgentNotifications(
28 deviceId: string,
29 userId: string,
30 ongoingEnabled: boolean,
31): void {
32 native?.configure?.(deviceId, userId, appScheme(), ongoingEnabled);
33}
34
35/** Posts a staged relay payload for the showcase capture; false when unsupported. */
36export function showAndroidShowcaseAgentActivity(data: Record<string, string>): boolean {
37 if (!native?.showShowcaseActivity) return false;
38 native.showShowcaseActivity(appScheme(), data);
39 return true;
40}
41
42export function clearAndroidAgentNotifications(): void {
43 native?.clear?.();
44}
45
46/** Tells the FCM handler which thread route is on screen so its alerts stay quiet. */
47export function setAndroidThreadOnScreen(path: string | null): void {
48 native?.setThreadOnScreen?.(path);
49}
50
51export function supportsAndroidLiveUpdateSettings(): boolean {
52 return Platform.OS === "android" && Number(Platform.Version) >= 36;
53}
54
55export async function openAndroidLiveUpdateSettings(): Promise<void> {
56 if (!native?.openLiveUpdateSettings?.()) {
57 await Linking.openSettings();
58 }
59}