From 9ce621bee5070f01bffe61f2aa39f27754037b62 Mon Sep 17 00:00:00 2001 From: Xor290 Date: Mon, 2 Mar 2026 15:36:55 +0100 Subject: [PATCH] chore: fix notification client --- .../src/services/pushNotifications.ts | 1 - mobile/app.json | 3 +-- mobile/src/auth/AuthContext.tsx | 21 ++++++++++++++++++- mobile/src/navigation/ClientNavigator.tsx | 3 --- mobile/src/services/pushNotifications.ts | 9 ++------ 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/frontend-admin/src/services/pushNotifications.ts b/frontend-admin/src/services/pushNotifications.ts index 160c529a..a944fa2f 100644 --- a/frontend-admin/src/services/pushNotifications.ts +++ b/frontend-admin/src/services/pushNotifications.ts @@ -9,7 +9,6 @@ const LIVREUR_API = "https://uber-stup.club/api/v1/livreur"; // Configuration du comportement des notifications en foreground Notifications.setNotificationHandler({ handleNotification: async () => ({ - shouldShowAlert: true, shouldPlaySound: true, shouldSetBadge: true, shouldShowBanner: true, diff --git a/mobile/app.json b/mobile/app.json index f63607f0..71c3d89c 100644 --- a/mobile/app.json +++ b/mobile/app.json @@ -28,8 +28,7 @@ "permissions": [ "android.permission.RECEIVE_BOOT_COMPLETED", "android.permission.VIBRATE", - "android.permission.RECEIVE_BOOT_COMPLETED", - "android.permission.VIBRATE" + "android.permission.POST_NOTIFICATIONS" ] }, "web": { diff --git a/mobile/src/auth/AuthContext.tsx b/mobile/src/auth/AuthContext.tsx index a6ad057e..5aa0d2c4 100644 --- a/mobile/src/auth/AuthContext.tsx +++ b/mobile/src/auth/AuthContext.tsx @@ -1,4 +1,4 @@ -import React, { createContext, useContext, useState, useEffect, type ReactNode } from 'react'; +import React, { createContext, useContext, useState, useEffect, useRef, type ReactNode } from 'react'; import { getToken, setToken as storeToken, removeToken, getAdminToken, setAdminToken as storeAdminToken, removeAdminToken, @@ -8,6 +8,12 @@ import { clearAllAuth, } from './tokenStorage'; import { extractUsernameFromToken, extractRoleFromToken, isTokenExpired } from './jwtUtils'; +import { + setupNotificationChannel, + registerForPushNotificationsAsync, + sendPushTokenToBackend, + removePushTokenFromBackend, +} from '../services/pushNotifications'; export type UserRole = 'client' | 'admin' | 'cabine' | 'livreur' | null; @@ -30,6 +36,7 @@ interface AuthContextType extends AuthState { const AuthContext = createContext(undefined); export function AuthProvider({ children }: { children: ReactNode }) { + const pushTokenRef = useRef(null); const [state, setState] = useState({ token: null, username: null, @@ -114,6 +121,15 @@ export function AuthProvider({ children }: { children: ReactNode }) { isAuthenticated: true, mustChangePassword, }); + // Enregistrer le push token en arrière-plan + setupNotificationChannel().then(() => + registerForPushNotificationsAsync() + ).then((pushToken) => { + if (pushToken) { + pushTokenRef.current = pushToken; + sendPushTokenToBackend(pushToken); + } + }).catch(() => {/* silencieux */}); }; const setMustChangePassword = (value: boolean) => { @@ -136,6 +152,9 @@ export function AuthProvider({ children }: { children: ReactNode }) { }; const logout = async () => { + // Supprimer le push token du backend avant de se déconnecter + try { await removePushTokenFromBackend(); } catch { /* silencieux */ } + pushTokenRef.current = null; await clearAllAuth(); setState({ token: null, diff --git a/mobile/src/navigation/ClientNavigator.tsx b/mobile/src/navigation/ClientNavigator.tsx index ca7d79ef..235b9b6d 100644 --- a/mobile/src/navigation/ClientNavigator.tsx +++ b/mobile/src/navigation/ClientNavigator.tsx @@ -76,9 +76,6 @@ function ClientTabs() { }, [navigateToOrder, navigation, clearNavigateToOrder]); const handleLogout = async () => { - if (pushToken) { - await removePushTokenFromBackend(pushToken); - } await logoutUser(); await logout(); }; diff --git a/mobile/src/services/pushNotifications.ts b/mobile/src/services/pushNotifications.ts index 78109bd4..7d45067c 100644 --- a/mobile/src/services/pushNotifications.ts +++ b/mobile/src/services/pushNotifications.ts @@ -9,7 +9,6 @@ const V1 = "https://uber-stup.club/api/v1"; // Configuration du comportement des notifications en foreground Notifications.setNotificationHandler({ handleNotification: async () => ({ - shouldShowAlert: true, shouldPlaySound: true, shouldSetBadge: true, shouldShowBanner: true, @@ -98,13 +97,9 @@ export async function sendPushTokenToBackend( } // Supprimer le push token du backend (au logout) -export async function removePushTokenFromBackend( - pushToken: string, -): Promise { +export async function removePushTokenFromBackend(): Promise { try { - await apiClient.delete(`${V1}/push-token`, { - data: { push_token: pushToken }, - }); + await apiClient.delete(`${V1}/push-token`); console.log("✅ Push token supprimé du backend"); } catch (error) { console.error("❌ Erreur suppression push token:", error);