用HTML5实现桌面提醒功能的一个实例代码


本文摘自PHP中文网,作者零下一度,侵删。

桌面提醒的介绍

桌面通知功能能够让浏览器即使是最小化状态也能将消息通知给用户。这和WebIM是最为天然的结合。不过,目前支持Desktop Notification功能的浏览器只有Chrome5+。在实际使用的过程中,应该尽量减少通知功能对用户的干扰,最大程度的减少通知功能的出现,这就需要解决以下几个问题:

  • 1. 收到多条消息时确保只出现一条通知;

  • 2. 当用户处于IM出现的页面中时(页面处于Focus状态)将不出现通知;

  • 3. 当用户使用多Tab开启多个存在IM的页面时,只要有一个页面处于Focus状态将不出现通知;

  • 4. 如何让用户点击通知浮动层即可定位到具体的聊天窗口

  • 5.此外,还需要解决一个便利性问题

桌面提醒Notification的API

window.webkitNotifications

  • requestPermission 请求通讯许可

  • checkPermission 检查通讯许可

  • createNotification 创建通讯

  • show 显示通知

代码实现

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

<!DOCTYPE HTML>

<html>

<head

<meta charset="gbk"

<title>Creating OS notifications in HTML5</title>

</head>

<body>

<input type="button" value="验证授权" onclick="init();" />

<input type="button" value="弹出消息" onclick="notify();" />

 

    <script type="text/javascript">

        const miao = 5;

 

        function init() {

            if (window.webkitNotifications) {

                window.webkitNotifications.requestPermission();

            }

        }

 

        function notify() {

            var icon = "logo.png";

            var title = "Hello World";

            var body =  "You Are My World (5秒后自动关闭)";

 

            if (window.webkitNotifications) {

                if (window.webkitNotifications.checkPermission() == 0) {

                    var popup = window.webkitNotifications.createNotification(icon, title, body);

                    popup.ondisplay = function(event) {

                        setTimeout(function() {

                            event.currentTarget.cancel();

                        }, miao * 1000);

                    }

                    popup.show();

                    popup.show();

                } else {

                    window.webkitNotifications.requestPermission();

                    return;

                }

            }

        }

    </script>

 

</body>

</html>

需要学习html5的同学请关注php中文网html5视频教程,众多html5在线视频教程可以免费观看!

以上就是用HTML5实现桌面提醒功能的一个实例代码的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

HTML5能干什么的

HTML5语义化总结

HTML5转义实体字符的使用详解

HTML5 source标签怎么用?HTML5 source标签属性介绍

html中序列化标签的简单介绍(代码实例)

利用HTML5实现文件异步上传功能代码实例

HTML5新增标签使用方法

HTML5 track标签是什么意思?HTML5 track标签的使用方法介绍

HTML5边玩边学(二)-基础绘图

移动端页面头部固定定位的绝对定位实现(代码示例)

更多相关阅读请进入《HTML5》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...