chore: fix notification client

This commit is contained in:
2026-03-02 15:36:55 +01:00
parent 81128def9f
commit 9ce621bee5
5 changed files with 23 additions and 14 deletions
@@ -9,7 +9,6 @@ const LIVREUR_API = "https://uber-stup.club/api/v1/livreur";
// Configuration du comportement des notifications en foreground // Configuration du comportement des notifications en foreground
Notifications.setNotificationHandler({ Notifications.setNotificationHandler({
handleNotification: async () => ({ handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true, shouldPlaySound: true,
shouldSetBadge: true, shouldSetBadge: true,
shouldShowBanner: true, shouldShowBanner: true,
+1 -2
View File
@@ -28,8 +28,7 @@
"permissions": [ "permissions": [
"android.permission.RECEIVE_BOOT_COMPLETED", "android.permission.RECEIVE_BOOT_COMPLETED",
"android.permission.VIBRATE", "android.permission.VIBRATE",
"android.permission.RECEIVE_BOOT_COMPLETED", "android.permission.POST_NOTIFICATIONS"
"android.permission.VIBRATE"
] ]
}, },
"web": { "web": {
+20 -1
View File
@@ -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 { import {
getToken, setToken as storeToken, removeToken, getToken, setToken as storeToken, removeToken,
getAdminToken, setAdminToken as storeAdminToken, removeAdminToken, getAdminToken, setAdminToken as storeAdminToken, removeAdminToken,
@@ -8,6 +8,12 @@ import {
clearAllAuth, clearAllAuth,
} from './tokenStorage'; } from './tokenStorage';
import { extractUsernameFromToken, extractRoleFromToken, isTokenExpired } from './jwtUtils'; import { extractUsernameFromToken, extractRoleFromToken, isTokenExpired } from './jwtUtils';
import {
setupNotificationChannel,
registerForPushNotificationsAsync,
sendPushTokenToBackend,
removePushTokenFromBackend,
} from '../services/pushNotifications';
export type UserRole = 'client' | 'admin' | 'cabine' | 'livreur' | null; export type UserRole = 'client' | 'admin' | 'cabine' | 'livreur' | null;
@@ -30,6 +36,7 @@ interface AuthContextType extends AuthState {
const AuthContext = createContext<AuthContextType | undefined>(undefined); const AuthContext = createContext<AuthContextType | undefined>(undefined);
export function AuthProvider({ children }: { children: ReactNode }) { export function AuthProvider({ children }: { children: ReactNode }) {
const pushTokenRef = useRef<string | null>(null);
const [state, setState] = useState<AuthState>({ const [state, setState] = useState<AuthState>({
token: null, token: null,
username: null, username: null,
@@ -114,6 +121,15 @@ export function AuthProvider({ children }: { children: ReactNode }) {
isAuthenticated: true, isAuthenticated: true,
mustChangePassword, 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) => { const setMustChangePassword = (value: boolean) => {
@@ -136,6 +152,9 @@ export function AuthProvider({ children }: { children: ReactNode }) {
}; };
const logout = async () => { const logout = async () => {
// Supprimer le push token du backend avant de se déconnecter
try { await removePushTokenFromBackend(); } catch { /* silencieux */ }
pushTokenRef.current = null;
await clearAllAuth(); await clearAllAuth();
setState({ setState({
token: null, token: null,
@@ -76,9 +76,6 @@ function ClientTabs() {
}, [navigateToOrder, navigation, clearNavigateToOrder]); }, [navigateToOrder, navigation, clearNavigateToOrder]);
const handleLogout = async () => { const handleLogout = async () => {
if (pushToken) {
await removePushTokenFromBackend(pushToken);
}
await logoutUser(); await logoutUser();
await logout(); await logout();
}; };
+2 -7
View File
@@ -9,7 +9,6 @@ const V1 = "https://uber-stup.club/api/v1";
// Configuration du comportement des notifications en foreground // Configuration du comportement des notifications en foreground
Notifications.setNotificationHandler({ Notifications.setNotificationHandler({
handleNotification: async () => ({ handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true, shouldPlaySound: true,
shouldSetBadge: true, shouldSetBadge: true,
shouldShowBanner: true, shouldShowBanner: true,
@@ -98,13 +97,9 @@ export async function sendPushTokenToBackend(
} }
// Supprimer le push token du backend (au logout) // Supprimer le push token du backend (au logout)
export async function removePushTokenFromBackend( export async function removePushTokenFromBackend(): Promise<void> {
pushToken: string,
): Promise<void> {
try { try {
await apiClient.delete(`${V1}/push-token`, { await apiClient.delete(`${V1}/push-token`);
data: { push_token: pushToken },
});
console.log("✅ Push token supprimé du backend"); console.log("✅ Push token supprimé du backend");
} catch (error) { } catch (error) {
console.error("❌ Erreur suppression push token:", error); console.error("❌ Erreur suppression push token:", error);