Chrome v35 desktop notification popup - javascript

Is there any fix yet for chrome version 35 desktop notification pop up ?
I have tried this also but no success.
Here is my code of html file
<html>
<head>
<title>Simple Notification example</title>
</head>
<button onclick="notifyMe()">Notify me!</button>
<script type="text/javascript">
function notifyMe() {
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
else if (Notification.permission === "granted") {
var notification = new Notification("Hi there!");
}
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
if(!('permission' in Notification)) {
Notification.permission = permission;
}
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
}
</script>
</html>
This code is asking to "allow" or "deny" notification pop up. But not working after it.
Anyone else experiencing this problem or any fix of this?

You forgot to show the notification.
var notification = new Notification("Hi there!");
notification.show();
I have tested the code an works fine.
Optionally you can register the onclick event to dismiss notification:
notification.onclick = function () {
notification.close();
}

Please check allow or block status in browser. copy the following url and paste into your chrome browser.
chrome://settings/contentExceptions#notifications

Related

window.open not working properly on Firefox 55.0.1

I'm trying to add Desktop Notifications to my site using the W3C Notifications API, documented at MDN. For an initial test and playing around, I have been using this fiddle I got from this question on Stackoverflow. All works out fine, except when the notification shows up and I click on it while using Firefox, the landing URL opens in a new tab and immediately Firefox switches to the page from which the notification was triggered.
This weird behavior was not seen on Chrome. It works just fine there. What is causing this behavior? Am I doing something wrong?
Here's the code from that fiddle:
<button onclick="notifyMe()">Notify me!</button>
<script>
// 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("http://stackoverflow.com/a/13328397/1269037");
};
}
}
</script>
Replace:
window.open("http://stackoverflow.com/a/13328397/1269037");
with:
var myWindow = window.open("http://stackoverflow.com/a/13328397/1269037");
myWindow.focus();

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.

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