婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > html5桌面通知(Web Notifications)實例解析

html5桌面通知(Web Notifications)實例解析

熱門標簽:智能芯電話機器人 咸陽穩定外呼系統軟件 小朱地圖標注 地圖標注柱狀圖 怎么做百度地圖標注 400開頭的電話好申請不 臨海地圖標注app 百度地圖標注為什么總是封號 四川移動電銷外呼客戶管理系統

html5桌面通知(Web Notifications)對于需要實現在新消息入線時,有桌面通知效果的情況下非常有用,在此簡單介紹一下這個html5的新屬性。

這里有個不錯的demo:html5 web notification demo

從上面這個demo中 我們就可以獲取所需要的基本核心代碼,如下:


復制代碼
代碼如下:
<script>
var Notification = window.Notification || window.mozNotification || window.webkitNotification;

Notification.requestPermission(function (permission) {
// console.log(permission);
});

function show() {
var instance = new Notification(
"test title", {
body: " test message"
}
);

instance.onclick = function () {
// Something to do
};
instance.onerror = function () {
// Something to do
};
instance.onshow = function () {
// Something to do
};
instance.onclose = function () {
// Something to do
};

return false;
}
</script>

 
其中:Notification.requestPermission 這句代碼的功能就是向用戶請求權限允許

通過以上的例子,基本思路我們已經有了,首先加載文檔時,就向用戶請求權限,獲取權限后以后都so easy了。


復制代碼
代碼如下:
window.addEventListener('load', function () {
// At first, let's check if we have permission for notification
if (Notification && Notification.permission !== "granted") {
Notification.requestPermission(function (status) {
if (Notification.permission !== status) {
Notification.permission = status;
}
});
}
});

火狐下 驗證是通過的,但是在chrome下總是出不來,后來發現這樣一段話


復制代碼
代碼如下:
Not a Bug, Feature.

Desktop Notifications can only be triggered via a user action. Typing into the
JavaScript console has the same effect as raw javascript code embedded into the web
page (no user action). Typing the javascript into the location bar, however,
represents a user-action (the user is intentionally visiting a javascript link to
enable notifications, probably for sites that tend to use href="javascript:" instead
of onclick="".

I'm pretty sure this is a non-issue.

原來在chrome下是必須要用戶手動觸發的,否則,chrome瀏覽器會無視這段的js

但是在我們網站里肯定不可能加一個按鈕或者超鏈接來顯式的讓用戶授權吧,好吧, 實際上這也不是個事情,我們可以在用戶經常點的按鈕上順便處理下這個授權就好,在chrome下是一次授權終身有用。除非你進入設置把他禁了。

整合一下,代碼如下:


復制代碼
代碼如下:
function showMsgNotification(title, msg){
var Notification = window.Notification || window.mozNotification || window.webkitNotification;

if (Notification && Notification.permission === "granted") {
var instance = new Notification(
title, {
body: msg,
icon: "image_url"
}
);

instance.onclick = function () {
// Something to do
};
instance.onerror = function () {
// Something to do
};
instance.onshow = function () {
// Something to do
// console.log(instance.close);
setTimeout(instance.close, 3000);
};
instance.onclose = function () {
// Something to do
};
}else if (Notification && Notification.permission !== "denied") {
Notification.requestPermission(function (status) {
if (Notification.permission !== status) {
Notification.permission = status;
}
// If the user said okay
if (status === "granted") {
var instance = new Notification(
title, {
body: msg,
icon: "image_url"
}
);

instance.onclick = function () {
// Something to do
};
instance.onerror = function () {
// Something to do
};
instance.onshow = function () {
// Something to do
setTimeout(instance.close, 3000);
};
instance.onclose = function () {
// Something to do
};

}else {
return false
}
});
}else{
return false;
}

}

標簽:山南 公主嶺 平涼 陜西 平頂山 黃石 黃石 南平

巨人網絡通訊聲明:本文標題《html5桌面通知(Web Notifications)實例解析》,本文關鍵詞  html5,桌面,通知,Web,Notifications,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《html5桌面通知(Web Notifications)實例解析》相關的同類信息!
  • 本頁收集關于html5桌面通知(Web Notifications)實例解析的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 海南省| 南和县| 禄丰县| 中卫市| 酒泉市| 香港 | 巢湖市| 凉山| 乳山市| 金溪县| 赞皇县| 中山市| 红安县| 襄樊市| 昭通市| 威宁| 合山市| 大洼县| 临高县| 贡嘎县| 秭归县| 封丘县| 长寿区| 铜陵市| 阳城县| 徐汇区| 敦化市| 资阳市| 盱眙县| 瓮安县| 舒兰市| 贵阳市| 衡水市| 桂东县| 高尔夫| 鄂温| 盐池县| 江永县| 工布江达县| 河北省| 乌鲁木齐市|