| 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 { useLocalSearchParams } from "expo-router";
|
| 11 | import { StatusBar } from "expo-status-bar";
|
| 12 | import { SafeAreaView } from "react-native-safe-area-context";
|
| 13 | import { Colors, Spacing } from "@/constants/theme";
|
| 14 | import { Card } from "@/components/ui/Cards";
|
| 15 | import { Section, SectionHeader } from "@/components/ui/Sections";
|
| 16 |
|
| 17 | type NotificationParams = {
|
| 18 | title?: string;
|
| 19 | body?: string;
|
| 20 | notificationId?: string;
|
| 21 | };
|
| 22 |
|
| 23 | type ReceivedNotification = {
|
| 24 | title?: string;
|
| 25 | body?: string;
|
| 26 | };
|
| 27 |
|
| 28 | export default function NotificationsPage() {
|
| 29 | const params = useLocalSearchParams<NotificationParams>();
|
| 30 | const [liveNotification, setLiveNotification] = useState<ReceivedNotification | null>(null);
|
| 31 |
|
| 32 | useEffect(() => {
|
| 33 | const sub = Notifications.addNotificationReceivedListener((n) => {
|
| 34 | setLiveNotification({
|
| 35 | title: n.request.content.title ?? undefined,
|
| 36 | body: n.request.content.body ?? undefined,
|
| 37 | });
|
| 38 | });
|
| 39 | return () => sub.remove();
|
| 40 | }, []);
|
| 41 |
|
| 42 | const displayed = liveNotification ?? (params.title || params.body ? { title: params.title, body: params.body } : null);
|
| 43 |
|
| 44 | return (
|
| 45 | <SafeAreaView style={styles.container}>
|
| 46 | <StatusBar style={"dark"} />
|
| 47 | <KeyboardAvoidingView behavior="height" style={styles.keyboardView}>
|
| 48 | <ScrollView
|
| 49 | contentContainerStyle={styles.scrollContainer}
|
| 50 | showsVerticalScrollIndicator={false}
|
| 51 | >
|
| 52 | <Section>
|
| 53 | <SectionHeader title="Recebidas" />
|
| 54 |
|
| 55 | {displayed ? (
|
| 56 | <Card style={{ padding: Spacing.cardGap }}>
|
| 57 | <Text>Title: {displayed.title}</Text>
|
| 58 | <Text>Body: {displayed.body}</Text>
|
| 59 | </Card>
|
| 60 | ) : (
|
| 61 | <Card style={{ padding: Spacing.cardGap }}>
|
| 62 | <Text>Nenhuma notificação recebida.</Text>
|
| 63 | </Card>
|
| 64 | )}
|
| 65 | </Section>
|
| 66 | </ScrollView>
|
| 67 | </KeyboardAvoidingView>
|
| 68 | </SafeAreaView>
|
| 69 | );
|
| 70 | }
|
| 71 |
|
| 72 | const styles = StyleSheet.create({
|
| 73 | container: {
|
| 74 | flex: 1,
|
| 75 | backgroundColor: Colors.light.background,
|
| 76 | },
|
| 77 | keyboardView: {
|
| 78 | flex: 1,
|
| 79 | },
|
| 80 | scrollContainer: {
|
| 81 | flexGrow: 1,
|
| 82 | paddingHorizontal: 24,
|
| 83 | justifyContent: "flex-start",
|
| 84 | minHeight: "100%",
|
| 85 | },
|
| 86 | header: {
|
| 87 | alignItems: "flex-start",
|
| 88 | marginBottom: 40,
|
| 89 | },
|
| 90 | });
|