3

I'm currently using the following code to show Desktop notifications in Google Chrome:

new Notification(data.sender.displayName, {
                    icon: '/images/logo.png',
                    body: data.content,
});

This is for a chat application and it doesn't make sense to show notifications on the same page as the chat. How can I make it so that the notification only appears when the tab is inactive or if the window is out of focus?

Thanks!

Carpetfizz
  • 7,979
  • 22
  • 73
  • 138

1 Answers1

5

Use the Visibility API to find out if the window is visible. You can use event listeners to set custom hide/unhide events. See here.

Community
  • 1
  • 1
rgajrawala
  • 2,018
  • 1
  • 20
  • 35