Creating a Desktop Notification using JavaScript Notification API Tutorial

Introduction

In this tutorial, I will teach you how to create a Desktop Notification using JavaScript Notification API. This tutorial aims to help IT/CS students or beginners learn some ways and techniques for developing web applications. Here, sample source code and snippets will be provided that demonstrates how to use JS Notification API and how to apply it to your own site or web application.

What is a Desktop Notification?

A Desktop Notification is a popup notification window on your desktop that notifies your software or application end-users. It contains data or content about the site or software notification which commonly includes the site logo, image banner, title, and description. Desktop notification helps your application or site to alert your end-users even if the browser is minimized.

What is a JavaScript Notification API?

Notification API is JavaScript API for displaying notifications to end-users. These can be seen even after the user has changed tabs or switched to another app because they are outside the top-level browsing context viewport. The API is intended to work with current notification systems on various platforms.

How to enable Notification in your browser?

The JavaScript Notification API provides the developer with a simple script for asking end-users to allow the site to use the browser's or system's notification feature to alert or notify them. To ask the end-user, you can use the Notification.requestPermission() method. Here's an example snippet for using requestPermission() method of Notification API.

  1. Notification.requestPermission().then((permission)=>{
  2. if(permission === 'granted'){
  3. alert("Notification Permission is Granted")
  4. }else if(permission === 'denied'){
  5. alert("Notification Permission is Denied")
  6. }
  7. })

Note: The Notification API is not supported by some other browsers or platforms. to learn more about this API compatibility. Visit https://developer.mozilla.org/en-US/docs/Web/API/notification#browser_compatibility

Example

Here is an example application that demonstrates the usage of Notification API.

Site Page Interface

The following script uses Bootstrap Framework CDN which means an internet connection is required to run the script with the right design on your local machine.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Desktop Notification Demo</title>
  7. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
  8. <script src="https://code.jquery.com/jquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous"></script>
  9. <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-IDwe1+LCz02ROU9k972gdyvl+AESN10+x7tBKgc9I5HFtuNz0wWnPclzo6p9vxnk" crossorigin="anonymous"></script>
  10.  
  11. html, body{
  12. min-height:calc(100%);
  13. width: calc(100%);
  14. }
  15. body{
  16. min-height: 100vh;
  17. width: 100vw;
  18. display: flex;
  19. flex-direction: column;
  20. align-items: center;
  21. justify-content: center;
  22. }
  23. </style>
  24. </head>
  25. <body class="bg-light bg-gradient">
  26. <div class="container w-100">
  27. <h1 class="text-center fw-bolder my-4">Desktop Notification using JavaScript Notification API</h1>
  28. <div class="d-flex justify-content-center">
  29. <hr class="col-4">
  30. </div>
  31. <div class="card rounded-0 shadow col-lg-5 col-md-7 col-sm-12 mx-auto">
  32. <div class="card-body">
  33. <div class="container-fluid">
  34. <p>This is a sample application that demonstrates the JavaScript Notification API.</p>
  35. <p class="text-muted">Sample Code of using JS Notification API</p>
  36. <pre class="text-bg-dark">
  37. <code>
  38. if(!("Notification" in window)){
  39. alert("Your current browser does not support desktop notification!");
  40. console.error("Your current browser does not support desktop notification!");
  41. }else{
  42. console.log(Notification.permission)
  43. if(Notification.permission !== 'granted' && Notification.permission !== 'denied'){
  44. Notification.requestPermission().then((permission)=>{
  45. if(permission === 'granted'){
  46. alert("Notification Permission is Granted")
  47. }else if(permission === 'denied'){
  48. alert("Notification Permission is Denied")
  49. }
  50. location.reload()
  51. })
  52. }
  53. }
  54. </code>
  55. </pre>
  56. <div class="d-grid gap-2">
  57. <button class="btn btn-sm btn-primary rounded-0" type="button" id="notif1">Sample Notification #1</button>
  58. <button class="btn btn-sm btn-warning rounded-0" type="button" id="notif2">Sample Notification #2</button>
  59. <button class="btn btn-sm btn-info rounded-0" type="button" id="notif3">Sample Notification #3 w/ Image</button>
  60. </div>
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65. </body>
  66.  
  67. <script src="./notification.js"></script>
  68. </html>

Main JS Script

The following script is a JavaScript file that contains the scripts that request permission to end-user for using notification and triggers the sample notifications.

  1. $(document).ready(function(){
  2.  
  3. function ask_permission(){
  4. if(!("Notification" in window)){
  5. alert("Your current browser does not support desktop notification!");
  6. console.error("Your current browser does not support desktop notification!");
  7. }else{
  8. console.log(Notification.permission)
  9. if(Notification.permission !== 'granted' && Notification.permission !== 'denied'){
  10. Notification.requestPermission().then((permission)=>{
  11. if(permission === 'granted'){
  12. alert("Notification Permission is Granted")
  13. }else if(permission === 'denied'){
  14. alert("Notification Permission is Denied")
  15. }
  16. location.reload()
  17. })
  18. }
  19. }
  20. }
  21. ask_permission()
  22. $('#notif1').click(function(){
  23. if(Notification.permission === 'default'){
  24. ask_permission()
  25. }else if(Notification.permission === 'granted'){
  26. var notification = new Notification("Sample Notification #1",{
  27. body:"This is a sample notification content or description 101.",
  28. icon:'http://localhost/js_notification/test-icon.png'
  29. })
  30. notification.onclick = (event)=>{
  31. event.preventDefault()
  32. window.open("https://sourcecodester.com")
  33. }
  34. }else{
  35. alert("Notification Permission is not granted.")
  36. }
  37. })
  38.  
  39. $('#notif2').click(function(){
  40. if(Notification.permission === 'default'){
  41. ask_permission()
  42. }else if(Notification.permission === 'granted'){
  43. var notification = new Notification("JavaScript Tutorials",{
  44. body:"Here are some tutorials of using JavaScript for IT/CS Students or beginners.",
  45. icon:'http://localhost/js_notification/test-icon.png'
  46. })
  47. notification.onclick = (event)=>{
  48. event.preventDefault()
  49. window.open("https://www.sourcecodester.com/tutorial/javascript")
  50. }
  51. }else{
  52. alert("Notification Permission is not granted.")
  53. }
  54. })
  55. $('#notif3').click(function(){
  56. if(Notification.permission === 'default'){
  57. ask_permission()
  58. }else if(Notification.permission === 'granted'){
  59. var notification = new Notification("JavaScript Tutorials",{
  60. body:"This mini-project is entitled Basketball Scoreboard Application. It is a web application that simulates the scoreboard of a basketball game. It has a simple and dark user interface, easy-to-read fonts, and uses Bootstrap Framework.",
  61. icon:'http://localhost/js_notification/test-icon.png',
  62. image:'https://www.sourcecodester.com/sites/default/files/styles/large/public/images/oretnom23/JS-scoreboard-basketball.jpg'
  63. })
  64. notification.onclick = (event)=>{
  65. event.preventDefault()
  66. window.open("https://www.sourcecodester.com/javascript/15730/basketball-scoreboard-application-using-javascript-and-jquery-free-source-code")
  67. }
  68. }else{
  69. alert("Notification Permission is not granted.")
  70. }
  71. })
  72. })

Sample Snapshots

Here are the snapshots of some results of the example application.

Application Main Page

Application Main Page

Sample Notification

Sample Notification

Sample Notification with Image

Sample Notification with Image

DEMO VIDEO

There you have it! You can now test the sample application that demonstrates the usage of JS Notification API. You can also download the sample application source code on this website for free. The download button is located below this article. Feel free to download and modify it to do some experiments using the said API.

That's the end of this tutorial. I hope this JavaScript Notification API Tutorial helps you with what you are looking for and that you'll find this useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Add new comment