WEB Notification 範例
Web Notification 做法如下,你還可以透過Google Tag Manager 把這段Code 部署上去。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
}); | |
} |
留言
發佈留言