How to get focus to a Chrome tab which created desktop notification?

后端 未结 5 1767
北荒
北荒 2020-12-07 14:50

I would like to implement same functionality as Gmail has nowadays. When new email arrives or new chat comes, notification popup appears and if you click it, the tab with Gm

5条回答
  •  青春惊慌失措
    2020-12-07 15:18

    Using Notifications.

    if (typeof Notification !== 'undefined') {
      alert('Please us a modern version of Chrome, Firefox, Opera or Safari.');
      return;
    }
    
    Notification.requestPermission(function (permission) {
      if (permission !== 'granted') return;
    
      var notification = new Notification('Here is the title', {
        icon: 'http://path.to/my/icon.png',
        body: 'Some body text',
      });
    
      notification.onclick = function () {
        window.focus();
      };
    });
    

提交回复
热议问题