Compare commits

...

2 Commits

Author SHA1 Message Date
a01ac077fe I fucking hate Nextcloud right now 2024-09-25 11:53:19 -05:00
d95064cb12 updates 2024-09-25 09:06:57 -05:00
3 changed files with 36 additions and 44 deletions

View File

@ -62,14 +62,11 @@ export default function RootLayout() {
}, [loaded]); }, [loaded]);
const handleUserSelected = async (selectedUser: User) => { const handleUserSelected = async (selectedUser: User) => {
setUser(selectedUser); console.log('User selected:', selectedUser); // Debug log
try { try {
await AsyncStorage.setItem('@user', JSON.stringify(selectedUser)); await AsyncStorage.setItem('@user', JSON.stringify(selectedUser));
console.log('User data stored in AsyncStorage:', selectedUser); // Debug log
// Store the Push Token to your server when the user is selected (optional) setUser(selectedUser);
if (expoPushToken) {
await savePushToken(selectedUser.id, expoPushToken); // Hypothetical function to send token to server
}
} catch (e) { } catch (e) {
console.error('Failed to save user or push token', e); console.error('Failed to save user or push token', e);
} }
@ -89,36 +86,38 @@ export default function RootLayout() {
useEffect(() => { useEffect(() => {
// Register for push notifications and set the push token // Register for push notifications and set the push token
registerForPushNotificationsAsync().then(async (token) => { registerForPushNotificationsAsync().then(async (token) => {
if (token) { const storedUser = await AsyncStorage.getItem('@user');
setExpoPushToken(token); if (!storedUser) {
console.log('No user found in AsyncStorage'); // Debug log
// Upload push token to backend when successfully received return;
}
try { try {
const storedUser = await AsyncStorage.getItem('@user'); const storedUser = await AsyncStorage.getItem('@user');
console.log('Stored user data:', storedUser); // Debug log
if (storedUser) { if (storedUser) {
const user = JSON.parse(storedUser); const user = JSON.parse(storedUser);
console.log('Parsed user data:', user); // Debug log
// Send the push token to your Next.js API, along with the user's ID // Send the push token to your Next.js API
await fetch(`${BASE_URL}/api/updatePushToken`, { await fetch(`${BASE_URL}/updatePushToken`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ body: JSON.stringify({
apiKey: API_KEY, // Use the API key stored in the environment apiKey: API_KEY,
userId: user.id, // The logged-in user's ID userId: user.id,
pushToken: token, // The Expo push token collected pushToken: token,
}), }),
}); });
console.log('Push token update request sent with user ID:', user.id); // Debug log
console.log('Push token successfully sent to backend');
} else { } else {
console.log('No user found in AsyncStorage'); console.log('No user found in AsyncStorage');
} }
} catch (error) { } catch (error) {
console.error('Failed to send push token to backend', error); console.error('Failed to send push token to backend', error);
} }
}
}); });
// Listener for received notifications while the app is running // Listener for received notifications while the app is running
@ -215,10 +214,3 @@ async function registerForPushNotificationsAsync() {
return token; return token;
} }
// (Optional) A function to store push tokens in your backend server
async function savePushToken(userId: number, expoPushToken: string) {
// You would need to implement this function to save the user's push token to your backend.
// For example, you could send a POST request to your Next.js API that stores expoPushToken
console.log('Saving push token for user:', userId, 'with token:', expoPushToken);
}

8
package-lock.json generated
View File

@ -20,7 +20,7 @@
"expo-font": "~12.0.9", "expo-font": "~12.0.9",
"expo-linear-gradient": "~13.0.2", "expo-linear-gradient": "~13.0.2",
"expo-linking": "~6.3.1", "expo-linking": "~6.3.1",
"expo-notifications": "~0.28.16", "expo-notifications": "~0.28.17",
"expo-router": "~3.5.23", "expo-router": "~3.5.23",
"expo-splash-screen": "~0.27.5", "expo-splash-screen": "~0.27.5",
"expo-status-bar": "~1.12.1", "expo-status-bar": "~1.12.1",
@ -10354,9 +10354,9 @@
} }
}, },
"node_modules/expo-notifications": { "node_modules/expo-notifications": {
"version": "0.28.16", "version": "0.28.17",
"resolved": "https://registry.npmjs.org/expo-notifications/-/expo-notifications-0.28.16.tgz", "resolved": "https://registry.npmjs.org/expo-notifications/-/expo-notifications-0.28.17.tgz",
"integrity": "sha512-sj4oDip+uFNmxieGHkfS2Usrwbw2jfOTfQ22a7z5tdSo/vD6jWMlCHOnJifqYLjPxyqf9SLTsQWO3bmk7MY2Yg==", "integrity": "sha512-tuhc/X385O1gLSBEsPpXSqmmBK6Ve6zG8u6YFa1kXILbyy83DHJuHB5ELJKo/HZdstlYeFjkDCei4haOuxCLCQ==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@expo/image-utils": "^0.5.0", "@expo/image-utils": "^0.5.0",

View File

@ -41,7 +41,7 @@
"react-native-vector-icons": "^10.1.0", "react-native-vector-icons": "^10.1.0",
"react-native-web": "~0.19.10", "react-native-web": "~0.19.10",
"expo-dev-client": "~4.0.26", "expo-dev-client": "~4.0.26",
"expo-notifications": "~0.28.16", "expo-notifications": "~0.28.17",
"expo-device": "~6.0.2" "expo-device": "~6.0.2"
}, },
"devDependencies": { "devDependencies": {