Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome desktop notification click to focus on content

I am building desktop notification into my a chrome extension that I am working on. The functionality I need required that the user be taken to the tab that caused the notification when they click on the notification window. I can get that working using the chrome.tabs API, but what I can't manage to figure out is how to bring Chrome to the front when the notification is clicked.

I know window.focus() is disabled in chrome, but this is definitely possible to do since that's the behavior of the Gmail desktop notifications.

like image 798
Wade Tandy Avatar asked Apr 30 '12 21:04

Wade Tandy


People also ask

Why are my desktop notifications not working?

On Windows, click the Start button and then Settings. In the navigation pane on the left, choose System, then Notifications. At the top of the page, make sure Notifications is on by swiping the button to the right. In the Notifications from apps and other senders section, make sure Google Chrome is turned on as well.

Why do I keep getting notifications from Chrome?

Chrome notifications usually appear during your browsing experience. It alerts you whenever a site or app is sending you a notification. If users accept the notifications from a website, they start getting notifications. Google Chrome is a popular browser.


2 Answers

notification = webkitNotifications.createNotification(...)
notification.onclick = function(){
    window.focus();
    this.cancel();
};
notification.show()

...works as expected, without any additional permissions.

like image 129
dragon Avatar answered Sep 20 '22 15:09

dragon


Use chrome.tabs.update(tabId, {active: true}); to focus a tab (not to be confused with chrome.windows.update).

The tabId is often obtained via the Tab type. This object is passed to many methods/event listeners (sometimes via the MessageSender type).

like image 23
Rob W Avatar answered Sep 22 '22 15:09

Rob W