2

I am reading the Web notification API and I want to send notification like they are on iOS or Android (ex: You have 7 new messages).

Considering I have accepted notification from www.my-foobar-super-site.com, can I, after authorizing notification from it, can my website send Web notification to a user when this user has no tabs open of this website?

Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
  • The Notifications API (alone) can only be used when your website is open. Otherwise you can display the notifications also when your website is closed, but you need to use the Notifications API + Service Workers + Push API: https://www.w3.org/TR/push-api/ – collimarco Apr 07 '22 at 14:10

2 Answers2

4

After accepting web notifications, can a website send notifications when browser has no tab open of this website?

Yes, but there's more to it than just a user granting the notifications permission.

The Web Notifications API is only concerned with displaying messages on-screen from a script already running on the user's computer, it is unrelated to "push notifications" or delivery.

To send "push" messages to a user's browser, you'll need to use the separate Push API - and run a Service Worker to receive messages from your service (via the Push API) and only then can your script use the Notifications API to show the notification message.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • 1
    For those looking for a tutorial with minimalistic code example, [tutorial](https://d3v.one/a-minimal-web-push-example/), [demo](https://d3v.one/apps/pwa/push/), [github](https://github.com/d3v-one/minimal-web-push-demo) – Dimitri Kopriwa Aug 16 '20 at 14:32
0

Yes, as long as you have registered a service worker.

miknik
  • 5,748
  • 1
  • 10
  • 26