有什么方法可以显示来自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屋!
查看全文