Untitled
// Step 1: Set up the notification channel for Android useEffect(() => { // Android-specific configuration for notification channels Notifications.setNotificationChannelAsync("custom_sound_channel444sds", { name: "Custom Sound Notifications", importance: Notifications.AndroidImportance.HIGH, sound: "notfication_sound", // This must match the sound name from app.json vibrationPattern: [0, 250, 250, 250], // Customize vibration lightColor: "#FF231F7C", // Customize LED color for the notification }); // Step 2: Set global notification handler for foreground notifications Notifications.setNotificationHandler({ handleNotification: async () => ({ shouldShowAlert: true, // Show alert when notification is received shouldPlaySound: true, // Play sound when notification is received shouldSetBadge: false, // Don't update app badge }), }); }, []); // Run once on component mount // Step 3: Function to send a custom notification with the sound const sendCustomNotification = async () => { await Notifications.scheduleNotificationAsync({ content: { title: "Order Received", // Notification title body: "You have a new order request!", // Notification body message sound: "notfication_sound", // The name of the custom sound defined in app.json data: { orderId: "12345" }, // Additional data for the notification }, trigger: null, // Send the notification immediately }); }; sendCustomNotification(); // Step 4: Listen for background notifications (optional) useEffect(() => { const backgroundSubscription = Notifications.addNotificationReceivedListener((notification) => { // Handle background notification console.log("Background Notification received:", notification); }); // Cleanup the listener when component unmounts return () => backgroundSubscription.remove(); }, []);
Leave a Comment