added push notifications

This commit is contained in:
2026-06-03 19:13:56 +02:00
parent a9278b8269
commit 6559a9cd4b
24 changed files with 1598 additions and 557 deletions
+35
View File
@@ -0,0 +1,35 @@
import {
getMessaging,
requestPermission,
getToken,
onTokenRefresh,
AuthorizationStatus,
} from '@react-native-firebase/messaging';
import { post } from '@/services/api';
export async function registerDeviceToken(authToken: string): Promise<void> {
const messaging = getMessaging();
const status = await requestPermission(messaging);
const granted =
status === AuthorizationStatus.AUTHORIZED ||
status === AuthorizationStatus.PROVISIONAL;
if (!granted) return;
const fcmToken = await getToken(messaging);
await fetch(`${process.env.EXPO_PUBLIC_API_URL}/device-tokens/register`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`,
},
body: JSON.stringify({ token: fcmToken }),
});
}
export function listenForTokenRefresh(authToken: string): () => void {
return onTokenRefresh(getMessaging(), async (newToken) => {
await post('/device-tokens/register', { token: newToken }, authToken);
});
}