chore: add push notif
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user