


Tutorial on making web notifications using HTML5 Notification API_html5 tutorial tips
When using the web version of Gmail, whenever a new email is received, a corresponding prompt box will pop up in the lower right corner of the screen. With the Notification API provided by HTML5, we can also easily implement such a function.
Make sure your browser supports it
If you are developing on a specific version of the browser, then I recommend that you first go to caniuse to check the browser's support for the Notification API to avoid wasting precious time on an unusable API.
How to get started
- 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, which can set the following properties:
- body: Set the body content of the notification bar.
dir: Define the display direction of the notification bar text, which can be set to auto (automatic), ltr (left to right), or rtl (right to left).
lang: Declares the language used for the text in the notification bar. (Annotation: The value of this attribute must belong to the BCP 47 language tag.)
tag: Assign an ID value to the notification bar to facilitate retrieval, replacement or removal of the notification bar.
icon: Set the URL of the image used as the notification bar icon
Get permission
Before displaying the notification bar, you need to apply for permission from the user. Only with the user's permission can the notification bar appear on the screen. The processing of permission application will have the following return value:
- Default: The user processing result is unknown, so the browser will regard the user as refusing to pop up the notification bar. ("Browser: You didn't ask for notification, so I won't notify you")
denied: The user refuses to pop up the notification bar. ("User: scroll away from my screen")
granted: The user allows the notification bar to pop up. ("User: Welcome! I'm excited to use this notification feature")
- Notification.requestPermission(function(permission){
- //display notification here making use of constructor
- });
Create a button using HTML
- <button id="button" >Read your notificationbutton>
Don’t forget CSS
- #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.4s ease;
- }
全部的Javascript代码如下:
- document.addEventListener('DOMContentLoaded',function(){
- document.getElementById('button').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://cdn2.iconfinder.com/data/icons/ios-7-style-metro-ui-icons/512/MetroUI_HTML5.png',
- dir:'auto'
- };
- var notification = new Notification("Here I am!",config);
- });
- });
- });
从这段代码可以看出,如果浏览器不支持Notification API,在点击按钮时将会出现警告“兄弟,很抱歉。你的浏览器并不能很好地支持通知功能”(Sorry bro, your browser is not good enough to display notification)。否则,在获得了用户的允许之后,我们自制的通知栏便可以出现在屏幕当中啦。
为什么要让用户手动关闭通知栏?
对于这个问题,我们可以借助setTimeout函数设置一个时间间隔,使通知栏能定时关闭。
- var config = {
- body:'Today too many 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);
该说的东西就这些了。如果你意犹未尽,希望更加深入地了解Notification API,可以阅读以下的页面:
MDN
Paul lund’s tutorial on notification API
在CodePen上查看demo
你可以在CodePen上看到由Prakash (@imprakash)编写的demo。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
