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
+16
View File
@@ -777,6 +777,22 @@ export const getPublicSettings = async (): Promise<PublicSettings> => {
}
};
export const registerPushToken = async (token: string): Promise<void> => {
try {
await apiClient.post(`${V1}/push-token`, { token });
} catch {
// silencieux
}
};
export const unregisterPushToken = async (): Promise<void> => {
try {
await apiClient.delete(`${V1}/push-token`);
} catch {
// silencieux
}
};
export const calculateOrderTotal = (order: any): number => {
if (typeof order.total === "number" && order.total > 0) return order.total;
if (typeof order.total_prix === "number" && order.total_prix > 0)
@@ -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();
+63
View File
@@ -0,0 +1,63 @@
import * as Notifications from "expo-notifications";
import * as Device from "expo-device";
import Constants from "expo-constants";
import { Platform, Alert } from "react-native";
import { registerPushToken } from "../api/api";
// Comportement des notifs reçues en foreground
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldPlaySound: true,
shouldSetBadge: true,
shouldShowBanner: true,
shouldShowList: true,
}),
});
function handleRegistrationError(errorMessage: string) {
console.error("[PUSH]", errorMessage);
Alert.alert("Push Notification Error", errorMessage);
}
export async function registerForPushNotifications(): Promise<string | undefined> {
if (Platform.OS === "android") {
await Notifications.setNotificationChannelAsync("default", {
name: "default",
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
}
if (Device.isDevice) {
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== "granted") {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== "granted") {
handleRegistrationError("Permission not granted to get push token for push notification!");
return;
}
const projectId =
Constants?.expoConfig?.extra?.eas?.projectId ??
Constants?.easConfig?.projectId;
if (!projectId) {
handleRegistrationError("Project ID not found");
return;
}
try {
const pushTokenString = (
await Notifications.getExpoPushTokenAsync({ projectId })
).data;
console.log("[PUSH] Token obtenu:", pushTokenString);
await registerPushToken(pushTokenString);
return pushTokenString;
} catch (e: unknown) {
handleRegistrationError(`${e}`);
}
} else {
handleRegistrationError("Must use physical device for push notifications");
}
}