濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > html5桌面通知(Web Notifications)實(shí)例解析

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

熱門標(biāo)簽:智能芯電話機(jī)器人 咸陽(yáng)穩(wěn)定外呼系統(tǒng)軟件 小朱地圖標(biāo)注 地圖標(biāo)注柱狀圖 怎么做百度地圖標(biāo)注 400開頭的電話好申請(qǐng)不 臨海地圖標(biāo)注app 百度地圖標(biāo)注為什么總是封號(hào) 四川移動(dòng)電銷外呼客戶管理系統(tǒng)

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

這里有個(gè)不錯(cuò)的demo:html5 web notification demo

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


復(fù)制代碼
代碼如下:
<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 這句代碼的功能就是向用戶請(qǐng)求權(quán)限允許。

通過以上的例子,基本思路我們已經(jīng)有了,首先加載文檔時(shí),就向用戶請(qǐng)求權(quán)限,獲取權(quán)限后以后都so easy了。


復(fù)制代碼
代碼如下:
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;
}
});
}
});

火狐下 驗(yàn)證是通過的,但是在chrome下總是出不來,后來發(fā)現(xiàn)這樣一段話


復(fù)制代碼
代碼如下:
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下是必須要用戶手動(dòng)觸發(fā)的,否則,chrome瀏覽器會(huì)無視這段的js

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

整合一下,代碼如下:


復(fù)制代碼
代碼如下:
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;
}

}

標(biāo)簽:山南 公主嶺 陜西 平頂山 黃石 黃石 南平

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《html5桌面通知(Web Notifications)實(shí)例解析》,本文關(guān)鍵詞  html5,桌面,通知,Web,Notifications,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《html5桌面通知(Web Notifications)實(shí)例解析》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于html5桌面通知(Web Notifications)實(shí)例解析的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    丹凤县| 英吉沙县| 晋中市| 毕节市| 陆河县| 汶川县| 广昌县| 晋州市| 筠连县| 忻城县| 金门县| 神木县| 郁南县| 余干县| 云阳县| 和硕县| 株洲市| 大英县| 武穴市| 青铜峡市| 恩施市| 嘉义县| 正定县| 盐山县| 平陆县| 商城县| 高安市| 汉寿县| 临海市| 张家口市| 东光县| 台南市| 冕宁县| 湛江市| 上杭县| 安塞县| 许昌县| 探索| 庐江县| 长阳| 台东市|