Carpetfizz
Carpetfizz

Reputation: 9149

Show Chrome desktop notifications only when tab is inactive

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!

Upvotes: 5

Views: 4194

Answers (1)

rgajrawala
rgajrawala

Reputation: 2188

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.

Upvotes: 7

Related Questions