New HTML5 features-

Source: Internet
Author: User
Webconfigurications is an amazing new feature in HTML5. It supports developers to configure and display desktop notifications to provide users with a better experience, even if you are busy with other work, you can receive notifications from the page, such as a reminder of a new email or ...,. I have been busy with my work and haven't updated my blog for a long time. I may take a look and find many friends leaving a message below. I am glad to write these valuable articles at that time, till now, it can help so many friends solve some Android problems.

I recently studied some new HTML5 technologies and want to write them down. On the one hand, I can deepen my understanding of the technologies and hope to help beginners understand and learn these technologies.

Today, we will explain the desktop notification technology, Web communications.

Web communications is an amazing new feature in HTML5. It supports developers to configure and display desktop Notifications to provide users with a better experience, even if you are busy with other work, you can receive notifications from the page, such as a reminder for a new email or a Message notification received in an online chat room.

Next, we will try to create our own message notifications step by step.

To create a Message Notification, first create a message box, which is very simple. Use the Notification class under the window object directly. The Code is as follows:

var n = new Notification("sir, you got a message", {icon: 'img/icon.png',body: 'you will have a meeting 5 minutes later.'});

In the constructor of the Notification class, there are two important parameters: the first is the message title, and the second is the message body object, including the icon of the message box) and message content (body ).

After the above code is executed, we have successfully created a message box instance. In Chrome, it is displayed as follows:

At this point, we have already succeeded in half, but whether the message box can be correctly displayed depends on the user's authorization. In view of the browser's security mechanism, the Message notification can be displayed only when the user agrees to the pop-up message notification box on the webpage. So now we need to apply for user authorization.

The Notification class provides a requestPermission method to request user authorization. The Code is as follows:

Notification. requestPermission (function (status) {// status indicates the authorization status. If you allow display of desktop notifications, the status is 'granted' console. log ('status: '+ status); // permission read-only attribute var permission = Notification. permission; // The default user does not receive or reject authorization requests. the user cannot display a notification. // The granted user accepts the authorization request. The notification console can be displayed. log ('permission: '+ permission );});


When this code is executed, the browser will ask the user whether to allow the site to display message notifications, as shown in:

If the user clicks the Block button on the left, no matter how we create the Notification instance, the message cannot be displayed. Only the user selects the Allow button, the code can be correctly executed and a message box is displayed.

As described in the above Code, after the requestPermission function is executed, it enters a callback function, which can pass a status parameter, indicating that after the user makes the selection, the final authorization status. If you click the close button on the right of the authorization prompt, it is equivalent to ignoring the authorization request. The status is default and the message cannot be displayed in the default status; if the user clicks the Block button to reject the authorization request, the status is denied, and the message cannot be displayed. If the user clicks the Allow button to accept the authorization request, in this case, the status will change to the granted State. The message can be correctly displayed only in the granted state.

At the same time, after the authorization request is executed, the browser will grant the permission status to the Notification permission attribute, which is read-only for developers, its value is the same as the preceding status value. Therefore, if we want to display a Message notification, We can first determine whether we have the permission:

if (Notification.permission === 'granted') {//show notification}


As described above, when the permission is granted, we can display a Message notification. However, simply displaying a message box is not attractive, so message notifications should be interactive, and there should be event participation before and after the message is displayed. A series of event functions have been developed at the beginning of Notification. developers can use these functions to process user interaction in a very good way:

Var n = new Notification ("sir, you got a message", {icon: 'img/icon.png ', body: 'you will have a meeting 5 minutes later. '}); // The onshow function will be called when the message box is displayed. // some data records and scheduled operations can be performed. onshow = function () {console. log ('notification shows up'); // close the message box setTimeout (function () {n. close () ;}, 5000) ;}; // called when the message box is clicked // you can open the view and close operations such as the message box n. onclick = function () {alert ('Open the associated view'); // opening the view... n. close () ;}; // when an error occurs, the onerror function will be called. // if no granted authorization is available, the onerror function n will be executed when the Notification object instance is created. onerror = function () {console. log ('notification encounters an error'); // do something useful}; // when a message box is closed, the onclose function is called n. onclose = function () {console. log ('notification is closed '); // do something useful };

We can see that Notification has four common functions that can be used to process event interaction. The onshow function can be executed during message display. The onclick function can be called after a user clicks a message, the onclose function is called when the message box is closed, and the onerror function is called when an error occurs. As mentioned above, if the Message notification is not authorized, the onerror function is also executed. After understanding the applications of these functions, you can basically handle message events well.

Finally, we will organize these steps to form a simple example to better demonstrate this new feature.

First, create the following file structure:

Then we need to organize the code of the above steps to form a JavaScript Object, as shown in the following code:

Var icationicationhandler = {isnotifsuppsupported: 'notification' in window, isPermissionGranted: function () {return Notification. permission = 'granted';}, requestPermission: function () {if (! This. isNotificationSupported) {console. log ('the current browser does not support Notification API '); return;} Notification. requestPermission (function (status) {// status indicates the authorization status. If you allow display of desktop notifications, the status is 'granted' console. log ('status: '+ status); // permission read-only attribute var permission = Notification. permission; // The default user does not receive or reject authorization. The notification cannot be displayed. // The granted user accepts the authorization. The notification console can be displayed. log ('permission: '+ permiss Ion) ;}) ;}, showNotification: function () {if (! This. isNotificationSupported) {console. log ('the current browser does not support Notification API '); return;} if (! This. isPermissionGranted () {console. log ('the current page has not been granted for notification'); return;} var n = new notification ("sir, you got a message", {icon: 'img/icon.png ', body: 'you will have a meeting 5 minutes later. '}); // The onshow function will be called when the message box is displayed. // some data records and scheduled operations can be performed. onshow = function () {console. log ('notification shows up'); // close the message box setTimeout (function () {n. close () ;}, 5000) ;}; // called when the message box is clicked // you can open the view and close operations such as the message box n. onclick = function () {alert ('Open the associated view'); // opening the view... n. close () ;}; // when an error occurs, the onerror function will be called. // if no granted authorization is available, the onerror function n will be executed when the Notification object instance is created. onerror = function () {console. log ('notification encounters an error'); // do something useful}; // when a message box is closed, the onclose function is called n. onclose = function () {console. log ('notification is closed '); // do something useful };}; document. addEventListener ('load', function () {// try to request permission when page has been loaded. notificationHandler. requestPermission ();});

We can see that the above Code creates an NotificationHandler object to manage message-related event logic. Generally, our process is as follows: after loading the page, we call the requestPermission function to request user authorization, after the Page code is executed for a period of time, when a message needs to be displayed, call the showNotification function to display the message. For example:

setTimeout(function() {//if there has new mail, show notificationNotificationHandler.showNotification();}, 5000);

Note that not all browsers support Notification, So we added an isNotificationSupported attribute to identify whether message notifications are supported by the browser. In the code above, if it is identified that the browser does not support this API, it will return directly. Of course, in actual development, we can choose other forms to remind users.

Then we will refer to the index.html file:

Show notification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.