eletrotupi / tcc/ frontend/app/notifications.tsxea1475564abe0f26ea6bb9c6c840b714261aad91
3.3 KB | 118 lines Raw
1
import {
2
  StyleSheet,
3
  ScrollView,
4
  KeyboardAvoidingView,
5
} from "react-native";
6
import { Text } from "@/components/ui/Text";
7

8
import { useState, useEffect } from "react";
9
import * as Notifications from "expo-notifications";
10
import { useAuth } from "@/context/AuthContext";
11
import { useLocalSearchParams } from "expo-router";
12
import { StatusBar } from "expo-status-bar";
13
import { SafeAreaView } from "react-native-safe-area-context";
14
import { Button } from "@/components/ui/Button";
15
import { Colors, Spacing } from "@/constants/theme";
16
import { Card } from "@/components/ui/Cards";
17
import { Section, SectionHeader } from "@/components/ui/Sections";
18

19
type NotificationParams = {
20
  title?: string;
21
  body?: string;
22
  notificationId?: string;
23
};
24

25
type ReceivedNotification = {
26
  title?: string;
27
  body?: string;
28
};
29

30
export default function NotificationsPage() {
31
  const { user } = useAuth();
32
  const params = useLocalSearchParams<NotificationParams>();
33
  const expoPushToken = user?.pushToken;
34
  const [liveNotification, setLiveNotification] = useState<ReceivedNotification | null>(null);
35

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

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

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

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

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

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

93
          <Button title="Quer ver uma coisa?" onPress={sendPushNotification} />
94
        </ScrollView>
95
      </KeyboardAvoidingView>
96
    </SafeAreaView>
97
  );
98
}
99

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