blob: 00e04534b66caea73c9210be39c09df67fca29bc [file] [log] [blame]
Matteo Scandolo686547a2017-08-08 13:05:25 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070019/* eslint-disable angular/ng_window_service*/
20(function() {
21 'use strict';
22
23 angular
24 .module('xos.helpers')
25 .factory('Notification', function(){
26 return window.Notification;
27 })
28 /**
29 * @ngdoc service
30 * @name xos.helpers.xosNotification
31 * @description This factory define a set of helper function to trigger desktop notification
32 **/
33 .service('xosNotification', function($q, $log, Notification) {
34
35 this.checkPermission = () => {
36 const deferred = $q.defer();
37 Notification.requestPermission()
38 .then(permission => {
39 if (permission === 'granted') {
40 deferred.resolve(permission);
41 }
42 else {
43 deferred.reject(permission);
44 }
45 });
46 return deferred.promise;
47 };
48
49 this.sendNotification = (title, options) => {
50 const notification = new Notification(title, options);
51 notification.onerror = function(err){
52 $log.error(err);
53 };
54 };
55
56 /**
57 * @ngdoc method
58 * @name xos.helpers.xosNotification#notify
59 * @methodOf xos.helpers.xosNotification
60 * @description
61 * This method will check for user permission and if granted will send a browser notification.
62 * @param {string} title The notification title
63 * @param {object} options The notification options: `{icon: 'url', body: 'Notification body'}`
64 **/
65
66 this.notify = (title, options) => {
67 if (!('Notification' in window)) {
68 $log.info('This browser does not support desktop notification');
69 }
70 else if (Notification.permission !== 'granted') {
71 this.checkPermission()
72 .then(() => this.sendNotification(title, options));
73 }
74 else if (Notification.permission === 'granted') {
75 this.sendNotification(title, options);
76 }
77 }
78
79 })
80})();