浏览器通知提醒 聊天提醒 Notification
发布在经验and插件2017年12月8日view:527
在文章任何区域双击击即可给文章添加【评注】!浮到评注点上可以查看详情。
var notify = function(title, content, icon, jetchat) {
"use strict";
// Notification not supporte;};



function openNotice() { 
if ("Notification" in window) {
    (function() {
        function handler() {
            console.log("请求通知许可");
            window.removeEventListener("click", handler);
            try {
                Notification.requestPermission();
            } catch (e) {
                console.log("请求许可通过onclick失败:{ }");
                if ("webkitNotifications" in window) {
                    //try webkit mode
                    try {
                        webkitNotifications.requestPermission();
                    } catch (e1) {
                        console.log("请求许可通过webkitNotifications失败:{ }");
                    };
                }
            }
        }
        try {
            Notification.requestPermission();
        } catch (e) {
            console.log("请求许可通过onclick失败:{ }");
        }

        window.addEventListener("click", handler);

        var timer = undefined;
        var notifications = [];
        var notificationsAlive = [];
        var lastNotifyTime = 0;
        var doNotify = function(title, content, icon, jetchat, key) {
            try {
                var option = {};
                if (content) {
                    option.body = content;
                }
                if (icon) {
                    option.icon = icon;
                }
                var notification = new Notification(title, option);
                window.setTimeout(function() {
                    this.close();
                }.bind(notification), 5000);
                notification.onclose = function() {
                    for (var i = 0; i < notificationsAlive; i++) {
                        if (notificationsAlive[i] === notification) {
                            notificationsAlive.splice(i, 1);
                        }
                    }
                }.bind(notification);
                notification.onclick = function(jetchat, key) {
                    console.log("切换到聊天通知{ }");
                    window.open("baidu.com");
                    /*jetchat.openChatWith(key);
                    window.focus();*/
                }.bind(this, jetchat, key);
                notificationsAlive.push(notification);
                while (notificationsAlive.length > 3) {
                    var toRemove = notificationsAlive.shift();
                    try {
                        toRemove.close();
                    } catch (e) {}
                }
                return notification;
            } catch (e) {
                console.log("Notification error: {"+e+"}");
                return undefined;
            }
            timer = undefined;
        }

        notify = function(title, content, icon, jetchat, key) {
            if (timer) {
                clearTimeout(timer);
                timer = undefined;
            }
            if (notificationsAlive.length >= 3) {
                timer = setTimeout(doNotify.bind(this, title, content, icon, jetchat, key), 300);
            } else {
                doNotify(title, content, icon, jetchat, key);
            }
        };
    })();
}

}

评论
发表评论
暂无评论
WRITTEN BY
古道川
技法无高低、功力有深浅。
TA的新浪微博
PUBLISHED IN

我的收藏