WEB Notification 範例

Web Notification 做法如下,你還可以透過Google Tag Manager 把這段Code 部署上去。
// Check and Get Permission
if (Notification && Notification.permission !== "granted") {
Notification.requestPermission(function (status) {
if (Notification.permission !== status) {
Notification.permission = status;
}
});
}
//
function spawnNotification(theBody,theTitle,opneurl) {
var options = {
body: theBody,
requireInteraction: true,
}
var n = new Notification(theTitle,options);
n.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open(opneurl, '_blank');
}
}
var __t ;
function showNotification() {
spawnNotification("Hi","","https://davidelin.blogspot.com");
clearTimeout(__t)
}
//Check Permission
//if we had permission, we show the Notification after 10 Sec, else we do not do anything.
if (Notification && Notification.permission === "granted") {
__t = setTimeout("showNotification()",10000);
} else if (Notification && Notification.permission !== "denied") {
Notification.requestPermission(function (status) {
if (Notification.permission !== status) {
Notification.permission = status;
}
if (status === "granted") {
__t = setTimeout("showNotification()",10000);
}
});
}


留言

此網誌的熱門文章

nginx 上安裝 TWCA 憑證

PSPAD / NOTEPAD++ 好用功能