From 614e1fd9ed4cba4c8a14fc88816afe45fad483e0 Mon Sep 17 00:00:00 2001 From: Vincent Fournier Date: Mon, 24 Aug 2015 10:32:29 -0400 Subject: [PATCH] Add HTML5 notifications Change-Id: I086ce9d7118e54e56bdac035925054fd627c337d --- app/components/notifications/notifications.js | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/app/components/notifications/notifications.js b/app/components/notifications/notifications.js index 3b9b6b3..2cc42db 100644 --- a/app/components/notifications/notifications.js +++ b/app/components/notifications/notifications.js @@ -1,23 +1,43 @@ -/*global PNotify */ +/*global PNotify, window, Notification */ 'use strict'; angular.module('bansho.notifications', []) .service('notifications', [ function () { - var push = function (type, title, message) { + var sendNotification = function (type, title, message) { + new Notification(title, {body: message}); + }; + + var sendPNotify = function (type, title, message) { $(function(){ new PNotify({ type: type, title: title, text: message, - styling: {}, + styling: {} }); }); }; + var push = function (type, title, message) { + if (window.Notification && Notification.permission === "granted") { + sendNotification(type, title, message); + } else if (window.Notification && Notification.permission !== "denied") { + sendPNotify(type, title, message); + + // Ask permission for next time + Notification.requestPermission(function (status) { + if (Notification.permission !== status) { + Notification.permission = status; + } + }); + } else { + sendPNotify(type, title, message); + } + }; + return { "push": push }; - }]);