search
HomeWeb Front-endH5 TutorialHTML5 implements Notification API desktop notification function _html5 tutorial skills

Why do we need HTML5 desktop notifications
Traditional desktop notifications can be written as a div and placed in the lower right corner of the page to automatically pop up, and get messages through polling and other methods. Push to users. One disadvantage of this method is that when I use JD.com to shop, I don’t know that Renren has news pushed to me, but I have to wait until I switch the current page to Renren to know that there is news. Pushed. This method of message push is based on page survival, but we need such a strategy: no matter which page you are looking at, as long as there is a message, it should be pushed to me. This is the problem that webkitNotification wants to solve. The message generated by Notification is not attached to a certain page, only to the browser.
The normal process of generating a desktop notification
Let’s first take a look at how a desktop notification is generated:
1. Check whether the browser supports Notification
2. Check the browser’s notification permissions (whether notifications are allowed)
3. If the permissions are insufficient, obtain the browser’s notification permissions
4. Create message notifications
5. Display message notifications
NOTE : Regarding the first point, some explanation is needed. Notification has not been standardized yet, so it currently only supports chrome19 and safari6. There is information on the Internet that Firefox26 also supports it, but the results of my Firefox27 test are that it cannot be supported.
I believe everyone is familiar with desktop notifications of HTML5. Common applications include the web version of WeChat, which requires setting up the desktop notification function before they can be used.
It is not difficult to implement such a function using a client program. For the traditional web version of desktop notifications, you can write a div and place it in the lower right corner of the page to automatically pop up, and obtain messages through polling and other methods and push them to the user. One disadvantage of this method is that when I use Taobao to shop, I don’t know that there are messages pushed to me on Weibo, but I have to wait until I switch the current page to Sina Weibo to know that there are messages pushed to me. . This method of message push is based on page survival, but we need such a strategy: no matter which page you are looking at, as long as there is a message, it should be pushed to me. This is the problem that webkitNotification wants to solve.
Notification is not standardized yet, so you can’t learn it on websites such as w3cschool. However, most current mainstream browsers support Notification. The desktop notification rendering of html5 is as follows:


The code is as follows:
XML/HTML CodeCopy content to clipboard
  1. /span>> 
  2. html> 
  3. head> 
  4. meta charset="utf- 8"> 
  5. title>html5 desktop notification title> 
  6. head> 
  7. body> 
  8. input type="button" value="Turn on desktop notifications" onclick ="showNotice();"> 
  9. script> 
  10. function showNotice(){
  11. Notification.requestPermission(function(status){  
  12. //The default value of status 'default' is equivalent to rejection 'denied' means the user does not want notifications 'granted' means the user agrees to enable notifications
  13. if("granted" != status)
  14. return;
  15. var notify = new Notification("Message",{
  16. dir:'auto',
  17. lang:'zh-CN',
  18. tag:'sds',//The id of the instantiated notification
  19. //icon supports ico, png, jpg, jpeg formats
  20. icon:'http://www.xttblog.com/icons/favicon.ico',//Thumbnail image of notification
  21. body:'html5 desktop notification' //The specific content of the notification
  22. });
  23. notify.onclick=function(){ 
  24. //If the notification message is clicked, the notification window will be activated
  25. window.focus();
  26. }
  27. });
  28. }
  29. script> 
  30. body> 
  31. html>

The above content is the HTML5 implementation of Notification API desktop notification function shared by the editor. I hope it will be helpful to everyone!
Original text: http://www.xttblog.com/?p=249

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the H5 tag in HTML?What is the H5 tag in HTML?May 09, 2025 am 12:11 AM

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

H5 Code: A Beginner's Guide to Web StructureH5 Code: A Beginner's Guide to Web StructureMay 08, 2025 am 12:15 AM

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

H5 Code Structure: Organizing Content for ReadabilityH5 Code Structure: Organizing Content for ReadabilityMay 07, 2025 am 12:06 AM

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

H5 vs. Older HTML Versions: A ComparisonH5 vs. Older HTML Versions: A ComparisonMay 06, 2025 am 12:09 AM

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

H5 vs. HTML5: Clarifying the Terminology and RelationshipH5 vs. HTML5: Clarifying the Terminology and RelationshipMay 05, 2025 am 12:02 AM

The difference between H5 and HTML5 is: 1) HTML5 is a web page standard that defines structure and content; 2) H5 is a mobile web application based on HTML5, suitable for rapid development and marketing.

HTML5 Features: The Core of H5HTML5 Features: The Core of H5May 04, 2025 am 12:05 AM

The core features of HTML5 include semantic tags, multimedia support, form enhancement, offline storage and local storage. 1. Semantic tags such as, improve code readability and SEO effect. 2. Multimedia support simplifies the process of embedding media content through and tags. 3. Form Enhancement introduces new input types and verification properties, simplifying form development. 4. Offline storage and local storage improve web page performance and user experience through ApplicationCache and localStorage.

H5: Exploring the Latest Version of HTMLH5: Exploring the Latest Version of HTMLMay 03, 2025 am 12:14 AM

HTML5isamajorrevisionoftheHTMLstandardthatrevolutionizeswebdevelopmentbyintroducingnewsemanticelementsandcapabilities.1)ItenhancescodereadabilityandSEOwithelementslike,,,and.2)HTML5enablesricher,interactiveexperienceswithoutplugins,allowingdirectembe

Beyond Basics: Advanced Techniques in H5 CodeBeyond Basics: Advanced Techniques in H5 CodeMay 02, 2025 am 12:03 AM

Advanced tips for H5 include: 1. Use complex graphics to draw, 2. Use WebWorkers to improve performance, 3. Enhance user experience through WebStorage, 4. Implement responsive design, 5. Use WebRTC to achieve real-time communication, 6. Perform performance optimization and best practices. These tips help developers build more dynamic, interactive and efficient web applications.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software