Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable browser notification on localhost

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 ...

like image 763
Hadeel36 Avatar asked Jul 23 '17 08:07

Hadeel36


People also ask

Can you receive push notifications when your browser is not running?

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.

How do I turn on notifications for a website on Chrome?

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.


2 Answers

Your code should be working.. may be you missed something else.. Please try this code..

Usage

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

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()
        }
    }
}
like image 126
Rameez Rami Avatar answered Oct 14 '22 05:10

Rameez Rami


Please check browser version whether it has support for notification and try by using http://{localhost ip} instead of localhost/xyz.

like image 3
Nikhil Arora Avatar answered Oct 14 '22 07:10

Nikhil Arora