diff --git a/firebase-messaging-sw.ts b/firebase-messaging-sw.ts index 7a752924fd..9208192bcc 100644 --- a/firebase-messaging-sw.ts +++ b/firebase-messaging-sw.ts @@ -23,6 +23,7 @@ fetch('/__/firebase/init.json').then(async (response) => { const messaging = getMessaging(app); onMessage(messaging, (payload) => { + console.log('firebase-messaging-sw.onMessage', { payload }); const notification = buildNotification(payload); return self.registration.showNotification(notification.title, notification); }); diff --git a/functions/src/notifications.ts b/functions/src/notifications.ts index 4ba3f60822..7aa932e9e9 100644 --- a/functions/src/notifications.ts +++ b/functions/src/notifications.ts @@ -17,8 +17,10 @@ export const sendGeneralNotification = functions.firestore const timestamp = context.params.timestamp; const message = snapshot.data(); - if (!message) return null; - console.log('New message added at ', timestamp, ' with payload ', message); + if (!message) return undefined; + + console.log(`New message added at ${timestamp} with payload ${message}`); + const deviceTokensPromise = getFirestore().collection('notificationsSubscribers').get(); const notificationsConfigPromise = getFirestore() .collection('config') @@ -37,9 +39,9 @@ export const sendGeneralNotification = functions.firestore if (!tokens.length) { console.log('There are no notification tokens to send to.'); - return null; + return undefined; } - console.log('There are', tokens.length, 'tokens to send notifications to.'); + console.log(`There are ${tokens.length} tokens to send notifications to.`); const payload = { data: { @@ -53,12 +55,13 @@ export const sendGeneralNotification = functions.firestore messagingResponse.results.forEach((result, index) => { const error = result.error; if (error) { - console.error('Failure sending notification to', tokens[index], error); + console.error(`Failure sending notification to ${tokens[index]}`, error); if (REMOVE_TOKEN_ERROR.includes(error.code)) { const tokenRef = getFirestore().collection('notificationsSubscribers').doc(tokens[index]); tokensToRemove.push(tokenRef.delete()); } } }); + return Promise.all(tokensToRemove); }); diff --git a/src/elements/header-toolbar.ts b/src/elements/header-toolbar.ts index 7f3668ceb6..29f99dfeb3 100644 --- a/src/elements/header-toolbar.ts +++ b/src/elements/header-toolbar.ts @@ -333,7 +333,7 @@ export class HeaderToolbar extends ReduxMixin(PolymerElement) { } @observe('signedIn') - _authStatusChanged(_signedIn?: boolean) { + _authStatusChanged(_signedIn: boolean) { if (this.isDialogOpen) { closeDialog(); } @@ -348,7 +348,7 @@ export class HeaderToolbar extends ReduxMixin(PolymerElement) { store.dispatch(requestPermission); } - _getNotificationsIcon(status) { + _getNotificationsIcon(status: NOTIFICATIONS_STATUS) { return status === NOTIFICATIONS_STATUS.DEFAULT ? 'bell-outline' : status === NOTIFICATIONS_STATUS.GRANTED @@ -356,7 +356,7 @@ export class HeaderToolbar extends ReduxMixin(PolymerElement) { : 'bell-off'; } - _hideNotificationBlock(status, blockStatus) { + _hideNotificationBlock(status: NOTIFICATIONS_STATUS, blockStatus: NOTIFICATIONS_STATUS) { return status !== NOTIFICATIONS_STATUS[blockStatus]; } diff --git a/src/store/notifications/actions.ts b/src/store/notifications/actions.ts index cbf40fc114..816508616f 100644 --- a/src/store/notifications/actions.ts +++ b/src/store/notifications/actions.ts @@ -1,7 +1,13 @@ import { Success } from '@abraham/remotedata'; import { Router } from '@vaadin/router'; import { doc, DocumentReference, DocumentSnapshot, getDoc, setDoc } from 'firebase/firestore'; -import { deleteToken, getMessaging, getToken as fbGetToken, onMessage } from 'firebase/messaging'; +import { + deleteToken, + getMessaging, + getToken as fbGetToken, + MessagePayload, + onMessage, +} from 'firebase/messaging'; import { Dispatch } from 'redux'; import { RootState } from '..'; import { log } from '../../console'; @@ -14,13 +20,15 @@ import { NotificationActions, NOTIFICATIONS_STATUS, UPDATE_NOTIFICATIONS_STATUS const messaging = getMessaging(firebaseApp); -onMessage(messaging, (payload) => { +onMessage(messaging, (payload: MessagePayload) => { const { notification } = payload; if (!notification) { log('Message missing payload'); return; } + console.log('notifications/actions.onMessage', { payload }); + showToast({ message: `${notification.title} ${notification.body}`, action: {