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]);
const handleUserSelected = async (selectedUser: User) => {
setUser(selectedUser);
console.log('User selected:', selectedUser); // Debug log
try {
await AsyncStorage.setItem('@user', JSON.stringify(selectedUser));
// Store the Push Token to your server when the user is selected (optional)
if (expoPushToken) {
await savePushToken(selectedUser.id, expoPushToken); // Hypothetical function to send token to server
}
console.log('User data stored in AsyncStorage:', selectedUser); // Debug log
setUser(selectedUser);
} catch (e) {
console.error('Failed to save user or push token', e);
}
@ -89,36 +86,38 @@ export default function RootLayout() {
useEffect(() => {
// Register for push notifications and set the push token
registerForPushNotificationsAsync().then(async (token) => {
if (token) {
setExpoPushToken(token);
// Upload push token to backend when successfully received
const storedUser = await AsyncStorage.getItem('@user');
if (!storedUser) {
console.log('No user found in AsyncStorage'); // Debug log
return;
}
try {
const storedUser = await AsyncStorage.getItem('@user');
console.log('Stored user data:', storedUser); // Debug log
if (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
await fetch(`${BASE_URL}/api/updatePushToken`, {
// Send the push token to your Next.js API
await fetch(`${BASE_URL}/updatePushToken`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
apiKey: API_KEY, // Use the API key stored in the environment
userId: user.id, // The logged-in user's ID
pushToken: token, // The Expo push token collected
apiKey: API_KEY,
userId: user.id,
pushToken: token,
}),
});
console.log('Push token successfully sent to backend');
console.log('Push token update request sent with user ID:', user.id); // Debug log
} else {
console.log('No user found in AsyncStorage');
}
} catch (error) {
console.error('Failed to send push token to backend', error);
}
}
});
// Listener for received notifications while the app is running
@ -215,10 +214,3 @@ async function registerForPushNotificationsAsync() {
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-linear-gradient": "~13.0.2",
"expo-linking": "~6.3.1",
"expo-notifications": "~0.28.16",
"expo-notifications": "~0.28.17",
"expo-router": "~3.5.23",
"expo-splash-screen": "~0.27.5",
"expo-status-bar": "~1.12.1",
@ -10354,9 +10354,9 @@
}
},
"node_modules/expo-notifications": {
"version": "0.28.16",
"resolved": "https://registry.npmjs.org/expo-notifications/-/expo-notifications-0.28.16.tgz",
"integrity": "sha512-sj4oDip+uFNmxieGHkfS2Usrwbw2jfOTfQ22a7z5tdSo/vD6jWMlCHOnJifqYLjPxyqf9SLTsQWO3bmk7MY2Yg==",
"version": "0.28.17",
"resolved": "https://registry.npmjs.org/expo-notifications/-/expo-notifications-0.28.17.tgz",
"integrity": "sha512-tuhc/X385O1gLSBEsPpXSqmmBK6Ve6zG8u6YFa1kXILbyy83DHJuHB5ELJKo/HZdstlYeFjkDCei4haOuxCLCQ==",
"license": "MIT",
"dependencies": {
"@expo/image-utils": "^0.5.0",

View File

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