-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
119 lines (93 loc) · 3.35 KB
/
Copy pathApp.js
File metadata and controls
119 lines (93 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import 'react-native-gesture-handler'; // Doit être au tout début
import { NavigationContainer } from '@react-navigation/native';
import React, { useEffect, useState, useRef } from 'react';
import * as Notifications from "expo-notifications";
import * as Device from "expo-device";
import Constants from 'expo-constants';
import Navbar from './screens/Navbar';
import axios from 'axios';
import { Alert } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { checkForUpdates } from './screens/components/update';
// Gérer les notifications en arrière-plan
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true, // 👈 Permet d'afficher la notification
shouldPlaySound: true,
shouldSetBadge: false,
}),
});
async function registerForPushNotificationsAsync() {
let token;
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') {
console.log('Permission refusée pour les notifications !');
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
console.log("Token Expo :", token);
} else {
console.log('Les notifications push ne fonctionnent pas sur un émulateur.');
}
return token;
}
export default function App() {
const [expoPushToken, setExpoPushToken] = useState('');
const [userId, setUserId] = useState(''); // Utilisateur connecté (ID)
const currentVersion = `v${Constants.expoConfig.version}`;
const notificationListener = useRef();
const responseListener = useRef();
// Fonction pour récupérer l'ID utilisateur depuis AsyncStorage
const getId = async () => {
try {
const value = await AsyncStorage.getItem('id');
if (value) {
setUserId(value); // Stocker l'ID utilisateur dans le state
}
} catch (error) {
console.error('Erreur de récupération de l\'ID utilisateur:', error);
}
};
useEffect(() => {
checkForUpdates(currentVersion);
getId();
registerForPushNotificationsAsync().then(token => {
if (token) {
setExpoPushToken(token);
if (userId) {
axios.post('https://tall-debonair-danger.glitch.me/store-token', {
token,
userId,
})
.then(response => {
console.log('Token enregistré avec succès:', response.data);
})
.catch(error => {
console.error('Erreur lors de l\'enregistrement du token:', error);
});
}
}
});
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
console.log("Notification reçue :", notification);
});
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
console.log("Notification cliquée :", response);
});
return () => {
Notifications.removeNotificationSubscription(notificationListener.current);
Notifications.removeNotificationSubscription(responseListener.current);
};
}, [userId]);
return (
<NavigationContainer>
<Navbar />
</NavigationContainer>
);
}