chore: add push notif

This commit is contained in:
2026-03-14 18:28:02 +01:00
parent 0ae1529353
commit b1a41c4a16
5 changed files with 145 additions and 3 deletions
@@ -7,9 +7,11 @@ import React, {
useCallback,
type ReactNode,
} from "react";
import * as Notifications from "expo-notifications";
import { getClientNotifications, markNotificationsRead } from "../api/api";
import type { ClientNotification } from "../api/api";
import { getToken } from "../auth/tokenStorage";
import { registerForPushNotifications } from "../services/pushNotifications";
interface ToastData {
message: string;
@@ -128,6 +130,30 @@ export function NotificationProvider({ children }: { children: ReactNode }) {
}
}, []);
// Enregistrement push token + listeners (pattern doc Expo)
useEffect(() => {
registerForPushNotifications().catch(console.error);
const notificationListener = Notifications.addNotificationReceivedListener((notif) => {
const body = notif.request.content.body ?? "";
const data = notif.request.content.data as any;
showToast(body, getToastType(data?.type ?? ""));
fetchNotifications();
});
const responseListener = Notifications.addNotificationResponseReceivedListener((response) => {
const data = response.notification.request.content.data as any;
if (data?.command_id) {
setNavigateToOrder(data.command_id);
}
});
return () => {
notificationListener.remove();
responseListener.remove();
};
}, [fetchNotifications, showToast]);
// Polling toutes les 15 secondes
useEffect(() => {
fetchNotifications();