How to send push notification to web browser?

202,992

Solution 1

So here I am answering my own question. I have got answers to all my queries from people who have build push notification services in the past.

Update (May 2022): Here is a doc on web push notification from Google.

See this detailed introduction to notification API from Mozilla.

Airships article on the topic and how web push & app push varies.

Answer to the original questions asked 6 years ago:

  1. Can we use GCM/APNS to send push notification to all Web Browsers including Firefox & Safari?

Answer: Google has deprecated GCM as of April 2018. You can now use Firebase Cloud Messaging (FCM). This supports all platforms including web browsers.

  1. If not via GCM can we have our own back-end to do the same?

Answer: Yes, push notification can be sent from our own back-end. Support for the same has come to all major browsers.

Check this codelab from Google to better understand the implementation.

Some Tutorials:

  • Implementing push notification in Django Here.
  • Using flask to send push notification Here & Here.
  • Sending push notifcaiton from Nodejs Here
  • Sending push notification using php Here & Here
  • Sending push notification from Wordpress. Here & Here
  • Sending push notification from Drupal. Here

Implementing own backend in various programming languages.:

Further Readings:

Are there any free services to do the same? There are some companies that provide a similar solution in free, freemium and paid models. Am listing few below:

  1. https://onesignal.com/ (Free | Support all platforms)
  2. https://firebase.google.com/products/cloud-messaging/ (Free)
  3. https://clevertap.com/ (Has free plan)
  4. https://goroost.com/

Note: When choosing a free service remember to read the TOS. Free services often work by collecting user data for various purposes including analytics.

Apart from that, you need to have HTTPS to send push notifications. However, you can get https freely via letsencrypt.org

Solution 2

Javier covered Notifications and current limitations.

My suggestion: window.postMessage while we wait for the handicapped browser to catch up, else Worker.postMessage() to still be operating with Web Workers.

These can be the fallback option with dialog box message display handler, for when a Notification feature test fails or permission is denied.

Notification has-feature and denied-permission check:

if (!("Notification" in window) || (Notification.permission === "denied") ) {
    // use (window||Worker).postMessage() fallback ...
}

Solution 3

You can push data from the server to the browser via Server Side Events. This is essentially a unidirectional stream that a client can "subscribe" to from a browser. From here, you could just create new Notification objects as SSEs stream into the browser:

var source = new EventSource('/events');

source.on('message', message => {
  var notification = new Notification(message.title, {
    body: message.body
  });
}); 

A bit old, but this article by Eric Bidelman explains the basics of SSE and provides some server code examples as well.

Solution 4

this is simple way to do push notification for all browser https://pushjs.org

Push.create("Hello world!", {
body: "How's it hangin'?",
icon: '/icon.png',
timeout: 4000,
onClick: function () {
    window.focus();
    this.close();
 }
});

Solution 5

GCM/APNS are only for Chrome and Safari respectively.

I think you may be looking for Notification:

https://developer.mozilla.org/en-US/docs/Web/API/notification

It works in Chrome, Firefox, Opera and Safari.

Share:
202,992

Related videos on Youtube

esafwan
Author by

esafwan

I Started with DOS, grew up with Windows and Matured with GNU/Linux. Was a Hardcore gamer, later interest shifted to programing and graphic designing. Is passionate when it comes to debating. Hates working under someone and has never so far. Currently busy building a career that is free from boss and hunger for money!

Updated on February 22, 2020

Comments

  • esafwan
    esafwan about 4 years

    I have been reading for past few hours about Push Notification API and Web Notification API. I also discovered that Google & Apple gives push notification service for free via GCM and APNS respectively.

    I am trying to understand if we can implement push notification to browsers using Desktop Notification, which I believe is what Web Notification API does. I saw a google documentation on how this can be done for Chrome here & here.

    Now what am still not able to understand is:

    1. Can we use GCM/APNS to send push notification to all Web Browsers including Firefox & Safari?
    2. If not via GCM can we have our own back-end to do the same?

    I believe all these answered in one answer can help a lot of people who are having similar confusions.

    • dandavis
      dandavis over 8 years
      sure, you can run your own backend, but it's complicated.
    • TRiNE
      TRiNE over 7 years
      It is not complicated much. browser-push is a complete example tutorial on how to send push notifications to browsers without third party service. lahiiru.github.io/browser-push
  • Marco Castelluccio
    Marco Castelluccio over 8 years
    You can use the Push API, each browser provides its own service.
  • collimarco
    collimarco about 8 years
    You need HTTPS only for the Push API, not for Safari
  • Kokizzu
    Kokizzu almost 8 years
    onesignal, nice info :3
  • Akash Budhia
    Akash Budhia over 7 years
    Incase you don't wish to pay 3rd party libraries, here's a developer guide of writing this yourself. cronj.com/blog/browser-push-notifications-using-javascript . It also mentions how to handle when you don't wish to move your main site to HTTPs. (Node.js and JavaScript).
  • TRiNE
    TRiNE over 7 years
    Here is complete guide and source code of mine. lahiiru.github.io/browser-push
  • Lemmings19
    Lemmings19 over 6 years
    Onesignal is free, but note how much data it captures from your users. Please read the ToS before using: onesignal.com/tos
  • That Realty Programmer Guy
    That Realty Programmer Guy about 6 years
    LetsEncrypt.org means you should have no trouble getting HTTPS for everythign
  • Vladimir Vlasov
    Vladimir Vlasov over 5 years
    Are you sure that gorush from the Go Lang link is applicable here? Because I think it's a service for sending push notifications to iOS and Android. There are some Go libraries for the Web Push Protocol like webpush-go and web-push-go.
  • Accountant م
    Accountant م over 5 years
    Also check this Firefox document about the subject
  • esafwan
    esafwan over 5 years
    @VladimirVlasov Updated the answer. Thanks for pointing it out.
  • esafwan
    esafwan over 5 years
    @Accountantم Added it to the list. Thanks
  • koder
    koder about 2 years
    PushAlert has also a free plan and they don't collect data.