Javascript | Send OS X Notifications

Wednesday, October 31st, 2018 | Coding
Last Modified: 2024-04-26



< script>
	
	var notify = function () {
	    // Check for notification compatibility.
	    if (!'Notification' in window) {
	        // If the browser version is unsupported, remain silent.
	        return;
	    }
	    // Log current permission level
	    console.log(Notification.permission);
	    // If the user has not been asked to grant or deny notifications
	    // from this domain...
	    if (Notification.permission === 'default') {
	        Notification.requestPermission(function () {
	            // ...callback this function once a permission level has been set.
	            notify();
	        });
	    }
	    // If the user has granted permission for this domain to send notifications...
	    else if (Notification.permission === 'granted') {
	        var n = new Notification(
	                    'New message from Liz',
	                    {
	                      'body': 'Liz: "Hi there!"',
	                      // ...prevent duplicate notifications
	                      'tag' : 'unique string'
	                    }
	                );
	        // Remove the notification from Notification Center when clicked.
	        n.onclick = function () {
	            this.close();
	        };
	        // Callback function when the notification is closed.
	        n.onclose = function () {
	            console.log('Notification closed');
	        };
	    }
	    // If the user does not want notifications to come from this domain...
	    else if (Notification.permission === 'denied') {
	        // ...remain silent.
	        return;
	    }
	};
	    
    notify();
	
< /script>




Input Action Output
A collection of snippets and links that have proven useful for development, programming, ColdFusion, Javascript, jQuery, PHP, Python, Dell, Minecraft, Apple, Mac, Windows, LINUX, Raspberry Pi, Adobe, CSS, and HTML.

©2024 Input Action Output