| 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 { registerForPushNotifications } from "@/hooks/useRegisterPushToken";
|
| 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 params = useLocalSearchParams<NotificationParams>();
|
| 32 | const [liveNotification, setLiveNotification] = useState<ReceivedNotification | null>(null);
|
| 33 |
|
| 34 | useEffect(() => {
|
| 35 | const sub = Notifications.addNotificationReceivedListener((n) => {
|
| 36 | setLiveNotification({
|
| 37 | title: n.request.content.title ?? undefined,
|
| 38 | body: n.request.content.body ?? undefined,
|
| 39 | });
|
| 40 | });
|
| 41 | return () => sub.remove();
|
| 42 | }, []);
|
| 43 |
|
| 44 | const displayed = liveNotification ?? (params.title || params.body ? { title: params.title, body: params.body } : null);
|
| 45 |
|
| 46 | async function sendPushNotification() {
|
| 47 | const expoPushToken = await registerForPushNotifications();
|
| 48 |
|
| 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 | });
|