7

Javascript in a web page (not a Chrome app or extension) can create a Chrome desktop notification, and set an URL to be opened when the notification is clicked.

var notification = new Notification('Notification title', {
  icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
  body: "Hey there! You've been notified!"
});

notification.onclick = function () {
  window.open("http://stackoverflow.com/a/13328397/1269037");
}

Is it possible to create a notification that, when is clicked, opens the tab which created that notification?

Lighty
  • 71
  • 1
  • 5

3 Answers3

14

Yes.

notification.onclick = function () {
  window.focus();
}
Zectbumo
  • 4,128
  • 1
  • 31
  • 26
2

Instead of window.open, try using:

window.location = "https://stackoverflow.com/a/13328397/1269037"

That worked for me.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
0

Certainly! If you want the notification to open the tab that created it, you can use the window. location to set the URL of the current tab. Here's the updated code:

var notification = new Notification('Notification title', {
  icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
  body: "Hey there! You've been notified!"
});

notification.onclick = function () {
  window.location = "https://stackoverflow.com/a/13328397/1269037";
}

By setting the window. location to the desired URL, the current tab will navigate to that URL when the notification is clicked. This will effectively open the desired location in the same tab that created the notification.