Desktop Notifications - Javascript

Asked

Viewed 327 times

2

I need to send desktop notifications to the user, even when the site is not open, but I can only call the notification at events (click, load, change...). How could I send these notifications?

Notification.requestPermission(function() {
                var notification = new Notification("Casa de Cambio Santos", {
                    icon: 'http://teste.png',
                    body: "Texto texto texto texto"
                });
                notification.onclick = function() {
                    window.open("http://teste.com.br");
                }
            });
  • Are you using wordpress? the Onesignal Plugin does this for you. Doesn’t it answer? try looking for something like Web Push Notifications.

1 answer

3


According to the description you gave you need to use Push API instead of just Notifications API. Think about the API Push as a Notifications with "steroids".

Whether your site is static or dynamic this API is javascript and runs on the user’s browser, however it is a little more laborious to implement than the Notifiuncles.

This API is available in Service Worker and needs a back-end as a service to actually send notifications to the customer (user).

There is a large offer of services (servers) that make this work of back-end for Push free or paid that may even have good documentation for you to implement a Service Worker on your website however if you own a server (manage) it might be a good idea to implement your own service and maintain full control over the notification system.

I usually don’t like posting answers like: "follow this link here" but if I were to post the codes to implement Push with due clarification would be a very long post beyond what is not needed "reinvent the wheel" with such a large range of good materials and good sources.

Then follow the links:

The example of the site Google Developers is very good but does not teach to create a service itself if you are interested in diving head first you can start by taking a look at the list of libs hosted on Github i particularly recommend web-push made to run on Node.


editing:

A Cookie Book about Push API (maybe the best I’ve ever read)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.