多个选项卡上的Webkit通知 [英] Webkit Notifications on Multiple Tabs
问题描述
我为我的应用使用了 WebKit通知。说:如果我使用这个代码:
$ pre $ var $ n = window.webkitNotifications.createNotification(
'icon.png' ,
'新评论',
'Praveen评论您的帖子!'
);
n.onclick = function(x){window.focus(); this.cancel(); };
n.show();
PS 1:前五行实际上是单个线。为了便于阅读,我已经发布了这种方式。
无法使用Google Chrome显示桌面通知。我的问题是,如果我打开了多个选项卡,该怎么办?
说这是不是当我的应用出现新评论时会被解雇。如果我打开了多个选项卡,该怎么办?这会产生很多通知吗?比方说,我打开了 10 - 15
标签,并且我收到了两封通知。将生成多少个通知, 20 - 30
?
如果是这种情况,如何防止生成每个打开的选项卡多次发送单个通知?
代码摘录[仅用于文档下载]
HTML
<按钮>通知我!< / button>
JS
window.addEventListener('load',function(){
//首先,让我们检查一下我们是否有通知
的许可//如果没有,我们要求它$ b $如果(通知&& Notification.permission!==授予){
Notification.requestPermission(function(status){
if(Notification.permission!== status){
Notification.permission = status;
}
});
}
var button = document.getElementsByTagName('button')[0];
button.addEventListener('click',function(){
//如果用户同意得到通知
// //让我们尝试发送10个通知
if(Notification& amp ;& Notification.permission ===granted){
for(var i = 0; i <10; i ++){
//感谢这个标签,我们只能看到 Hi!9通知
var n =新通知(Hi!+ i,{tag:'soMan yNotification'});
}
}
//如果用户没有告诉他是否要通知
//注意:由于Chrome,我们不确定是否设置了权限属性
//,因此检查默认值是不安全的。
else if(Notification& Notification.permission!==denied){
Notification.requestPermission(function(status){
if(Notification.permission!== status) {
Notification.permission = status;
}
//如果用户说好了
if(status ===granted){
对于(var i = 0; i <10; i ++){
//感谢标签,我们只应该看到Hi!9通知
var n = new Notification(Hi! + i,{tag:'soManyNotification'});
}
}
//否则,我们可以回退到常规模态警报
else {
alert(Hi!);
}
});
}
//如果用户拒绝通知
else {
//我们可以退回到常规模态警报
alert(Hi !);
}
});
});
I am using WebKit Notifications for my app. Say if I am using this code:
var n = window.webkitNotifications.createNotification(
'icon.png',
'New Comment',
'Praveen commented on your post!'
);
n.onclick = function(x) { window.focus(); this.cancel(); };
n.show();
PS 1: The first five lines are actually a single line. Just for readability I have posted this way.
PS 2: For the full code, please see this: Unable to show Desktop Notifications using Google Chrome.
My question is, what if I have more than one tab opened?
Say if this is gonna get fired when a new comment appears on my app. What if I have more than one tab open? Will this generate many notifications? Say, I have 10 - 15
tabs open and I get two notifications fired. How many notifications will be generated, 20 - 30
?
If that is the case, how to prevent generation of a single notification multiple times for each opened tab?
A detailed explanation of Tagging notifications so only the last one appears is available on the MDN docs site
An excerpt of the code [just in case the docs go down]
The HTML
<button>Notify me!</button>
The JS
window.addEventListener('load', function () {
// At first, let's check if we have permission for notification
// If not, let's ask for it
if (Notification && Notification.permission !== "granted") {
Notification.requestPermission(function (status) {
if (Notification.permission !== status) {
Notification.permission = status;
}
});
}
var button = document.getElementsByTagName('button')[0];
button.addEventListener('click', function () {
// If the user agreed to get notified
// Let's try to send ten notifications
if (Notification && Notification.permission === "granted") {
for (var i = 0; i < 10; i++) {
// Thanks to the tag, we should only see the "Hi! 9" notification
var n = new Notification("Hi! " + i, {tag: 'soManyNotification'});
}
}
// If the user hasn't told if he wants to be notified or not
// Note: because of Chrome, we are not sure the permission property
// is set, therefore it's unsafe to check for the "default" value.
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") {
for (var i = 0; i < 10; i++) {
// Thanks to the tag, we should only see the "Hi! 9" notification
var n = new Notification("Hi! " + i, {tag: 'soManyNotification'});
}
}
// Otherwise, we can fallback to a regular modal alert
else {
alert("Hi!");
}
});
}
// If the user refuses to get notified
else {
// We can fallback to a regular modal alert
alert("Hi!");
}
});
});
这篇关于多个选项卡上的Webkit通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!