diff --git a/app/coffee/modules/events.coffee b/app/coffee/modules/events.coffee index 6202289b..4e3d4874 100644 --- a/app/coffee/modules/events.coffee +++ b/app/coffee/modules/events.coffee @@ -87,6 +87,35 @@ class EventsService @liveAnnouncementService.show(data.title, data.desc) @rootScope.$digest() + liveNotifications: -> + userId = @.auth.userData.get('id') + + subscribe = () => + @.subscribe null, "live_notifications.#{userId}", (data) => + notification = new Notification(data.title, { + icon: "/#{window._version}/images/favicon.png", + body: data.body, + tag: data.id + }) + notification.onshow = () => + if data.timeout and data.timeout > 0 + setTimeout => + notification.close() + , + data.timeout + + if data.url + notification.onclick = () => + window.open data.url + if !Notification + console.log("This browser does not support desktop notification") + else if Notification.permission == "granted" + subscribe() + else if Notification.permission != 'denied' + Notification.requestPermission (permission) => + if (permission == "granted") + subscribe() + ########################################### # Heartbeat (Ping - Pong) ########################################### @@ -216,6 +245,7 @@ class EventsService @.sendMessage(message) @.startHeartBeatMessages() @.notifications() + @.liveNotifications() onMessage: (event) -> @.log.debug "WebSocket message received: #{event.data}"