diff --git a/server/ui-src/App.vue b/server/ui-src/App.vue index 139076e..e846b83 100644 --- a/server/ui-src/App.vue +++ b/server/ui-src/App.vue @@ -21,7 +21,9 @@ export default { searching: false, isConnected: false, scrollInPlace: false, - message: false + message: false, + notificationsSupported: false, + notificationsEnabled: false } }, watch: { @@ -47,6 +49,10 @@ export default { this.currentPath = window.location.hash.slice(1); }); + this.notificationsSupported = 'https:' == document.location.protocol + && ("Notification" in window && Notification.permission !== "denied"); + this.notificationsEnabled = this.notificationsSupported && Notification.permission == "granted"; + this.connect(); this.loadMessages(); }, @@ -214,6 +220,7 @@ export default { } self.total++; self.unread++; + self.browserNotify("New mail from: " + response.Data.From.Address, response.Data.Subject); } else if (response.Type == "prune") { // messages have been deleted, reload messages to adjust self.scrollInPlace = true; @@ -252,6 +259,41 @@ export default { let d = new Date(message.Created) return moment(d).fromNow().toString(); }, + + browserNotify: function(title, message) { + if (!("Notification" in window)) { + return; + } + + if (Notification.permission === "granted") { + let b = message.Subject; + let options = { + body: message, + icon: 'mailpit.png' + } + new Notification(title, options); + } + }, + + requestNotifications: function() { + // check if the browser supports notifications + if (!("Notification" in window)) { + alert("This browser does not support desktop notification"); + } + + // we need to ask the user for permission + else if (Notification.permission !== "denied") { + let self = this; + Notification.requestPermission().then(function (permission) { + // If the user accepts, let's create a notification + if (permission === "granted") { + self.browserNotify("Notifications enabled", "You will receive notifications when new mails are received."); + self.notificationsEnabled = true; + } + }); + } + } + } } @@ -315,7 +357,7 @@ export default {