Fix notifications logic and FCM Service Worker config
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
importScripts('https://www.gstatic.com/firebasejs/10.8.0/firebase-app-compat.js');
|
||||
importScripts('https://www.gstatic.com/firebasejs/10.8.0/firebase-messaging-compat.js');
|
||||
|
||||
// Configuração que o utilizador precisa substituir com os dados reais
|
||||
firebase.initializeApp({
|
||||
apiKey: "REPLACE_WITH_API_KEY",
|
||||
projectId: "REPLACE_WITH_PROJECT_ID",
|
||||
messagingSenderId: "REPLACE_WITH_MESSAGING_SENDER_ID",
|
||||
appId: "REPLACE_WITH_APP_ID"
|
||||
apiKey: "AIzaSyBBitFgNKnJ_3B0aqJgbbhGL_erufKd9lk",
|
||||
authDomain: "mycloset1-864c4.firebaseapp.com",
|
||||
projectId: "mycloset1-864c4",
|
||||
storageBucket: "mycloset1-864c4.firebasestorage.app",
|
||||
messagingSenderId: "219982610263",
|
||||
appId: "1:219982610263:web:0ebe67d9cf0e7d2753c812"
|
||||
});
|
||||
|
||||
const messaging = firebase.messaging();
|
||||
|
||||
48
src/App.jsx
48
src/App.jsx
@@ -675,31 +675,33 @@ export default function App() {
|
||||
|
||||
const intervalId = setInterval(() => {
|
||||
const now = new Date();
|
||||
const currentHour = String(now.getHours()).padStart(2, '0');
|
||||
const currentMinute = String(now.getMinutes()).padStart(2, '0');
|
||||
const currentTime = `${currentHour}:${currentMinute}`;
|
||||
|
||||
const todayStr = `${now.getFullYear()}-${String(now.getMonth()+1).padStart(2,'0')}-${String(now.getDate()).padStart(2,'0')}`;
|
||||
|
||||
if (currentTime === dailyOutfitTime && lastNotifiedDate !== todayStr) {
|
||||
const todaysLooks = getLooksForDayGlobal(todayStr);
|
||||
if (todaysLooks.length > 0) {
|
||||
const mainLook = todaysLooks[0];
|
||||
if ('Notification' in window && Notification.permission === 'granted') {
|
||||
navigator.serviceWorker?.getRegistration().then(reg => {
|
||||
const title = 'MyCloset - Outfit Diário';
|
||||
const options = {
|
||||
body: `O seu outfit planeado "${mainLook.name}" está pronto para hoje!`,
|
||||
icon: '/favicon.ico'
|
||||
};
|
||||
if (reg) {
|
||||
reg.showNotification(title, options);
|
||||
} else {
|
||||
new Notification(title, options);
|
||||
}
|
||||
});
|
||||
setLastNotifiedDate(todayStr);
|
||||
saveUserSetting('lastNotifiedDate', todayStr);
|
||||
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) {
|
||||
const todaysLooks = getLooksForDayGlobal(todayStr);
|
||||
if (todaysLooks.length > 0) {
|
||||
const mainLook = todaysLooks[0];
|
||||
if ('Notification' in window && Notification.permission === 'granted') {
|
||||
navigator.serviceWorker?.getRegistration().then(reg => {
|
||||
const title = 'MyCloset - Outfit Diário';
|
||||
const options = {
|
||||
body: `O seu outfit planeado "${mainLook.name}" está pronto para hoje!`,
|
||||
icon: '/favicon.ico'
|
||||
};
|
||||
if (reg) {
|
||||
reg.showNotification(title, options);
|
||||
} else {
|
||||
new Notification(title, options);
|
||||
}
|
||||
});
|
||||
setLastNotifiedDate(todayStr);
|
||||
saveUserSetting('lastNotifiedDate', todayStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user