Use the HTML5 Notification API to create a web Notification tutorial, html5notification
This article describes how to use the HTML5 Notification API to create web notifications. Examples include the CSS and Javascript code to be used. For more information, see
A prompt box is displayed at the bottom right of the screen whenever you receive a new email when using Gmail for the web page. With the Notification API provided by html5.
Ensure browser support
If you develop on a browser of a specific version, I suggest you first go to caniuse to check the browser's support for Notification APIs, this prevents you from wasting your time on an unusable API.
How to start
Copy the content to the clipboard using JavaScript Code
- Var notification = new Notification ('notification title ',{
- Body: 'Your message'
- });
The above code constructs a simple notification bar. The first parameter of the constructor sets the title of the notification bar, and the second parameter is an option object. The following attributes can be set for this object:
- Body: Set the body of the notification bar.
Dir: defines the display direction of the text in the notification bar. It can be set to auto (auto), ltr (left to right), and rtl (Right to left ).
Lang: language used in the notification bar. The value of this attribute must belong to the BCP 47 language tag .)
Tag: assign an ID value to the notification bar to facilitate searching, replacing, or removing the notification bar.
Icon: Set the URL of the image as the icon in the notification bar.
Get Permissions
You need to apply for permissions to the user before displaying the notification bar. The notification bar can appear on the screen only when the user permits it. The following return values apply for permission application processing:
- Default: the user's processing result is unknown, so the browser will regard the user as rejecting the pop-up notification bar. ("Browser: I will not notify you if you do not require notification ")
Denied: the notification bar is displayed. ("User: scroll out of my screen ")
Granted: you can bring up a notification bar. ("User: Welcome! I'm glad to be able to use this notification feature ")
Copy the content to the clipboard using JavaScript Code
- Notification. requestPermission (function (permission ){
- // Display notification here making use of constructor
- });
Create a button with HTML
Copy XML/HTML Code to clipboard
- <Button id = "button"> Read your notification </button>
Do not forget CSS
Copy the content to the clipboard using CSS Code.
- # Button {
- Font-size: 1.1rem;
- Width: 200px;
- Height: 60px;
- Border: 2px solid # df7813;
- Border-radius: 20px/50px;
- Background: # fff;
- Color: # df7813;
- }
- # Button: hover {
- Background: # df7813;
- Color: # fff;
- Transition: 0.4 s elapsed;
- }
All Javascript code is as follows:
Copy the content to the clipboard using JavaScript Code
- Document. addEventListener ('domainloaded', function (){
- Document. getElementById ('click'). addEventListener ('click', function (){
- If (! ('Notification' in window )){
- Alert ('Sorry bro, your browser is not good enough to display notification ');
- Return;
- }
- Notification. requestPermission (function (permission ){
- Var config = {
- Body: 'Thanks for clicking that button. Hope you liked .',
- Icon: 'https: // ingress ',
- Dir: 'auto'
- };
- Var notification = new Notification ("Here I am! ", Config );
- });
- });
- });
From this code, we can see that if the browser does not support the Notification API, a warning will appear when you click the button. Sorry, sorry. Your browser does not support the notification function very well. "(Sorry bro, your browser is not good enough to display notification ). Otherwise, after obtaining the user's permission, our self-made notification bar can appear on the screen.
Why do you need to manually close the notification bar?
For this problem, we can use the setTimeout Function to set a time interval so that the notification bar can be closed at regular intervals.
Copy the content to the clipboard using JavaScript Code
- Var config = {
- Body: 'Today too your guys got eyes on me, you did the same thing. Thanks ',
- Icon: 'icon.png ',
- Dir: 'auto'
- }
- Var notification = new Notification ("Here I am! ", Config );
- SetTimeout (function (){
- Notification. close (); // closes the notification
- },5000 );
That's what we should say. If you want to learn more about the Notification API, read the following page:
MDN
Paul lund's tutorial on notification API
View the demo on CodePen
You can see the demo written by Prakash (@ imprakash) on CodePen.