Skip to main content

Sending browser independant notifications from a web-app

So I came across a data-mining product that presents very useful information via a web-app. What gets delivered to the users of the product is new information  in the form of reports at regular intervals. So the process is,

  1. new data is acquired 
  2. all the reports are run again on newer data
  3. a manager overseeing the product sends an email to the users that notify them that the reports have been updated.

Motivation

So this process is great and it requires very little effort on part of the manager emailing the users. So the question is "why make that manager use even little effort, when the manager's time could be better utilized?". While the manager can add some more personalized info in the email, the entire process can potentially have the following disadvantages
  1. human error: the manager may get busy and may forget to notify the users of the updated reports
  2. distractions: if the user has to navigate away from the web app and check their email, this could potentially cause distractions. Say the user has a very personal email that is impossible to avoid? this can completely take their attention away from work
Well one way to solve this problem could be through the web-app sending notifications. Having the web-app present notification, makes the app feel... hmm how should I put it? it makes the web-app feel "lived-in" i.e. more personalized. A notification from the web-app gives the user one less exit venue i.e. one less reason to go away from the app. 

Solution

Moving on, W3C recently introduced the Notifications API, which is awesome however it is not really browser independent, as it does not work on IE.  So why add a really cool feature to the web-app that isolates a full set of users?

So all of the above points prompted me to write this little jQuery plugin that can send web-app notifications from the browser.
So I had no experience in writing jQuery plugins, so I found it a bit odd from some tutorials that, all you had to do was initialize an element on your web-page in your script like this,

   $('elem').somePlugin()

I thought, hmm what if you want to modify the properties of the plugin? then what? So in the quest to accomplish that from the jquery plugin, after some looking around I came across this post, that talks about the class and function pattern. With that pattern, an instance is created and returned for the DOM element, so if you want to modify the object later you can do that. So you can do this,

  var plugin = $('elem').somePlugin();
  plugin.modifyPlugin();

Implementation details

So why do I want to make changes to the plugin? Well I will get to it in a bit, but first some back story. I came across this new web speech API that's part of the W3C spec and I thought, "well it would be even better, if the notifications were spoken by the browser". Now I realize that the web speech API is still new and has little support, but I figured I would add that feature anyway, as an "icing on the cake" so if the browser supports it, it will play the sound and if it does not, the plugin would still show the notifications anyway.

To wrap this up, how does this all fit together?  lets have a look at the code below

       var nPlugin,
             url='/sample',
             speakNotifications= true,
             username="Bhuman";

    function test(){
    nPlugin = $("#notificationDiv").appNotify(url, speakNotifications, username);
}
function stopSpeech(){
    nPlugin.stopSpeech();
}

So first we initialise the plugin, by giving it the url and specify if the notifications should be spoken and the name of the user. The reason why we give it the name of the user is, say there are 2 new notifications, so not only with the plugin queue the notifications to be shown, but on receiving them on a browser that does support notifications, it will figure out what time of the day it is say "Good morning/afternoon or evening Bhuman, you have 2 new notifications".  So the notification plugin can be thought of as a class that deals with the notifications and exposes certain public methods that can be used to interact with the plugin. So it exposes the stopSpeech()  method which can be called anytime to stop the notification from being spoken, should it be doing so. So this functionality can be achieved thanks to the class and function pattern, talked about in this post.

When should the notifications be sent?

The next part of the puzzle was how and when will the notifications be sent to the browser? Well ideally the server should only send data when it actually has something to send. One way to achieve that is using websockets, but again it is new and has limited support on older browsers. So the next best to that was long-polling. So what I have did is included a Node.js project in this repository that demonstrates receiving notifications via long-polling.

Licence details

So the plugin is open-source and it is licensed under the BSD 2-Clause license. So I have built a website that gives more info about this plugin and the plugin can be downloaded from here.

Comments

Popular posts from this blog

Upload to AWS S3 from Java API

In this post, you will see code samples for how to upload a file to AWS S3 bucket from a Java Spring Boot app. The code you will see here is from one of my open-source repositories on Github, called document-sharing. Problem Let’s say you are building a document sharing app where you allow your users to upload the file to a public cloud solution. Now, let’s say you are building the API for your app with Spring Boot and you are using AWS S3 as your public cloud solution. How would you do that? This blog post contains the code that can help you achieve that. Read more below,  Upload to AWS S3 bucket from Java Spring Boot app - My Day To-Do (mydaytodo.com)

Addressing app review rejections for auto-renewing subscription in-app purchase (iOS)

The ability to know what the weather is like while planning your day is a feature of  My Day To-Do  Pro and as of the last update it’s also a part of the  Lite version . Unlike the Pro version it’s an auto-renewing subscription based  in-app purchase (IAP)  in the Lite version. What means is that when a user purchases it, the user only pays for the subscription duration after which the user will be automatically charged for the next period. Adding an  auto-renewing  subscription based IAP proved to be somewhat challenging in terms of the app store review i.e. the app update was rejected by the App Review team thrice because of missing information about the IAP. Therefore in this post I will share my experiences and knowledge of adding auto-renewing IAP in hopes to save someone else the time that I had to spend on this problem. In-App purchase This year I started adding IAPs to My Day To-Do Lite which lead to learning about different types of IAP...

Serving HTML content in an iOS app that works in iOS 7 and later (using Swift)

As I have mentioned in an earlier post , I really enjoying coding in Swift. Now what am I doing with it? Well I am trying to build an HTML5 app that must work on devices with iOS 7. So in iOS8 apple has introduced a whole bunch of features that facilitate easy communication between web content and lets just call it back-end Swift code, but those features are not in iOS 7. So why do I want to build something that would work in an older OS? well I do not expect existing iOS users to upgrade to iOS 8 straight away and i also know a couple of people who would be very reluctant to upgrade their iPhones to iOS 8. Now in case you do not, you can have a read of the "Working with WebViews" section of this post , to know how to serve HTML content with WebViews. So when I started building my app, I wanted to know: How do I invoke some Swift code from my HTML content? Well the solution to this may feel a little bit "hacky" but it is a solution to achieve this.  The followi...