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

后端 未结 5 1759
北荒
北荒 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条回答
  •  萌比男神i
    2020-12-07 15:21

    You can just place window.focus() in Google Chrome. It will focus to that window when clicked.

    var n = window.webkitNotifications.createNotification('ico.gif', 'Title', 'Text');
    n.onclick = function(x) { window.focus(); this.close(); };
    n.show();
    

    I opened the inspector in Gmail, added the above code, moved to a different tab, and ran it. The notification appeared and once clicked, it brought me back to Gmail.

提交回复
热议问题