Enhance notification handling and improve alert messages in index.tsx

This commit is contained in:
2025-07-27 00:36:27 +02:00
parent 2ecf1d229d
commit f9a8472da1

View File

@@ -1,6 +1,15 @@
import * as Notifications from 'expo-notifications'; import * as Notifications from 'expo-notifications';
import React, { useEffect } from "react"; import React, { useEffect } from "react";
import { Button, Platform, Text, View } from "react-native"; import { Alert, Button, Platform, Text, View } from "react-native";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowBanner: true,
shouldShowList: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
async function registerForPushNotificationsAsync() { async function registerForPushNotificationsAsync() {
const { status: existingStatus } = await Notifications.getPermissionsAsync(); const { status: existingStatus } = await Notifications.getPermissionsAsync();
@@ -10,22 +19,27 @@ async function registerForPushNotificationsAsync() {
finalStatus = status; finalStatus = status;
} }
if (finalStatus !== 'granted') { if (finalStatus !== 'granted') {
alert('Permission for notifications not granted!'); Alert.alert('Permission for notifications not granted!');
return false; return false;
} }
return true; return true;
} }
async function sendNotification() { async function sendNotification() {
await Notifications.scheduleNotificationAsync({ try {
content: { const id = await Notifications.scheduleNotificationAsync({
title: "Hello!", content: {
body: "Ez egy értesítés példája.", title: "Hello!",
}, body: "Ez egy értesítés példája.",
trigger: Platform.OS === 'android' },
? ({ seconds: 1, repeats: false } as any) trigger: Platform.OS === 'android'
: null, ? ({ seconds: 1, repeats: false } as any)
}); : null,
});
Alert.alert('Notification scheduled!', `ID: ${id}`);
} catch (e) {
Alert.alert('Hiba történt!', String(e));
}
} }
export default function Index() { export default function Index() {