Skip to main content

Push notifications in Web Apps via Service Workers

A push notification is a message that is “pushed” from backend server or application to user interface, e.g. (But not limited to) mobile applications and desktop applications.
Service workers are a great way to run some script in the background and not putting the load on your main business logic running on front-end.
 Your app does not have to be open in the browser, to get the notification. It mean that’s the real purpose of push notifications, notify the user about the content of your application.
                         Application server tells Google Cloud Messaging (GCM) server that there is something new content and GCM server then awakens the service worker which generates the push notification.

Register Service Worker



if ('serviceWorker' in navigator && 'PushManager' in window) {
  navigator.serviceWorker.register('software-worker.js')
  .then(function(reg) {
    registration = reg;
  })
  .catch(function(error) {
    return;
  });
} else {
  // Well Service Worker or Push Notifications 
  // aren't supported 
  // yet in your browser
}

Here is what  did in above snippet of code:
1. Checking if the browser supports service worker and push notifications.
2. Handle accordingly if the browser doesn’t support them.
3. If they do, register the service worker file and get the registration object.

Subscribe to Notifications from client



registration.pushManager.subscribe({
    userVisibleOnly: true,
    applicationServerKey: UInt8array Key
  })
  .then(function(subscription) {
    if (subscription) {
       // User is already subscribed
    }
  })
  .catch(function(err) {
    // User have declined request for Push notifications
  });
This is what doing above:
1. Using push manager service create the subscription.
2. You need to have application Server Key for this.
3. Once got the subscription object, send it to server.


Note: user Visible Only: true must be true, otherwise you will get an error in your browser console.

Push notifications handler



self.addEventListener('push', function(event) {
  event.waitUntil(
    self.registration.showNotification(Title, {
      body: Body,
      icon: Icon
    })
  );
});

It will trigger every time, your service worker will be awakened by GCM server.
And that’s it, you have an active service worker that will push notifications to the user every time GCM server will call it.


This post’s scope was only to cover the use of service workers in push notifications


                






















Comments

Popular posts from this blog

Pay Per Click by AppWorks Technologies Pvt Ltd

Microsoft launches a new AI startup program at Station F in Paris

Microsoft is rethinking its strategy when it comes to startup acceleration in Paris. The company is going to focus on artificial intelligence. This will lead to a new program for AI startups at  Station F . Microsoft has had a startup accelerator in the Sentier neighborhood for a few years now. When Station F opens at the  end of June , the company is going to focus exclusively on artificial intelligence with a partnership with  INRIA  and move everything to the startup campus. “We think that we’re first going to select 5 or 6 startups that can foster an ecosystem around INRIA and themselves,” Microsoft France Developer Experience leader Christophe Shaw told me. “The idea is that we’re eventually going to have a hundred startups in this club.” And the first startup joining this program is  Recast.ai . This French startup has been building a service that helps you build, launch and manage chatbots. The company also takes care of hosting those bots. A few big French companies hav

Making telescopes that curve and twist

New computational tool automates design of telescoping structures that provide compact storage and rapid deployment. Shown here: a complex telescoping lizard expands to many times its original volume, serving as a stress test of the method. Credit: Chris Yu/Carnegie Mellon University A new tool for computational design allows users to turn any 3D shape into a collapsible telescoping structure. New mathematical methods developed by researchers at Carnegie Mellon University capture the complex and diverse properties of such structures, which are valuable for a variety of applications in 3D fabrication and robotics—particularly where mechanisms must be compact in size and easily deployable. The research, "Computational Design of Telescoping Structures," led by Carnegie Mellon Professors Stelian Coros and Keenan Crane and PhD student Christopher Yu, will be presented at the annual SIGGRAPH conference, 30 July to 3 August in Los Angeles. The conference each year spotlights the