Files
eggs-backend/resources/firebase.js
husanjon ab73d05ecc
Some checks failed
Build and Push to Docker Hub / build-test-push (push) Failing after 1m55s
gold eggs backend
2026-04-15 08:59:36 +02:00

31 lines
1008 B
JavaScript

// Request permission to receive notifications
messaging.requestPermission()
.then(() => messaging.getToken())
.then(token => {
console.log('====================');
console.log('FCM Token:', token);
console.log('====================');
// Send the token to the server to subscribe to notifications
fetch('/save-fcm-token', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ token: token })
});
})
.catch(error => {
console.error('Error getting permission or token', error);
});
// Handle incoming messages
messaging.onMessage(payload => {
console.log('Message received. ', payload);
// Customize notification here
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
};
new Notification(notificationTitle, notificationOptions);
});