Kenneth
Kenneth

Reputation: 51

How do I open a browser window from a desktop notification that's generated from a background page?

I have built a simple chrome application which has a background page that shows desktop notifications when a new article is available.

If a user clicks on the notifications when a browser window is open, then they are taken to the new article page and everything is right with the world.

If however the browser is closed when the notification is shown, then the href does not open the browser. I have also tried to capture the click and set a window.open, but that isn't working either.

As an aside, it would also be good if I could check to see if an instance of my app is already open and use that window/tab when the notification is clicked, but that's secondary to the first problem.

Any help would be great!

Thanks

Upvotes: 5

Views: 1796

Answers (2)

Shankar Morwal
Shankar Morwal

Reputation: 157

Check if chrome window is open. if open the create in new tab else oepn new window.

chrome.windows.getCurrent(function(currentWindow) {
  if (currentWindow != null) {
    return chrome.tabs.create({
      'url': url
    });
  } else {
    return chrome.windows.create({
      'url': url,
      'focused': true
    });
  }
});

Upvotes: 3

Mohamed Mansour
Mohamed Mansour

Reputation: 40149

Have you tried, creating a window then tab using the Chrome API? For example:

linkDOM.addEventListener('click', function() {
  chrome.windows.create({url: 'http://someurl.com'});
}, false);

I usually do the above when programmatically opening a link from extension pages.

Upvotes: 0

Related Questions