How to remove all chrome notifications on one click in website? [duplicate] - javascript

This is my code to show notification in Google Chrome.
How can I close notification in code?
document.addEventListener('DOMContentLoaded', function() {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
});
function notifyMe() {
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('test', {
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
body: "test",
});
notification.onclick = function() {
window.open("http://stackoverflow.com/a/13328397/1269037");
};
}
}

It's simple, every notification object has close() method you need to just push them on to an array and call close() on each one of them before window close
var notify=[];
for(var i=0; i<=4;i++){
var notification = new Notification('test', {
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
body: "test"+i
}); //create some notifications
notify.push(notification);
}
function removeAllNotifys()
{
for(var i=0; i<notify.length;i++){
notify[i].close(); //remove them all
}
}
window.onbeforeunload = removeAllNotifys;
You can also associate removeAllNotifys() on some button click to clear all notification or use setTimeout to remove them say after 2 seconds .

Related

how to change the popup title in java script?

could you please tell me how to change the pop up title ..currently it is showing http:\\s.codepen.io can we change this url ?
http://codepen.io/anon/pen/WRaZOq
function notifyMe() {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Hi there!");
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
// At last, if the user has denied notifications, and you
// want to be respectful there is no need to bother them any more.
}Notification.requestPermission().then(function(result) {
console.log(result);
});function spawnNotification(theBody,theIcon,theTitle) {
var options = {
icon: theIcon
}
var n = new Notification(theTitle,options);
}
You need to have a look at the Notification constructor
First arg. is the title and the second arg. is an object specifying other details including body of the message.So you need something like this
var notification = new Notification("the title",{body:"body of the notification"});

Inconsistent/Delayed HTML5 Desktop Push Notifications on Chrome

I'm writing a chat webapp that needs to be able to trigger desktop push notifications through the notifications API: https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API
Unfortunately, it seems that the notifications don't show up until I flush it all out by making another notification with this fiddle: https://jsfiddle.net/yoshi6jp/Umc9A/
This is the code that I am using:
function triggerDesktopNotification() {
function makeNotification() {
var notification = new Notification('AppName', {
body: 'You have a new message!',
icon: '/favicon.ico',
});
notification.onclick = () => {
notification.close();
};
}
if (Notification.permission === 'granted') {
makeNotification();
}
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
if (permission === 'granted') {
makeNotification();
}
});
}
}
I can confirm that the code is executing properly by placing console.log() immediately after the new Notification call. Interestingly, if I put an alert() there instead, the notification shows up when I see the alert box (after navigating back into my tab).
If I understood you right ;
Alert interrupts program stack where it placed i think.Why dont you try to fire it async with setTimeout function like that ?
setTimeout( function(){
alert("asd");
})
edited js fiddle here

pushUp Notification not too frequent and browser allow

hello all i am using this code for browser/desktop PushUP Notification
document.addEventListener('DOMContentLoaded', function () {
if (Notification.permission !== "granted")
Notification.requestPermission();
var notification = window.Notification || window.mozNotification || window.webkitNotification;
notification.requestPermission(function(permission){});
});function notifyMe(titleisthi,contentbody,linktoopen) {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
} if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification(titleisthi, {
icon: 'avator/small-icon.png',
body: contentbody,
});
notification.onclick = function () {
window.open(linktoopen);
};}}
The Problem is that this code sometimes sends many notifications or its too frequent can there be something which dosent sends more than 2 notification within 30 minutes. and also users dont need to allow the browser to receive the notification or not.
any help is appreciated.
i know sending notifications too frequently depends on my code or calling the function but i was wondering if there is something which can prevent them globally.

Hide JS Notification Object By Tag Name

I'm using the notification API for my project to show browser notifications where each notification has a unique tag (ID), but I can't seem to find a way to close or hide the notification by the tag name, without calling the close function on the object, since it might be closed with other pages than where it was originated. Is this sort of thing possible?
You could save the notifications in localStorage and then retrieve it and close.
e.g.
// on create
var n = new Notification('Notification Title', {
tag: _this.attr('data-notification-id')
});
window.localStorage.setItem('data-notification-id', n);
and
// then later
var n = window.localStorage.getItem('data-notification-id');
n.close();
I've solved this now, but my solutions seems odd, so I'm still accepting other answers that follow a more "normal" approach.
Basically, a new notification object that is created with a tag while a notification that is currently already visible already has the same tag, the original notification is removed. So by creating a new notification object with the same tag and immediately removing it, I can "remove" the old notifications.
The link to view the notification
View this notification
And the jQuery
$('a[data-notification-id]').on('click', function(){
var _this = $(this);
var n = new Notification('Notification Title', {
tag: _this.attr('data-notification-id')
});
setTimeout(n.close.bind(n), 0);
});
You could stringify the notification options and save to session (or local) storage using the tag as the storage key. Then you can use the stored notification options to re-create/replace it and then call close.
Create the notification:
if (("Notification" in window)) {
if (Notification.permission === "granted") {
var options = {
body: sBody,
icon: sIcon,
title: sTitle, //used for re-create/close
requireInteraction: true,
tag: sTag
}
var n = new Notification(sTitle, options);
n.addEventListener("click", function (event) {
this.close();
sessionStorage.removeItem('notification-' + sTag);
}, false);
sessionStorage.setItem('notification-' + sTag, JSON.stringify(options));
}
}
Clear the notification:
function notificationClear(sTag) {
var options = JSON.parse(sessionStorage.getItem('notification-' + sTag));
if (options) {
options.requireInteraction = false;
if (("Notification" in window)) {
if (Notification.permission === "granted") {
var n = new Notification(options.title, options);
setTimeout(function () {
n.close();
sessionStorage.removeItem('notification-' + sTag);
}, 500); //can't close it immediately, so setTimeout is used
}
}
}
}

how to change the close time of the HTML5 desktop notification to not autoclose?

I am working with the HTML5 desktop notification. it's working well and give me proper output as per my requirements. Now, I want to display that notification until user close that notification manually how it.s possible my code is as following.
function notifyMe() {
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
else if (Notification.permission === "granted") {
var options = {
body: "due to your inactive response timer is stoped automatically. Start your timer again.",
dir : "ltr"
};
var notification = new Notification("Your timer is stop",options);
}
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
if (!('permission' in Notification)) {
Notification.permission = permission;
}
if (permission === "granted") {
var options = {
body: "due to your inactive response timer is stoped automatically. Start your timer again.",
dir : "ltr"
};
var notification = new Notification("Your timer is stop",options);
}
});
}
}
It stays indefinitely on Chrome. There's a bug in Firefox which auto-closes it after 4 seconds: https://bugzilla.mozilla.org/show_bug.cgi?id=875114
Try with "requireInteraction" in the option, will work in firefox and chrome both.
The code is:
var options = {
body: "due to your inactive response timer is stoped automatically. Start your timer again.",
dir : "ltr",
requireInteraction: true
};

Categories

Resources