-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
216 lines (189 loc) · 5.85 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import React from "react";
import Constants from "expo-constants";
import { Platform } from "react-native";
import * as SplashScreen from "expo-splash-screen";
import * as Notifications from "expo-notifications";
import { useAuthStore } from "./src/store/authStore";
import { AppNavigator } from "./src/navigation/AppNavigator";
import { View, Text, ActivityIndicator, Alert } from "react-native";
import { ConvexProvider, ConvexReactClient } from "convex/react";
import { SafeAreaProvider } from "react-native-safe-area-context";
import { ErrorBoundary } from "./src/components/ErrorBoundary";
SplashScreen.preventAutoHideAsync();
Notifications.setNotificationHandler({
handleNotification: async () => {
try {
return {
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: false,
};
} catch (error) {
console.error("Error handling notification:", error);
return {
shouldShowAlert: false,
shouldPlaySound: false,
shouldSetBadge: false,
};
}
},
});
const initializeConvexClient = () => {
try {
const convexUrl = Constants.expoConfig?.extra?.convex?.apiUrl;
if (!convexUrl) {
throw new Error(
"Convex URL not configured. Please check your app.json configuration."
);
}
return new ConvexReactClient(convexUrl);
} catch (error) {
console.error("Error initializing Convex client:", error);
throw error;
}
};
const registerForPushNotificationsAsync = async () => {
let token;
try {
if (Platform.OS === "android") {
try {
await Notifications.setNotificationChannelAsync("default", {
name: "default",
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
sound: "notification.wav",
});
} catch (error) {
console.error("Error creating notification channel:", error);
}
}
if (!Constants.isDevice) {
console.log("Push notifications require physical device");
return undefined;
}
const { status: existingStatus } =
await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== "granted") {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== "granted") {
console.log("Push notification permission not granted");
return undefined;
}
const projectId = Constants.expoConfig?.extra?.eas?.projectId;
if (!projectId) {
throw new Error("Project ID is not configured in app.json");
}
token = (
await Notifications.getExpoPushTokenAsync({
projectId,
applicationId: Constants.expoConfig?.android?.package,
})
).data;
return token;
} catch (error) {
console.error("Error registering for push notifications:", error);
return undefined;
}
};
const LoadingScreen = () => (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<ActivityIndicator size="large" color="#0000ff" />
<Text style={{ marginTop: 10 }}>Loading...</Text>
</View>
);
export default function App() {
const [isReady, setIsReady] = React.useState(false);
const [error, setError] = React.useState<Error | null>(null);
const [convexClient, setConvexClient] =
React.useState<ConvexReactClient | null>(null);
const [expoPushToken, setExpoPushToken] = React.useState<
string | undefined
>();
const [notification, setNotification] =
React.useState<Notifications.Notification>();
const notificationListener = React.useRef<any>();
const responseListener = React.useRef<any>();
const initializeAuth = useAuthStore((state) => state.initializeAuth);
React.useEffect(() => {
async function initialize() {
try {
const client = initializeConvexClient();
setConvexClient(client);
await initializeAuth();
const token = await registerForPushNotificationsAsync();
setExpoPushToken(token);
await SplashScreen.hideAsync();
setIsReady(true);
} catch (e) {
console.error("Initialization error:", e);
setError(
e instanceof Error ? e : new Error("Failed to initialize app")
);
await SplashScreen.hideAsync();
}
}
initialize();
notificationListener.current =
Notifications.addNotificationReceivedListener((notification) => {
setNotification(notification);
});
responseListener.current =
Notifications.addNotificationResponseReceivedListener((response) => {
console.log("Notification response:", response);
});
return () => {
if (convexClient) {
convexClient.close();
}
if (notificationListener.current) {
Notifications.removeNotificationSubscription(
notificationListener.current
);
}
if (responseListener.current) {
Notifications.removeNotificationSubscription(responseListener.current);
}
};
}, []);
if (!isReady && !error) {
return <LoadingScreen />;
}
if (error) {
return (
<View
style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
padding: 20,
}}
>
<Text style={{ fontSize: 18, fontWeight: "bold", marginBottom: 10 }}>
Failed to Start App
</Text>
<Text style={{ textAlign: "center", color: "#666" }}>
{error.message}
</Text>
</View>
);
}
return (
<ErrorBoundary>
<SafeAreaProvider>
{convexClient ? (
<ConvexProvider client={convexClient}>
<View style={{ flex: 1 }}>
<AppNavigator />
</View>
</ConvexProvider>
) : (
<LoadingScreen />
)}
</SafeAreaProvider>
</ErrorBoundary>
);
}