eletrotupi / tcc/ frontend/app/notifications.tsxbb83acf206c8fc02747310d3c1fff53cd3adff61
3.4 KB | 122 lines Raw
1
import {
2
  View,
3
  StyleSheet,
4
  ScrollView,
5
  KeyboardAvoidingView,
6
  Platform,
7
  Alert,
8
} from "react-native";
9
import { Text } from "@/components/ui/Text";
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 = user?.pushToken;
38
  const [liveNotification, setLiveNotification] = useState<ReceivedNotification | null>(null);
39

40
  useEffect(() => {
41
    const sub = Notifications.addNotificationReceivedListener((n) => {
42
      setLiveNotification({
43
        title: n.request.content.title ?? undefined,
44
        body: n.request.content.body ?? undefined,
45
      });
46
    });
47
    return () => sub.remove();
48
  }, []);
49

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

52
  async function sendPushNotification() {
53
    if (expoPushToken) {
54
      const message = {
55
        to: expoPushToken,
56
        sound: "default",
57
        title: "Oi meu querido",
58
        body: "Vamo escutar um The Doors",
59
        data: { screen: "notifications" },
60
      };
61

62
      await fetch("https://exp.host/--/api/v2/push/send", {
63
        method: "POST",
64
        headers: {
65
          Accept: "application/json",
66
          "Accept-encoding": "gzip, deflate",
67
          "Content-Type": "application/json",
68
        },
69
        body: JSON.stringify(message),
70
      });
71
    }
72
  }
73

74
  return (
75
    <SafeAreaView style={styles.container}>
76
      <StatusBar style={"dark"} />
77
      <KeyboardAvoidingView behavior="height" style={styles.keyboardView}>
78
        <ScrollView
79
          contentContainerStyle={styles.scrollContainer}
80
          showsVerticalScrollIndicator={false}
81
        >
82
          <Section>
83
            <SectionHeader title="Recebidas" />
84

85
            {displayed ? (
86
              <Card style={{ padding: Spacing.cardGap }}>
87
                <Text>Title: {displayed.title}</Text>
88
                <Text>Body: {displayed.body}</Text>
89
              </Card>
90
            ) : (
91
              <Card style={{ padding: Spacing.cardGap }}>
92
                <Text>Nenhuma notificação recebida.</Text>
93
              </Card>
94
            )}
95
          </Section>
96

97
          <Button title="Quer ver uma coisa?" onPress={sendPushNotification} />
98
        </ScrollView>
99
      </KeyboardAvoidingView>
100
    </SafeAreaView>
101
  );
102
}
103

104
const styles = StyleSheet.create({
105
  container: {
106
    flex: 1,
107
    backgroundColor: Colors.light.background,
108
  },
109
  keyboardView: {
110
    flex: 1,
111
  },
112
  scrollContainer: {
113
    flexGrow: 1,
114
    paddingHorizontal: 24,
115
    justifyContent: "flex-start",
116
    minHeight: "100%",
117
  },
118
  header: {
119
    alignItems: "flex-start",
120
    marginBottom: 40,
121
  },
122
});