Update daily notification logic: reset lastNotifiedDate on time change and add debug logs
This commit is contained in:
40
src/App.jsx
40
src/App.jsx
@@ -673,37 +673,67 @@ export default function App() {
|
||||
useEffect(() => {
|
||||
if (!user || !dailyOutfitNotifEnabled || !dailyOutfitTime) return;
|
||||
|
||||
console.log('[Daily Outfit] Notification system initialized. Scheduled for:', dailyOutfitTime);
|
||||
|
||||
const intervalId = setInterval(() => {
|
||||
const now = new Date();
|
||||
const todayStr = `${now.getFullYear()}-${String(now.getMonth()+1).padStart(2,'0')}-${String(now.getDate()).padStart(2,'0')}`;
|
||||
|
||||
console.log(`[Daily Outfit] Checking... Time now: ${now.getHours()}:${now.getMinutes()}, Scheduled: ${dailyOutfitTime}. Last notified: ${lastNotifiedDate}`);
|
||||
|
||||
if (lastNotifiedDate !== todayStr) {
|
||||
const currentMins = now.getHours() * 60 + now.getMinutes();
|
||||
const [targetH, targetM] = dailyOutfitTime.split(':').map(Number);
|
||||
const targetMins = targetH * 60 + targetM;
|
||||
|
||||
if (currentMins >= targetMins) {
|
||||
console.log('[Daily Outfit] Time matched! Checking for outfits today...');
|
||||
const todaysLooks = getLooksForDayGlobal(todayStr);
|
||||
|
||||
if (todaysLooks.length > 0) {
|
||||
const mainLook = todaysLooks[0];
|
||||
if ('Notification' in window && Notification.permission === 'granted') {
|
||||
navigator.serviceWorker?.getRegistration().then(reg => {
|
||||
console.log(`[Daily Outfit] Found outfit: ${mainLook.name}. Attempting to send notification...`);
|
||||
|
||||
if ('Notification' in window) {
|
||||
if (Notification.permission === 'granted') {
|
||||
const title = 'MyCloset - Outfit Diário';
|
||||
const options = {
|
||||
body: `O seu outfit planeado "${mainLook.name}" está pronto para hoje!`,
|
||||
icon: '/favicon.ico'
|
||||
};
|
||||
if (reg) {
|
||||
|
||||
try {
|
||||
navigator.serviceWorker?.getRegistration().then(reg => {
|
||||
if (reg && reg.showNotification) {
|
||||
console.log('[Daily Outfit] Using Service Worker to show notification.');
|
||||
reg.showNotification(title, options);
|
||||
} else {
|
||||
console.log('[Daily Outfit] Using standard Notification API.');
|
||||
new Notification(title, options);
|
||||
}
|
||||
}).catch(err => {
|
||||
console.error('[Daily Outfit] Service Worker registration failed, using standard API.', err);
|
||||
new Notification(title, options);
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('[Daily Outfit] Fallback notification error:', e);
|
||||
new Notification(title, options);
|
||||
}
|
||||
|
||||
setLastNotifiedDate(todayStr);
|
||||
saveUserSetting('lastNotifiedDate', todayStr);
|
||||
} else {
|
||||
console.log('[Daily Outfit] Permission not granted. Current state:', Notification.permission);
|
||||
}
|
||||
} else {
|
||||
console.log('[Daily Outfit] Notifications API not supported in this browser.');
|
||||
}
|
||||
} else {
|
||||
console.log('[Daily Outfit] No outfits planned for today.');
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log('[Daily Outfit] Already notified today.');
|
||||
}
|
||||
}, 30000); // Verifica a cada 30 segundos
|
||||
|
||||
@@ -2659,6 +2689,8 @@ export default function App() {
|
||||
onChange={(e) => {
|
||||
setDailyOutfitTime(e.target.value);
|
||||
saveUserSetting('dailyOutfitTime', e.target.value);
|
||||
setLastNotifiedDate('');
|
||||
saveUserSetting('lastNotifiedDate', '');
|
||||
}}
|
||||
className={`px-3 py-1.5 rounded-xl text-sm outline-none border font-bold ${darkMode ? 'bg-gray-800 border-gray-700 text-white' : 'bg-white border-gray-200 text-gray-900'}`}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user