Repository navigation
Expand file tree
/
Copy pathpush.ts
More file actions
49 lines (40 loc) · 1.55 KB
/
Copy pathpush.ts
File metadata and controls
49 lines (40 loc) · 1.55 KB
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
import * as Notifications from 'expo-notifications';
import Constants, { ExecutionEnvironment } from 'expo-constants';
import { Platform } from 'react-native';
import { supabase } from './supabase';
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowBanner: true,
shouldShowList: true,
shouldPlaySound: true,
shouldSetBadge: false,
}),
});
const isExpoGo = Constants.executionEnvironment === ExecutionEnvironment.StoreClient;
export async function registerForPushNotifications(userId: string): Promise<string | null> {
// Remote push notifications are not supported in Expo Go (SDK 53+).
if (isExpoGo) return null;
const projectId = Constants.expoConfig?.extra?.eas?.projectId;
if (!projectId || projectId === 'your-eas-project-id') return null;
if (Platform.OS === 'android') {
await Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
});
}
const existing = await Notifications.getPermissionsAsync() as any;
let granted: boolean = existing.granted ?? existing.status === 'granted';
if (!granted) {
const r = await Notifications.requestPermissionsAsync() as any;
granted = r.granted ?? r.status === 'granted';
}
if (!granted) return null;
const token = (await Notifications.getExpoPushTokenAsync({ projectId })).data;
await supabase.from('push_tokens').upsert({
expo_token: token,
user_id: userId,
platform: Platform.OS,
updated_at: new Date().toISOString(),
});
return token;
}