Skip to content

Commit

Permalink
Cleanup notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
abraham committed Jan 9, 2022
1 parent fbd2f93 commit d0c74bb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions firebase-messaging-sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
13 changes: 8 additions & 5 deletions functions/src/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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: {
Expand All @@ -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);
});
6 changes: 3 additions & 3 deletions src/elements/header-toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export class HeaderToolbar extends ReduxMixin(PolymerElement) {
}

@observe('signedIn')
_authStatusChanged(_signedIn?: boolean) {
_authStatusChanged(_signedIn: boolean) {
if (this.isDialogOpen) {
closeDialog();
}
Expand All @@ -348,15 +348,15 @@ 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
? 'bell'
: 'bell-off';
}

_hideNotificationBlock(status, blockStatus) {
_hideNotificationBlock(status: NOTIFICATIONS_STATUS, blockStatus: NOTIFICATIONS_STATUS) {
return status !== NOTIFICATIONS_STATUS[blockStatus];
}

Expand Down
12 changes: 10 additions & 2 deletions src/store/notifications/actions.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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: {
Expand Down

0 comments on commit d0c74bb

Please sign in to comment.