Webkit notifications on multiple tabs

I am using WebKit Notifications for my application. Let's say if I use 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 one line. For readability, I published this method.

PS 2:. For full code see this: Unable to show desktop alerts using Google Chrome .

My question is, what if I have more than one tab open?

Tell me if this will be launched when a new comment appears in my application. What should I do if I have several tabs open? Will it generate a lot of notifications? Say I have a tab 10 - 15 open and I get two notifications. How many notifications will be generated, 20 - 30 ?

If so, how to prevent the creation of one notification several times for each open tab?

+7
source share
2 answers

A detailed explanation of tag notifications, so only the latter is available. on the MDN documentation site

Code snippet [just in case documents are omitted]

HTML

 <button>Notify me!</button> 

Js

 window.addEventListener('load', function () { // At first, let check if we have permission for notification // If not, let 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 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 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!"); } }); }); 
+5
source

You just need to specify the "tag" option for notification. Notifications with the same value in the tag are displayed only once, even if many tabs are open.

For example:

 var notification = new Notification('Hey!', { body : 'So nice to hear from you', tag : 'greeting-notify', icon : 'https://mysite.com/my_funny_icon.png' }); 
+16
source

All Articles