| 1 | import {
|
| 2 | View,
|
| 3 | Text,
|
| 4 | StyleSheet,
|
| 5 | ScrollView,
|
| 6 | KeyboardAvoidingView,
|
| 7 | Platform,
|
| 8 | Alert,
|
| 9 | } from "react-native";
|
| 10 |
|
| 11 | import { useState, useEffect } from "react";
|
| 12 | import * as Notifications from "expo-notifications";
|
| 13 | import { useAuth } from "@/context/AuthContext";
|
| 14 | import { useLocalSearchParams } from "expo-router";
|
| 15 | import { StatusBar } from "expo-status-bar";
|
| 16 | import { SafeAreaView } from "react-native-safe-area-context";
|
| 17 | import { useThemeColor } from "@/hooks/use-theme-color";
|
| 18 | import { Button } from "@/components/ui/Button";
|
| 19 | import { Colors, Spacing } from "@/constants/theme";
|
| 20 | import { Card } from "@/components/ui/Cards";
|
| 21 | import { Section, SectionHeader } from "@/components/ui/Sections";
|
| 22 |
|
| 23 | type NotificationParams = {
|
| 24 | title?: string;
|
| 25 | body?: string;
|
| 26 | notificationId?: string;
|
| 27 | };
|
| 28 |
|
| 29 | Notifications.setNotificationHandler({
|
| 30 | handleNotification: async () => ({
|
| 31 | shouldPlaySound: true,
|
| 32 | shouldSetBadge: false,
|
| 33 | shouldShowBanner: true,
|
| 34 | shouldShowList: true,
|
| 35 | }),
|
| 36 | });
|
| 37 |
|
| 38 | export default function NotificationsPage() {
|
| 39 | const { user } = useAuth();
|
| 40 | const params = useLocalSearchParams<NotificationParams>();
|
| 41 | const [expoPushToken, setExpoPushToken] = useState(user?.pushToken);
|
| 42 |
|
| 43 | useEffect(() => {
|
| 44 | if (user) {
|
| 45 | setExpoPushToken(user.pushToken);
|
| 46 | }
|
| 47 | }, [user]);
|
| 48 |
|
| 49 | async function sendPushNotification() {
|
| 50 | if (expoPushToken) {
|
| 51 | const message = {
|
| 52 | to: expoPushToken,
|
| 53 | sound: "default",
|
| 54 | title: "Oi meu querido",
|
| 55 | body: "Vamo escutar um The Doors",
|
| 56 | data: { screen: "notifications" },
|
| 57 | };
|
| 58 |
|
| 59 | await fetch("https://exp.host/--/api/v2/push/send", {
|
| 60 | method: "POST",
|
| 61 | headers: {
|
| 62 | Accept: "application/json",
|
| 63 | "Accept-encoding": "gzip, deflate",
|
| 64 | "Content-Type": "application/json",
|
| 65 | },
|
| 66 | body: JSON.stringify(message),
|
| 67 | });
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | return (
|
| 72 | <SafeAreaView style={styles.container}>
|
| 73 | <StatusBar style={"dark"} />
|
| 74 | <KeyboardAvoidingView behavior="height" style={styles.keyboardView}>
|
| 75 | <ScrollView
|
| 76 | contentContainerStyle={styles.scrollContainer}
|
| 77 | showsVerticalScrollIndicator={false}
|
| 78 | >
|
| 79 | <Section>
|
| 80 | <SectionHeader title="Recebidas" />
|
| 81 |
|
| 82 | <Card style={{ padding: Spacing.cardGap }}>
|
| 83 | <Text>Title: {params.title}</Text>
|
| 84 | <Text>Body: {params.body}</Text>
|
| 85 | <Text>
|
| 86 | Data:{" "}
|
| 87 | {params.notificationId && JSON.stringify(params.notificationId)}
|
| 88 | </Text>
|
| 89 | </Card>
|
| 90 | </Section>
|
| 91 |
|
| 92 | <Button title="Quer ver uma coisa?" onPress={sendPushNotification} />
|
| 93 | </ScrollView>
|
| 94 | </KeyboardAvoidingView>
|
| 95 | </SafeAreaView>
|
| 96 | );
|
| 97 | }
|
| 98 |
|
| 99 | const styles = StyleSheet.create({
|
| 100 | container: {
|
| 101 | flex: 1,
|
| 102 | backgroundColor: Colors.light.background,
|
| 103 | },
|
| 104 | keyboardView: {
|
| 105 | flex: 1,
|
| 106 | },
|
| 107 | scrollContainer: {
|
| 108 | flexGrow: 1,
|
| 109 | paddingHorizontal: 24,
|
| 110 | justifyContent: "start",
|
| 111 | minHeight: "100%",
|
| 112 | },
|
| 113 | header: {
|
| 114 | alignItems: "left",
|
| 115 | marginBottom: 40,
|
| 116 | },
|
| 117 | });
|