179 lines
6.0 KiB
TypeScript
179 lines
6.0 KiB
TypeScript
/**
|
|
* Push-Token-Registration mit Expo.
|
|
*
|
|
* Flow:
|
|
* 1. Permission anfragen (falls noch nicht entschieden)
|
|
* 2. ExponentPushToken[xxx] via getExpoPushTokenAsync() holen
|
|
* 3. Token an Backend POST /api/users/me/push-token senden
|
|
* 4. Bei Logout: DELETE /api/users/me/push-token?token=…
|
|
*
|
|
* Wird einmal pro App-Start nach Login aufgerufen — idempotent durch
|
|
* upsert im Backend.
|
|
*/
|
|
import { useEffect, useRef } from 'react';
|
|
import { Platform } from 'react-native';
|
|
import * as Notifications from 'expo-notifications';
|
|
import * as Device from 'expo-device';
|
|
import Constants from 'expo-constants';
|
|
import { apiFetch } from '../lib/api';
|
|
import { getDeviceId } from '../lib/deviceId';
|
|
|
|
// VoIP-PushKit (iOS only) — separater Push-Token für CallKit-Wake-from-killed.
|
|
// Lazy require: react-native-voip-push-notification ist iOS-only, Android-Bundle
|
|
// soll nicht crashen.
|
|
let RNVoipPushNotification: any = null;
|
|
if (Platform.OS === 'ios') {
|
|
try {
|
|
RNVoipPushNotification = require('react-native-voip-push-notification').default;
|
|
} catch {
|
|
// Modul (noch) nicht gelinkt — z.B. im Expo-Go-Fallback. Silent skip.
|
|
}
|
|
}
|
|
|
|
const lastRegisteredToken: { current: string | null } = { current: null };
|
|
const lastRegisteredVoipToken: { current: string | null } = { current: null };
|
|
|
|
/** Holt iOS-VoIP-Push-Token via PushKit. Resolve mit `null` wenn nicht verfügbar. */
|
|
async function fetchVoipToken(): Promise<string | null> {
|
|
if (Platform.OS !== 'ios' || !RNVoipPushNotification) return null;
|
|
return new Promise((resolve) => {
|
|
let resolved = false;
|
|
const onToken = (token: string) => {
|
|
if (resolved) return;
|
|
resolved = true;
|
|
resolve(token || null);
|
|
};
|
|
try {
|
|
// Listener registrieren BEVOR registerVoipToken — sonst race.
|
|
RNVoipPushNotification.addEventListener('register', onToken);
|
|
RNVoipPushNotification.registerVoipToken();
|
|
// Safety-Timeout: nach 4s aufgeben (Cert/Provisioning fehlt etc.).
|
|
setTimeout(() => {
|
|
if (resolved) return;
|
|
resolved = true;
|
|
resolve(null);
|
|
}, 4000);
|
|
} catch (err) {
|
|
if (__DEV__) console.warn('[voip] register failed:', err);
|
|
resolve(null);
|
|
}
|
|
});
|
|
}
|
|
|
|
export async function registerPushTokenWithBackend(): Promise<string | null> {
|
|
// Simulator/Emulator support: Expo Push funktioniert auf physical devices only
|
|
// — Simulator gibt Permission denied. Wir loggen aber crashen nicht.
|
|
if (!Device.isDevice) {
|
|
if (__DEV__) console.log('[push] skipped (simulator)');
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
// 1) Permission
|
|
const { status: existing } = await Notifications.getPermissionsAsync();
|
|
let status = existing;
|
|
if (existing !== 'granted') {
|
|
const { status: requested } = await Notifications.requestPermissionsAsync();
|
|
status = requested;
|
|
}
|
|
if (status !== 'granted') {
|
|
if (__DEV__) console.log('[push] permission denied');
|
|
return null;
|
|
}
|
|
|
|
// 2) Android-Channels (muss vor getExpoPushTokenAsync existieren)
|
|
if (Platform.OS === 'android') {
|
|
await Notifications.setNotificationChannelAsync('chat', {
|
|
name: 'Chat-Nachrichten',
|
|
importance: Notifications.AndroidImportance.HIGH,
|
|
vibrationPattern: [0, 250, 250, 250],
|
|
lightColor: '#007AFF',
|
|
sound: 'default',
|
|
});
|
|
// Dedizierter Channel für eingehende Anrufe — MAX importance, längere
|
|
// Vibration, bypasst DND nicht (das bräuchte Critical-Alert-Permission).
|
|
await Notifications.setNotificationChannelAsync('calls', {
|
|
name: 'Anrufe',
|
|
importance: Notifications.AndroidImportance.MAX,
|
|
vibrationPattern: [0, 500, 200, 500, 200, 500],
|
|
lightColor: '#007AFF',
|
|
sound: 'default',
|
|
lockscreenVisibility: Notifications.AndroidNotificationVisibility.PUBLIC,
|
|
});
|
|
}
|
|
|
|
// 3) Token holen
|
|
const projectId =
|
|
Constants.expoConfig?.extra?.eas?.projectId ??
|
|
Constants.easConfig?.projectId;
|
|
if (!projectId) {
|
|
console.warn('[push] EAS projectId missing — token cannot be issued');
|
|
return null;
|
|
}
|
|
const tokenData = await Notifications.getExpoPushTokenAsync({ projectId });
|
|
const token = tokenData.data;
|
|
if (!token) return null;
|
|
|
|
// 4) VoIP-Token parallel holen (iOS-only, no-op auf Android).
|
|
const voipToken = await fetchVoipToken();
|
|
|
|
// 5) Idempotenz-Skip: wenn beide Tokens unverändert, kein Re-POST.
|
|
if (
|
|
lastRegisteredToken.current === token &&
|
|
lastRegisteredVoipToken.current === voipToken
|
|
) {
|
|
return token;
|
|
}
|
|
|
|
// 6) Senden an Backend
|
|
const deviceId = await getDeviceId();
|
|
await apiFetch('/api/users/me/push-token', {
|
|
method: 'POST',
|
|
body: {
|
|
token,
|
|
platform: Platform.OS as 'ios' | 'android',
|
|
deviceId,
|
|
...(voipToken ? { voipToken } : {}),
|
|
},
|
|
});
|
|
|
|
lastRegisteredToken.current = token;
|
|
lastRegisteredVoipToken.current = voipToken;
|
|
if (__DEV__) {
|
|
console.log('[push] token registered:', token.slice(0, 30) + '…');
|
|
if (voipToken) console.log('[voip] token registered:', voipToken.slice(0, 30) + '…');
|
|
}
|
|
return token;
|
|
} catch (err) {
|
|
console.warn('[push] registration failed:', err);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function unregisterPushTokenFromBackend(): Promise<void> {
|
|
const token = lastRegisteredToken.current;
|
|
if (!token) return;
|
|
try {
|
|
await apiFetch(
|
|
`/api/users/me/push-token?token=${encodeURIComponent(token)}`,
|
|
{ method: 'DELETE' },
|
|
);
|
|
lastRegisteredToken.current = null;
|
|
} catch (err) {
|
|
console.warn('[push] unregister failed:', err);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Hook: registriert Push-Token sobald User authenticated ist.
|
|
* Verwendet in app/_layout.tsx nach Auth-Init.
|
|
*/
|
|
export function usePushTokenRegistration(userId: string | null | undefined) {
|
|
const lastUserId = useRef<string | null>(null);
|
|
useEffect(() => {
|
|
if (!userId || userId === lastUserId.current) return;
|
|
lastUserId.current = userId;
|
|
void registerPushTokenWithBackend();
|
|
}, [userId]);
|
|
}
|