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

Html5 based widget for an iOS app: Today extension powered by the Ionic framework

At some point the thought of adding a Widget to my iOS app came to mind which was followed by starting work on adding a widget for my app, My Day Todos . Obviously the first step was to learn how to add a Widget to an iOS app and in that learning process I discovered many things about widgets in iOS first of which was a widget in iOS is a an app extension i.e a  Today Extension . While I am still haven't finished working on the widget for my iOS app, I thought I would take some time out and share what I have learned. In this post, I will share a few important tips and provide an example of how to add a Widget to an iOS app and have the widget UI powered by Html5 via  Ionic framework . I added some code to my Github repo, Html5StarterAppWithSwift in order to show how this can be achieved. There are already too many tutorials out on the Web on how to add a Today extension to an iOS app so I won't be including that here. Instead I will focus on sharing some of the useful tip...

Why can't I cancel a local notification in my iOS app?(in Swift)

Prelude I have mentioned the fact that I am working on my first iOS app in a number of my previous posts and as such I face a number of newbie/noob(?) problems. They are not really problems as much as they are simply things that I do not know, for e.g. the following, how do I get the day of the week? where is my string.replaceAll in Swift? knowing which local notification brought my app to foreground ? serving HTML content in an iOS app that works on iOS 7 and above Thankfully, I have managed to make at least one generic solution and contribute it to the community i.e. my open-source Xcode project template( HTML5StarterAppWithSwift ) which you can get from Github . So this post basically describes another very simple problem that I have found a solution to, but I do not fully understand why the problem was occurring in the first place. Introduction As I have mentioned in this post , my iOS app uses local notifications i.e. UILocalNotification   and i...

How does the browser's native login prompt work and how to use it programmatically

This is another one of those rather interesting problems that I had to solve at my day job and since I knew little about how to solve it when I first came across it, I am going to blog about it. I am sure there are others like me, who may not know how to solve it when tasked with such a problem. Problem So I am working on a Java based project at work(lets call it Project X )  for which my latest task was to change the way it handles login. Problem details I think it's best I provide some context before I describe my problem, so here we go, You see when you try to access a certain restricted area of a web-app when not logged-in the browser will show you a login prompt, and only when you enter the right credentials, will you be able to access that area of a web-app. That entire process of seeing the login prompt, entering the credentials etc requires human intervention and my task was to achieve all that programmatically i.e. without any human intervention for Project...