How I can enable browser notification when I'm using localhost, when writing in console Notification.requestPermission();
nothing appear but on any regular site write in console Notification.requestPermission();
the permission is requested.
How I can enable it to test my code ? Thank you very much in advance ...
Can I receive push notifications when my browser is not running? – No, web push notifications can not be received on a PC unless a browser is running.
Go to Settings > Privacy and Security > Site Settings, then scroll down to Notifications in the pop-up window that appears. From there, you can toggle the Sites Can Ask to Send Notifications switch that turns website notification prompts on or off.
Your code should be working.. may be you missed something else.. Please try this code..
sendNotification({
title: 'New Notification',
message: 'Your message goes here',
icon:'https://cdn2.iconfinder.com/data/icons/mixed-rounded-flat-icon/512/megaphone-64.png',
clickCallback: function () {
alert('do something when clicked on notification');
}
});
function sendNotification (data) {
if (data == undefined || !data) { return false }
var title = (data.title === undefined) ? 'Notification' : data.title
var clickCallback = data.clickCallback
var message = (data.message === undefined) ? 'null' : data.message
var icon = (data.icon === undefined) ? 'https://cdn2.iconfinder.com/data/icons/mixed-rounded-flat-icon/512/megaphone-64.png' : data.icon
var sendNotification = function (){
var notification = new Notification(title, {
icon: icon,
body: message
})
if (clickCallback !== undefined) {
notification.onclick = function () {
clickCallback()
notification.close()
}
}
}
if (!window.Notification) {
return false
} else {
if (Notification.permission === 'default') {
Notification.requestPermission(function (p) {
if (p !== 'denied') {
sendNotification()
}
})
} else {
sendNotification()
}
}
}
Please check browser version whether it has support for notification and try by using http://{localhost ip} instead of localhost/xyz.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With