• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a Desktop Notification using JavaScript Notification API Tutorial

Keytv

Blockchain Consensus Architect
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
99
Likes
185
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    Desktop Notification Demo</

    title

    >
  8. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"

    integrity=

    "sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi"

    crossorigin=

    "anonymous"

    >
  9. <script

    src

    =

    "https://code.jquery.com/jquery-3.6.1.min.js"

    integrity=

    "sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ="

    crossorigin=

    "anonymous"

    ></

    script

    >
  10. <script

    src

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"

    integrity=

    "sha384-IDwe1+LCz02ROU9k972gdyvl+AESN10+x7tBKgc9I5HFtuNz0wWnPclzo6p9vxnk"

    crossorigin=

    "anonymous"

    ></

    script

    >

  11. <style

    >
  12. html, body{
  13. min-height:calc(100%);
  14. width: calc(100%);
  15. }
  16. body{
  17. min-height: 100vh;
  18. width: 100vw;
  19. display: flex;
  20. flex-direction: column;
  21. align-items: center;
  22. justify-content: center;
  23. }
  24. </

    style

    >
  25. </

    head

    >
  26. <body

    class

    =

    "bg-light bg-gradient"

    >
  27. <div

    class

    =

    "container w-100"

    >
  28. <h1

    class

    =

    "text-center fw-bolder my-4"

    >

    Desktop Notification using JavaScript Notification API</

    h1

    >
  29. <div

    class

    =

    "d-flex justify-content-center"

    >
  30. <hr

    class

    =

    "col-4"

    >
  31. </

    div

    >
  32. <div

    class

    =

    "card rounded-0 shadow col-lg-5 col-md-7 col-sm-12 mx-auto"

    >
  33. <div

    class

    =

    "card-body"

    >
  34. <div

    class

    =

    "container-fluid"

    >
  35. <p

    >

    This is a sample application that demonstrates the JavaScript Notification API.</

    p

    >
  36. <p

    class

    =

    "text-muted"

    >

    Sample Code of using JS Notification API</

    p

    >
  37. <pre

    class

    =

    "text-bg-dark"

    >
  38. <code

    >
  39. if(!("Notification" in window)){
  40. alert("Your current browser does not support desktop notification!");
  41. console.error("Your current browser does not support desktop notification!");
  42. }else{
  43. console.log(Notification.permission)
  44. if(Notification.permission !== 'granted' && Notification.permission !== 'denied'){
  45. Notification.requestPermission().then((permission)=>{
  46. if(permission === 'granted'){
  47. alert("Notification Permission is Granted")
  48. }else if(permission === 'denied'){
  49. alert("Notification Permission is Denied")
  50. }
  51. location.reload()
  52. })
  53. }
  54. }
  55. </code>
  56. </pre>
  57. <div class="d-grid gap-2">
  58. <button class="btn btn-sm btn-primary rounded-0" type="button" id="notif1">Sample Notification #1</button>
  59. <button class="btn btn-sm btn-warning rounded-0" type="button" id="notif2">Sample Notification #2</button>
  60. <button class="btn btn-sm btn-info rounded-0" type="button" id="notif3">Sample Notification #3 w/ Image</button>
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. </body>

  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. function

    ask_permission(

    )

    {
  3. if

    (

    !

    (

    "Notification"

    in

    window)

    )

    {
  4. alert(

    "Your current browser does not support desktop notification!"

    )

    ;
  5. console.error

    (

    "Your current browser does not support desktop notification!"

    )

    ;
  6. }

    else

    {
  7. console.log

    (

    Notification.permission

    )
  8. if

    (

    Notification.permission

    !==

    'granted'

    &&

    Notification.permission

    !==

    'denied'

    )

    {
  9. Notification.requestPermission

    (

    )

    .then

    (

    (

    permission)

    =>

    {
  10. if

    (

    permission ===

    'granted'

    )

    {
  11. alert(

    "Notification Permission is Granted"

    )
  12. }

    else

    if

    (

    permission ===

    'denied'

    )

    {
  13. alert(

    "Notification Permission is Denied"

    )
  14. }
  15. location.reload

    (

    )
  16. }

    )
  17. }
  18. }
  19. }
  20. ask_permission(

    )
  21. $(

    '#notif1'

    )

    .click

    (

    function

    (

    )

    {
  22. if

    (

    Notification.permission

    ===

    'default'

    )

    {
  23. ask_permission(

    )
  24. }

    else

    if

    (

    Notification.permission

    ===

    'granted'

    )

    {
  25. var

    notification =

    new

    Notification(

    "Sample Notification #1"

    ,

    {
  26. body:

    "This is a sample notification content or description 101."

    ,
  27. icon:

    'http://localhost/js_notification/test-icon.png'
  28. }

    )
  29. notification.onclick

    =

    (

    event)

    =>

    {
  30. event.preventDefault

    (

    )
  31. window.open

    (

    "https://sourcecodester.com"

    )
  32. }
  33. }

    else

    {
  34. alert(

    "Notification Permission is not granted."

    )
  35. }
  36. }

    )

  37. $(

    '#notif2'

    )

    .click

    (

    function

    (

    )

    {
  38. if

    (

    Notification.permission

    ===

    'default'

    )

    {
  39. ask_permission(

    )
  40. }

    else

    if

    (

    Notification.permission

    ===

    'granted'

    )

    {
  41. var

    notification =

    new

    Notification(

    "JavaScript Tutorials"

    ,

    {
  42. body:

    "Here are some tutorials of using JavaScript for IT/CS Students or beginners."

    ,
  43. icon:

    'http://localhost/js_notification/test-icon.png'
  44. }

    )
  45. notification.onclick

    =

    (

    event)

    =>

    {
  46. event.preventDefault

    (

    )
  47. window.open

    (

    "https://www.sourcecodester.com/tutorial/javascript"

    )
  48. }
  49. }

    else

    {
  50. alert(

    "Notification Permission is not granted."

    )
  51. }
  52. }

    )
  53. $(

    '#notif3'

    )

    .click

    (

    function

    (

    )

    {
  54. if

    (

    Notification.permission

    ===

    'default'

    )

    {
  55. ask_permission(

    )
  56. }

    else

    if

    (

    Notification.permission

    ===

    'granted'

    )

    {
  57. var

    notification =

    new

    Notification(

    "JavaScript Tutorials"

    ,

    {
  58. 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."

    ,
  59. icon:

    'http://localhost/js_notification/test-icon.png'

    ,
  60. image:

    'https://www.sourcecodester.com/site...images/oretnom23/JS-scoreboard-basketball.jpg'
  61. }

    )
  62. notification.onclick

    =

    (

    event)

    =>

    {
  63. event.preventDefault

    (

    )
  64. window.open

    (

    "https://www.sourcecodester.com/java...-using-javascript-and-jquery-free-source-code"

    )
  65. }
  66. }

    else

    {
  67. alert(

    "Notification Permission is not granted."

    )
  68. }
  69. }

    )
  70. }

    )

Sample Snapshots

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

Application Main Page

Sample Notification

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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top