eletrotupi / tcc/ frontend/app/notifications.tsx409c3751b50fc119fcd4ef0ef0b88531565cfd95
3.4 KB | 128 lines Raw
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
type ReceivedNotification = {
30
  title?: string;
31
  body?: string;
32
};
33

34
export default function NotificationsPage() {
35
  const { user } = useAuth();
36
  const params = useLocalSearchParams<NotificationParams>();
37
  const [expoPushToken, setExpoPushToken] = useState(user?.pushToken);
38
  const [liveNotification, setLiveNotification] = useState<ReceivedNotification | null>(null);
39

40
  useEffect(() => {
41
    if (user) {
42
      setExpoPushToken(user.pushToken);
43
    }
44
  }, [user]);
45

46
  useEffect(() => {
47
    const sub = Notifications.addNotificationReceivedListener((n) => {
48
      setLiveNotification({
49
        title: n.request.content.title ?? undefined,
50
        body: n.request.content.body ?? undefined,
51
      });
52
    });
53
    return () => sub.remove();
54
  }, []);
55

56
  const displayed = liveNotification ?? (params.title || params.body ? { title: params.title, body: params.body } : null);
57

58
  async function sendPushNotification() {
59
    if (expoPushToken) {
60
      const message = {
61
        to: expoPushToken,
62
        sound: "default",
63
        title: "Oi meu querido",
64
        body: "Vamo escutar um The Doors",
65
        data: { screen: "notifications" },
66
      };
67

68
      await fetch("https://exp.host/--/api/v2/push/send", {
69
        method: "POST",
70
        headers: {
71
          Accept: "application/json",
72
          "Accept-encoding": "gzip, deflate",
73
          "Content-Type": "application/json",
74
        },
75
        body: JSON.stringify(message),
76
      });
77
    }
78
  }
79

80
  return (
81
    <SafeAreaView style={styles.container}>
82
      <StatusBar style={"dark"} />
83
      <KeyboardAvoidingView behavior="height" style={styles.keyboardView}>
84
        <ScrollView
85
          contentContainerStyle={styles.scrollContainer}
86
          showsVerticalScrollIndicator={false}
87
        >
88
          <Section>
89
            <SectionHeader title="Recebidas" />
90

91
            {displayed ? (
92
              <Card style={{ padding: Spacing.cardGap }}>
93
                <Text>Title: {displayed.title}</Text>
94
                <Text>Body: {displayed.body}</Text>
95
              </Card>
96
            ) : (
97
              <Card style={{ padding: Spacing.cardGap }}>
98
                <Text>Nenhuma notificação recebida.</Text>
99
              </Card>
100
            )}
101
          </Section>
102

103
          <Button title="Quer ver uma coisa?" onPress={sendPushNotification} />
104
        </ScrollView>
105
      </KeyboardAvoidingView>
106
    </SafeAreaView>
107
  );
108
}
109

110
const styles = StyleSheet.create({
111
  container: {
112
    flex: 1,
113
    backgroundColor: Colors.light.background,
114
  },
115
  keyboardView: {
116
    flex: 1,
117
  },
118
  scrollContainer: {
119
    flexGrow: 1,
120
    paddingHorizontal: 24,
121
    justifyContent: "start",
122
    minHeight: "100%",
123
  },
124
  header: {
125
    alignItems: "left",
126
    marginBottom: 40,
127
  },
128
});