Notification with Audio in JavaScript

Walter Nascimento
Walter Nascimento | EN
2 min readNov 29, 2021

Display notification using javascript only

[Clique aqui para ler em português]

We always need a feedback to our user about some information, whether it’s a finalized registration or something removed, and today one of the best ways to give feedback to our users is with notification, so let’s create a notification using javascript and stay more professional we will add an audio whenever the notification is called.

Code

First let’s create the interface, we’ll do something simple, using just HTML.

HTML Code

To display our notification, let’s just create one button.

JS Code

In our javascript code we have our constants that have the message title, the message an icon and an audio file.

For the notification, a function called notifyMe was created that will be executed whenever we click on the button.

In the notifyMe function, we first check if the browser supports notification, then we check if the user has already allowed to be notified, and if not, then we check if he hasn’t denied it either, and then the notification api itself makes the request to the user checking whether or not he authorizes the sending of notification.

Finally, we have the callNotify function in which the notification is displayed with the title message and icon that we defined, and we still use the browser’s own audio api so that whenever this function is called, play in the audio file.

ready simple like that.

Demo

See below for the complete working project.

Youtube

If you prefer to watch it, see the development on youtube.

Thanks for reading!

If you have any questions, complaints or tips, you can leave them here in the comments. I will be happy to answer!

😊😊 See you later! 😊😊

--

--