lundi 5 mars 2018

Notifications api is not working in promise callback

I'm getting the msg from socket. I'm using react in my project and I tried like below to display notification.

constructor(props){
    super(props);
    // some codes.....


    var self = this;
    this.socket.on('message', function(msg){
      if (/* condition */) {
        self.makeNotification(`You have received new message`)
      }
      self.onRecievedMessage(msg);
    });
  onRecievedMessage(msg) {
    // some code to change state variable
  }
  makeNotification(message) {
      var e = new window.Notification('Alert', {
      body: message,
      icon: 'url',
      tag: 'new_message'
    });
    console.log('Notification object is', e)
  }
}

but I can not see the notification. of course, I executed

Notification.requestPermission().then(result => {})

to get the permission and I granted.

Note: when I execute the function in the console notification is working. new Notification("test title")

Aucun commentaire:

Enregistrer un commentaire