├── .gitignore ├── .npmignore ├── Gruntfile.js ├── LICENSE ├── README.md ├── demo ├── app.js ├── assets │ ├── letter-j-32.ico │ └── sample.png ├── index.html └── message │ ├── message.html │ └── message.js ├── dist ├── angular-desktop-notification.js └── angular-desktop-notification.min.js ├── karma.conf.js ├── package.json ├── src ├── angular-desktop-notification.js └── module.js └── tests └── angular-desktop-notification.spec.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /demo 2 | .gitignore 3 | /Gruntfile.js 4 | karma.conf.js 5 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | grunt.initConfig({ 3 | browserify: { 4 | dev: { 5 | src: ['src/module.js'], 6 | dest: 'dist/angular-desktop-notification.js' 7 | } 8 | }, 9 | uglify: { 10 | prod: { 11 | options: { mangle: true, compress: true }, 12 | src: 'dist/angular-desktop-notification.js', 13 | dest: 'dist/angular-desktop-notification.min.js' 14 | } 15 | } 16 | }); 17 | 18 | grunt.loadNpmTasks('grunt-browserify'); 19 | grunt.loadNpmTasks('grunt-contrib-uglify'); 20 | grunt.registerTask('default', ['browserify', 'uglify']); 21 | }; 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Jhom San Pascual 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular Desktop Notification 2 | A simple HTML5 notification for Angular 1 3 | [DEMO](https://jmsanpascual.github.io/angular-desktop-notification/) 4 | 5 | ## Getting Started 6 | 1. Go to your project directory using your command line tool then install the project using npm 7 | 8 | ```shell 9 | npm install angular-desktop-notification 10 | ``` 11 | 2. Include angular.js and angular-desktop-notification.js to your index page. 12 | 13 | ```html 14 | 15 | 16 | ``` 17 | 3. Add the ngDesktopNotification module to you application. 18 | 19 | ```javascript 20 | angular.module('myApp', ['ngDesktopNotification']); 21 | ``` 22 | 4. You can now use the injectable service 'desktopNotification'. 23 | 24 | ```javascript 25 | angular.module('myApp').controller(function (desktopNotification) { 26 | desktopNotification.show('My Notification'); 27 | }); 28 | ``` 29 | 5. You can also set the default app-wide configuration for desktopNotification 30 | 31 | ```javascript 32 | angular.module('myApp').config(function (desktopNotificationProvider) { 33 | desktopNotificationProvider.config({ 34 | autoClose: false, 35 | showOnPageHidden: true 36 | }); 37 | }); 38 | ``` 39 | 40 | ## How to use 41 | A simple usage would be, request the permission and display the notification in the success callback 42 | ```javascript 43 | desktopNotification.requestPermission().then(function (permission) { 44 | // User allowed the notification 45 | desktopNotification.show('Hello', { 46 | body: 'I am an HTML5 notification', 47 | onClick: function () { 48 | // Handle click event 49 | } 50 | }); 51 | }, function (permission) { 52 | // User denied the notification 53 | }); 54 | ``` 55 | 56 | ## API Documentation 57 | 58 | This method returns true if the browser supports the Notification API, false otherwise 59 | ```javascript 60 | desktopNotification.isSupported(); 61 | ``` 62 | 63 | This method will get the current permission set in the browser which could be one of the ff. 64 | - desktopNotification.permissions.default 65 | - desktopNotification.permissions.denied 66 | - desktopNotification.permissions.granted 67 | ```javascript 68 | desktopNotification.currentPermission(); 69 | ``` 70 | 71 | This method returns a $q promise, if the user allowed the notification the successCallback will be executed, errorCallback will be executed otherwise 72 | ```javascript 73 | desktopNotification.requestPermission().then(successCallback, errorCallback); 74 | ``` 75 | 76 | This method will display the notification using the parameter values 77 | - title - should be a string 78 | - options - should be an object with the ff. properties 79 | - options.body - the message of the notification 80 | - options.icon - a string path of an icon, jpg and etc. 81 | - options.autoClose - a boolean property that will close the notification after the duration specified (Defaults to true) 82 | - options.duration - an integer that will set the seconds before the notification is automatically closed (Defaults to 5) 83 | - options.showOnPageHidden - a boolean property that will only show the notification if the page is hidden (Defaults to false) 84 | - options.onClick - a function that will trigger when the notification is clicked (Defaults to none) 85 | ```javascript 86 | desktopNotification.show(title, options); 87 | ``` 88 | > Note: Please see all the available parameters here at the [official documentation](https://developer.mozilla.org/en/docs/Web/API/notification#Instance_properties) 89 | 90 | ## Limitations 91 | Angular Desktop Notification is not supported in all browsers. 92 | Please see [supported browser versions](http://caniuse.com/#feat=notifications) for more information on the official support specification. 93 | 94 | Also, only Chrome allows the desktop notification to persist, if and only if, the user hovers on it. Please check the [other browsers closing behavior](https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API#Closing_notifications). 95 | 96 | ## Inspirations and Motivations 97 | - https://github.com/sagiegurari/angular-web-notification 98 | - https://github.com/ttsvetko/HTML5-Desktop-Notifications 99 | 100 | ## License 101 | This project is licensed under the MIT License - see the [LICENSE](https://github.com/jmsanpascual/angular-desktop-notification/blob/master/LICENSE) file for details 102 | 103 | ## TODO 104 | - [x] Build tools for concatenation and minification 105 | - [x] Unit tests (Incomplete) 106 | - [ ] Support for older browser versions and IE 107 | -------------------------------------------------------------------------------- /demo/app.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | 'use strict'; 3 | 4 | angular 5 | .module('demo', ['ngDesktopNotification']) 6 | .controller('DemoController', DemoController); 7 | 8 | DemoController.$inject = ['$scope', 'desktopNotification']; 9 | 10 | function DemoController($scope, desktopNotification) { 11 | var vm = this, 12 | popup; 13 | 14 | vm.permission = undefined; 15 | vm.checkiFSupported = checkiFSupported; 16 | vm.requestPermission = requestPermission; 17 | vm.showNotification = showNotification; 18 | vm.showNotificationWithIcon = showNotificationWithIcon; 19 | vm.showNotificationWithClick = showNotificationWithClick; 20 | vm.openPopupWindow = openPopupWindow; 21 | vm.sendMessage = sendMessage; 22 | 23 | activate(); 24 | 25 | function activate() { 26 | vm.isSupported = desktopNotification.isSupported(); 27 | vm.permission = desktopNotification.currentPermission(); 28 | vm.autoClose = true; 29 | } 30 | 31 | function checkiFSupported() { 32 | vm.isSupported = desktopNotification.isSupported(); 33 | alert('Supported: ' + (vm.isSupported ? 'true' : 'false')); 34 | } 35 | 36 | function requestPermission() { 37 | desktopNotification.requestPermission().then(function (permission) { 38 | vm.permission = permission; 39 | alert('Permission: true'); 40 | }, function (permission) { 41 | vm.permission = permission; 42 | 43 | if (vm.permission === 'denied') { 44 | alert('Requesting permission again when the user has once blocked' + 45 | ' the notification is not possible'); 46 | } else { 47 | alert('Permission: false'); 48 | } 49 | }); 50 | } 51 | 52 | function showNotification() { 53 | var notif = desktopNotification.show('1234', { 54 | body: 'I am a simple notification', 55 | autoClose: vm.autoClose 56 | }); 57 | 58 | if (!notif) { 59 | alert('Desktop notification is either not supported or blocked.'); 60 | } 61 | } 62 | 63 | function showNotificationWithIcon() { 64 | var notif = desktopNotification.show('Notification with Icon', { 65 | icon: 'assets/letter-j-32.ico', 66 | body: 'I have an icon!', 67 | autoClose: vm.autoClose 68 | }); 69 | 70 | if (!notif) { 71 | alert('Desktop notification is either not supported or blocked.'); 72 | } 73 | } 74 | 75 | function showNotificationWithClick() { 76 | var notif = desktopNotification.show('Notification with Click Event', { 77 | icon: 'assets/letter-j-32.ico', 78 | body: 'Click me!', 79 | autoClose: vm.autoClose, 80 | onClick: function (event) { 81 | alert('Notification is clicked!'); 82 | } 83 | }); 84 | 85 | if (!notif) { 86 | alert('Desktop notification is either not supported or blocked.'); 87 | } 88 | } 89 | 90 | function openPopupWindow() { 91 | if (popup) { 92 | alert('Popup is already open'); 93 | return; 94 | } 95 | 96 | popup = window.open('message/message.html'); 97 | } 98 | 99 | function sendMessage(message) { 100 | if (popup) { 101 | popup.postMessage(message, '*'); 102 | } 103 | } 104 | } 105 | 106 | })(); 107 | -------------------------------------------------------------------------------- /demo/assets/letter-j-32.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmsanpascual/angular-desktop-notification/106f1373d5a6a857489b36aae6f53b7f41c10923/demo/assets/letter-j-32.ico -------------------------------------------------------------------------------- /demo/assets/sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmsanpascual/angular-desktop-notification/106f1373d5a6a857489b36aae6f53b7f41c10923/demo/assets/sample.png -------------------------------------------------------------------------------- /demo/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |
15 | Supported: {{ vm.isSupported }}
16 | Permission: {{ vm.permission }}
17 |
13 | Notification should work 14 |
15 |17 | Just like the image below: (Notification should not work) 18 |
19 |