Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Chrome extension to display desktop notification

  sonic0002        2013-06-29 22:23:51       17,624        8    

Have you ever thought about writing extension for web browsers so that we can complete some tasks easily. Do you think writing extension for browsers is very difficult? You have no clue where to begin? On Chrome, this isn't any problem now as long as you know how to write HTML,CSS and JavaScript.

We will show one example of Chrome extension today. The extension will display a desktop notification on your desktop. You need to create two files here, one is the notification.json which is the configuration file for the extension, it contains the name and version and some initialization information about the extension, the other one is the notification.js file which to be executed when the extension is loaded.

Below is the code for manifest.json:

{
    "name":"PixelsTech Website Statsitic Viewer",
    "manifest_version":2,
    "version":"1.0",
    "background":{
        "scripts":["notification.js"]
    },
    "browser_action":{
        "default_icon":"pixelstech.gif"
    },
    "permissions":[
        "notifications"
    ],
    "web_accessible_resources":[
        "pixelstech.gif"
    ]
}

Here the most important configurations are the script to run in the background when the extension is loaded and the permission of the extension, it should be able to display notification.

Next is the notification.js:

var Notification=(function(){
    var notification=null;

    return {
        displayContent:function(icon,title,content){
            notification=webkitNotifications.createNotification(icon,title,content);
            notification.show();
        },
        displayURL:function(url){
            notification=webkitNotifications.createHTMLNotification(url);
            notification.show();
        },
        hide:function(){
            notification.close();
        }
    };
})();

chrome.browserAction.onClicked.addListener(function(windowId){
    var icon="pixelstech.gif",title="PixelsTech Website Statistic Viewer",content="1,000,000";
    Notification.displayContent(icon,title,content);
});

We create a helper class Notification to manage the notification instance, we can use webkitNotifications.createNotification() to create the notification object and call its show() method to display the notification. The parameters passed are fairly easy to understand, the first one is the icon to be shown on the notification. Then we will register the browser action to start the extension.

After 2013, the Legacy Notification API which contains webkitNotifications has been deprecated. Users should use Rich Notification instead now. Below is the new nptification.js.

var Notification=(function(){
    var notification=null;
 
    return {
        display:function(opt){
            notification=chrome.notifications.create(opt);
            notification.show();
        },
        hide:function(){
            notification.close();
        }
    };
})();
 
chrome.browserAction.onClicked.addListener(function(windowId){
	var opt = {
		type: "basic",
		title: "PixelsTech Website Statistic Viewer",
		message: "1,000,000",
		iconUrl: "pixelstech.gif"
	};
    Notification.display(opt);
});

After creating these two files, we put them in the same folder, and then we need to open Chrome and load the extension.Here are steps:

  1. Visit chrome://extensions in your browser (or open up the Chrome menu by clicking the icon to the far right of the Omnibox:The menu's icon is three horizontal bars.. and select Extensions under the Tools menu to get to the same place).

  2. Ensure that the Developer mode checkbox in the top right-hand corner is checked.

  3. Click Load unpacked extension… to pop up a file-selection dialog.

  4. Navigate to the directory in which your extension files live, and select it.

For details about how to load extension in Chrome, please refer here.

EXAMPLE  CHROME EXTENSION  DESKTOP NOTIFICATION 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  8 COMMENTS


LogoS [Reply]@ 2015-05-20 11:04:22

Didn't work for me. First the error in:

    "permissions":[
        "notifications",
    ],
That comma shouldn't be there.
 
Later it didn't show any notifications. Maybe it is because the chrome version, you did it a year ago, maybe things change a lot since then.
 
I'm searching for a tiny example to how notifications work and I didn't found anything useful yet.
Thanks for sharing this anyways.

Have a nice day!
sonic0002 [Reply]@ 2015-05-20 23:09:56

What Chrome version are you using? I want to have some tests on it.

BTW, thank you for pointing out the coma issue. Yes, it shouldn't be there.

LogoS [Reply]@ 2015-05-21 06:46:01

I'm in 42.0.2311.152 now.

And I noted that was 2 years ago. I saw some samples from 2013 that didn't work either. But this point me in the right direction. I made it working this way:

Background.js

chrome.browserAction.onClicked.addListener(function(windowId){
var opt = {
type: "basic",
title: "Notificación de prueba",
message: "Usted ha sido notificado",
iconUrl: "./icono.png"
}
chrome.notifications.create(opt);
});


manifest.json

{
"name":"Notificación de prueba",
"manifest_version":2,
"version":"1.0",
"background":{
"scripts":["background.js"]
},
"browser_action":{
"default_icon":"icono.png"
},
"permissions":[
"notifications"
]
}

sonic0002 [Reply]@ 2015-05-21 09:23:40

I did some search on the update and found that Legacy Notification API has been deprecated back in 2013. See developer.chrome.com/extensions/desktop_notifications

The webkitNotifications used in the sample code belongs to the Legacy Notification API.

Have tested with your code and it works. Thanks.

Anupal [Reply]@ 2015-12-19 05:22:34

Great! I found it working. Thanks

I want you help more in this. Now in the same code i want to do a ajax call to my website and check for any notification to display. How could this possible? 

Please help. 

Ke Pi [Reply]@ 2015-12-19 12:04:12

In your manifest.json, add below permission to allow cross-origin access to your website through AJAX.

"permissions":[
	"notifications",
	"http://www.pixelstech.net/"
],

 

Then do a normal AJAX call in chrome.browserAction.onClicked.addListener()..

Anonymous [Reply]@ 2016-03-08 12:14:26

Thank you!!! You realy help me!!!!!!

Ke Pi [Reply]@ 2016-03-08 21:13:57

You are welcome