Wavelength/app/(tabs)/index.tsx

109 lines
2.8 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from "react";
import { StyleSheet, Alert } from "react-native";
2024-10-08 16:58:50 -05:00
import { ThemedText } from "@/components/ThemedText";
import { ThemedView } from "@/components/ThemedView";
2024-10-08 19:40:36 -05:00
import { Colors } from '@/constants/Colors';
import { useColorScheme } from '@/hooks/useColorScheme';
import FontAwesome from "@expo/vector-icons/FontAwesome";
2024-10-08 16:58:50 -05:00
import Button from "@/components/buttons/Button";
import { getUserData } from "@/components/services/securestorage/UserData";
2024-10-08 16:58:50 -05:00
const Index = () => {
2024-10-08 19:40:36 -05:00
const scheme = useColorScheme() ?? 'light';
const [pushToken, setPushToken] = useState<string | null>(null);
useEffect(() => {
const fetchUserData = async () => {
const userData = await getUserData();
if (userData) {
setPushToken(userData.pushToken);
}
};
fetchUserData();
}, []);
const sendPushNotification = async () => {
if (!pushToken) {
Alert.alert('Error', 'Push token not available');
return;
}
const message = {
to: pushToken,
sound: 'default',
title: 'Hey Baby!',
body: 'Are you ready for push notifications?!?',
data: {
someData: 'goes here'
},
};
try {
const response = await fetch(`https://exp.host/--/api/v2/push/send`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Accept-encoding': 'gzip, deflate',
'Content-Type': 'application/json',
},
body: JSON.stringify(message),
});
const result = await response.json();
console.log('Result:', result);
Alert.alert('Success', 'Push notification sent successfully');
} catch (error) {
console.error('Error sending push notification:', error);
Alert.alert('Error', 'Failed to send push notification');
}
};
return (
2024-10-08 16:58:50 -05:00
<ThemedView style={styles.container}>
<ThemedText style={styles.text}>
Home Screen
</ThemedText>
<ThemedView style={styles.footerContainer}>
2024-10-08 19:40:36 -05:00
<Button width={220} height={60}
onPress={sendPushNotification}
2024-10-08 19:40:36 -05:00
>
<FontAwesome name="bell" size={18}
color={Colors[scheme].background} style={styles.buttonIcon}
2024-10-08 19:40:36 -05:00
/>
<ThemedText
style={[
styles.buttonLabel,
{color: Colors[scheme].background}
]}
>
Send Push Notification
2024-10-08 19:40:36 -05:00
</ThemedText>
</Button>
</ThemedView>
2024-10-08 16:58:50 -05:00
</ThemedView>
);
}
2024-10-08 16:58:50 -05:00
export default Index;
const styles = StyleSheet.create({
2024-10-08 16:58:50 -05:00
container: {
flex: 1,
alignItems: 'center',
2024-10-08 16:58:50 -05:00
justifyContent: 'center',
},
2024-10-08 16:58:50 -05:00
text: {
fontSize: 24,
},
2024-10-08 16:58:50 -05:00
footerContainer: {
flex: 1 / 3,
alignItems: 'center',
},
2024-10-08 19:40:36 -05:00
buttonLabel: {
fontSize: 16,
},
buttonIcon: {
paddingRight: 8,
},
});