有哪些方法可以显示来自 Web 应用程序的桌面通知? [英] What ways are out there to display a desktop notification from a web app?
本文介绍了有哪些方法可以显示来自 Web 应用程序的桌面通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做的是最好显示一个类似于通知的烤面包机,但任何将更新推送"到桌面的方法都很有趣.
What I would like to do is show a toaster like notification preferably but any method of "pushing" updates to the desktop is interesting.
谢谢
推荐答案
以下是 Chrome、Firefox、Opera 和 Safari 桌面通知的工作示例,复制自 Chrome 桌面通知示例.
在 JSBin 上实时试用.
Below is a working example of desktop notifications for Chrome, Firefox, Opera and Safari, copied from Chrome desktop notification example.
Try it live on JSBin.
// request permission on page load
document.addEventListener('DOMContentLoaded', function () {
if (Notification.permission !== "granted")
Notification.requestPermission();
});
function notifyMe() {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('Notification title', {
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
body: "Hey there! You've been notified!",
});
notification.onclick = function () {
window.open("https://stackoverflow.com/a/13328397/1269037");
};
}
}
<button onclick="notifyMe()">Notify me!</button>
<小时>
在我对 Chrome 桌面通知示例.
这篇关于有哪些方法可以显示来自 Web 应用程序的桌面通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文