feat: notification service update
This commit is contained in:
parent
b02a3bb22e
commit
f5f285a1ba
@ -18,13 +18,16 @@ firebase.initializeApp(firebaseConfig);
|
|||||||
// Initialize Firebase Messaging
|
// Initialize Firebase Messaging
|
||||||
const messaging = firebase.messaging();
|
const messaging = firebase.messaging();
|
||||||
|
|
||||||
// Handle background messages
|
// Handle background messages (data-only notifications)
|
||||||
messaging.onBackgroundMessage((payload) => {
|
messaging.onBackgroundMessage((payload) => {
|
||||||
console.log('Received background message ', payload);
|
console.log('Received background message ', payload);
|
||||||
|
|
||||||
const notificationTitle = payload.notification?.title || 'Wesal App';
|
// For data-only messages, title and body are in payload.data
|
||||||
|
const notificationTitle = payload.data?.title || payload.notification?.title || 'Wesal App';
|
||||||
|
const notificationBody = payload.data?.body || payload.notification?.body || 'You have a new notification';
|
||||||
|
|
||||||
const notificationOptions = {
|
const notificationOptions = {
|
||||||
body: payload.notification?.body || 'You have a new notification',
|
body: notificationBody,
|
||||||
icon: '/icons/Icon-192.png',
|
icon: '/icons/Icon-192.png',
|
||||||
badge: '/icons/Icon-192.png',
|
badge: '/icons/Icon-192.png',
|
||||||
data: payload.data,
|
data: payload.data,
|
||||||
@ -73,29 +76,7 @@ self.addEventListener('notificationclick', (event) => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle push events
|
// Note: Removed duplicate push event handler - Firebase onBackgroundMessage handles this
|
||||||
self.addEventListener('push', (event) => {
|
|
||||||
console.log('Push event received:', event);
|
|
||||||
|
|
||||||
if (event.data) {
|
|
||||||
const data = event.data.json();
|
|
||||||
console.log('Push data:', data);
|
|
||||||
|
|
||||||
const notificationTitle = data.notification?.title || 'Wesal App';
|
|
||||||
const notificationOptions = {
|
|
||||||
body: data.notification?.body || 'You have a new notification',
|
|
||||||
icon: '/icons/Icon-192.png',
|
|
||||||
badge: '/icons/Icon-192.png',
|
|
||||||
data: data.data,
|
|
||||||
tag: data.data?.type || 'general',
|
|
||||||
requireInteraction: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
event.waitUntil(
|
|
||||||
self.registration.showNotification(notificationTitle, notificationOptions)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle service worker activation
|
// Handle service worker activation
|
||||||
self.addEventListener('activate', (event) => {
|
self.addEventListener('activate', (event) => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user