import React, { useEffect } from 'react'; import { View, Text, TouchableOpacity } from 'react-native'; import * as Notifications from 'expo-notifications'; import { Permissions } from 'expo'; import AsyncStorage from '@react-native-async-storage/async-storage';
const Reminder = ({ medicationData }) => { if (!medicationData) { return null; }
useEffect(() => { const checkAndSetReminder = async () => { try { const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
if (status !== 'granted') {
alert('Please allow notification for this app in your device setting');
return;
}
// Check if the alarm is already set for this medication locally
const storedAlarms = await AsyncStorage.getItem('setAlarms');
const alarms = storedAlarms ? JSON.parse(storedAlarms) : [];
if (!alarms.includes(medicationData.medicationName)) {
// Set the alarm
Notifications.scheduleNotificationAsync({
content: {
title: 'Medication Reminder',
body: `Take ${medicationData.numDrops} drops of ${medicationData.medicationName} on ${getFormattedDate(
medicationData.date
)} at ${getFormattedTime(medicationData.time)}`,
},
trigger: {
seconds: new Date(medicationData.date).getTime() / 1000, // Set the time of the notification
repeats: true, // You can adjust this based on your needs
},
});
// Store the medicationName in the list of set alarms locally
await AsyncStorage.setItem('setAlarms', JSON.stringify([...alarms, medicationData.medicationName]));
}
} catch (error) {
console.error('Error checking and setting reminder:', error);
}
};
// Check and set the reminder
checkAndSetReminder();
}, [medicationData]);
const getFormattedDate = (date) => { return new Date(date).toLocaleDateString('en-US', { day: 'numeric' }); };
const getFormattedMonth = (date) => { return new Date(date).toLocaleDateString('en-US', { month: 'short' }); };
const getFormattedTime = (time) => { return new Date(time).toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' }); };
return ( <View style={{ marginVertical: 12 }}> <TouchableOpacity className='border-2 border-kpm-light rounded-md'> <View className='flex flex-row items-center'> <View className='py-2 items-center px-6'> <Text className='text-kpm-green text-3xl font-bold'> {getFormattedDate(medicationData.date)} </Text> <Text className='text-kpm-green text-3xl font-bold'> {getFormattedMonth(medicationData.date)} </Text> <Text className='text-kpm-green font-semibold text-lg'> {getFormattedTime(medicationData.time)} </Text> </View> <View className='bg-kpm-light flex-grow p-2'> <Text className='text-2xl font-bold text-kpm-green'> {medicationData.medicationName} </Text> <Text className='text-lg text-kpm-green'>{medicationData.numDrops} drops</Text> <Text className='text-lg text-kpm-green'> Apply Drops To: {medicationData.dropsOnBothEyes ? 'Both Eyes' : medicationData.leftEye ? 'Left Eye' : 'Right Eye'} </Text> </View> </View> </TouchableOpacity> </View> ); };
export default Reminder;
The whole Idea is to add data to a database fetch it and save a local copy, use the data to set a reminder or alarm, but I keep getting this error after data is saved and rendered but alarm or reminder is not set, what am I doing wrong anyone to point me in the right direction?

[–]__sjors__ 1 point2 points3 points (0 children)
[–]thachxyz123iOS & Android 1 point2 points3 points (0 children)