Home Web Front-end H5 Tutorial HTML 5的消息通知机制

HTML 5的消息通知机制

Mar 21, 2017 pm 04:41 PM

 HTML 5 已经被应用到Web开发中。跟平常一样,任何一个软件新版本的发布都期待一些新的特性,这对HTML 5也不例外。为了仅仅通过HTML提高用户交互,HTML 5已经提供了很多新的API。
     是不是非常有趣呢?并且HTML 5已经和CSS 3结合的非常棒了。
     因此,我也开始写一系列与HTML 5的API相关的文章来介绍API的使用和功能,例如Geolocation, Notification, File, 等等,第一篇文章是和Notification API相关。
     首先要意识到是,HTML 5的特性和API仅能在支持HTML 5的浏览器中正常使用,如果浏览器不支持HTML 5,就不能使用HTML 5在网页中实现任何一个功能了。现在就来了解一下notification API吧。
     什么是HTML 消息通知?
     HTML消息通知是指我们可以告诉用户一个确定的事件行为,即使此时用户在浏览器的另一个选项卡。这个通知对于New Mail, New Tweet等事件是非常有用的。
     怎么使用?

要使用Notification API,有几个步骤:首先,要从用户那里获取显示通知的许可,只有当用户允许时,才能显示通知给用户。所以先要征求用户的许可而不是直接显示通知。然后,

获取用户许可之后,我们可以显示两种类型的信息:

1

2

Normal/Default Notification

HTML Notification

Copy after login

最后执行通知代码。

我已经创建了一个显示通知的JavaScript函数,注意:函数仅限用于这篇文章,因为有很多种方式可以按照每个人的需求去扩展。

HTML:

1

2

3

4

5

6

7

8

9

<h2>Show Normal Notification</h2>

<button class="btn btn-success" id="show_notification">

    Normal Notification

</button>

    

<h2>Show HTML Notification</h2>

<button class="btn btn-success" id="show_html_notification">

    HTML Notification

</button>

Copy after login

JavaScript:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

// Function to show Notification

function createNotification(type)

{

   if(type ==  &#39;&#39;)

     type = &#39;normal&#39;;

                       

   if(type != &#39;html&#39;)

   {

    var title ="You have received HTML 5 Notification";

    var msg="Content Goes Here......";

    var notification = window.Notifications.createNotification("1.png", title, msg);

   }

   else

   {

     var notification = window.Notifications.createHTMLNotification(&#39;content.html&#39;);

   }

   notification.show();

}

 

// Binding the Click event on buttons.

 

$(document).ready(function ()

{       

  if (window.webkitNotifications)

  {

   window.Notifications = window.webkitNotifications;

   $(&#39;#show_notification&#39;).click(function ()

   {

     if (window.Notifications.checkPermission() == 0)

     {

       createNotification(&#39;normal&#39;);

     }

     else

     {

       window.Notifications.requestPermission();

     }

   });

                               

   $(&#39;#show_html_notification&#39;).click(function ()

   {

    if (window.Notifications.checkPermission() == 0)

    {

       createNotification(&#39;html&#39;);

    }

    else

    {

       window.Notifications.requestPermission();

    }

   });

  }   

  else

  {

   alert(&#39;HTML 5 Notifications are not supported on this browser/OS.&#39;);

  }

});

Copy after login

     总结
     HTML 5 notification适用于像Google Chrome一样的Web Kit浏览器,对于HTML 5 notification,Mozilla Firefox有其自己的私有属性。我将在其它文章中介绍。

以上就是HTML 5的消息通知机制的内容,更多相关内容请关注PHP中文网(www.php.cn)!

相关文章:

Redis消息通知系统的实现

web消息通知系统设计问题

基于HTML5 Notifications API的消息通知插件

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

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

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

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.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

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

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

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

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

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

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

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

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

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

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

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

See all articles