5

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
  • 8,707
  • 22
  • 85
  • 146

1 Answers1

7

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,148
  • 1
  • 22
  • 35