Add button to enable desktop notifications

This commit is contained in:
Sam Chow 2018-05-03 11:46:02 -04:00
parent e80c56e441
commit 2d3583fb44
3 changed files with 96 additions and 0 deletions

View file

@ -266,6 +266,11 @@ function($rootScope, $interval, UserService, ApiService, StringBuilderService, P
notificationService.notifications = resp['notifications'];
notificationService.additionalNotifications = resp['additional'];
notificationService.notificationClasses = notificationService.getClasses(notificationService.notifications);
if (notificationService.notifications.length > 0 && localStorage.getItem('quay.enabledDesktopNotifications') === 'on') {
console.log(notificationService);
notificationService.sendBrowserNotifications();
}
});
if (Features.APP_SPECIFIC_TOKENS) {
@ -277,6 +282,33 @@ function($rootScope, $interval, UserService, ApiService, StringBuilderService, P
});
}
};
notificationService.sendBrowserNotifications = () => {
let mostRecentTimestamp = parseInt(localStorage.getItem('quay.notifications.mostRecentTimestamp'), 10);
if (!mostRecentTimestamp) {
mostRecentTimestamp = new Date(notificationService.notifications[0].created).getTime();
}
const newNotifications = notificationService.notifications
.filter(obj => new Date(obj.created).getTime() > mostRecentTimestamp);
if (newNotifications.length > 0) {
let message = 'You have unread notifications';
if (newNotifications.length === 1) {
message = notificationService.getMessage(newNotifications[0]);
}
new Notification(message, {
icon: 'http://quay.io/static/img/quay-logo.png',
image: 'http://quay.io/static/img/quay-logo.png',
});
const newTimestamp = new Date(newNotifications[0].created).getTime();
localStorage.setItem('quay.notifications.mostRecentTimestamp', newTimestamp.toString());
}
};
notificationService.reset = function() {
$interval.cancel(pollTimerHandle);
pollTimerHandle = $interval(notificationService.update, 5 * 60 * 1000 /* five minutes */);