Why do we need HTML5 desktop notificationsTraditional 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
- >
-
<html>
-
<head>
-
<meta charset="utf- 8">
-
<title>html5 desktop notification title>
-
head>
-
<body>
-
<input type="button" value="Turn on desktop notifications" onclick ="showNotice();">
-
<script>
- function showNotice(){
- Notification.requestPermission(function(status){
- //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
- if("granted" != status)
- return;
-
var notify = new Notification("Message",{
- dir:'auto',
- lang:'zh-CN',
- tag:'sds',//The id of the instantiated notification
- //icon supports ico, png, jpg, jpeg formats
- icon:'http://www.xttblog.com/icons/favicon.ico',//Thumbnail image of notification
- body:'html5 desktop notification' //The specific content of the notification
- });
-
notify.onclick=function(){
- //If the notification message is clicked, the notification window will be activated
- window.focus();
- }
- });
- }
-
script>
-
body>
-
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