1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Send Desktop Notifications in Chrome

Discussion in 'JavaScript and AJAX' started by pradeep, Feb 25, 2014.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Desktop notifications for web applications is a great plus, the user can be notified irrespective of the current window/tab they are using. Chrome, which has the largest share of the browser market, supports desktop notification, also Safari, or any webkit based browser.

    You can easily implement it you web applications, like mail clients (GMail has this feature), CRMs, Stock Market apps, and anything you can think may use this feature. All you need to some JavaScript and a server side script to send updates, or you can do without the server part also.

    Now, let's get our hands dirty with some desktop notification code.

    Implementing Desktop notifications on Webkit based browsers



    Try this on Chrome/Safari, first time a popup will appear asking for permission.

    Code:
    if (window.webkitNotifications) { // check if browser has support
        var has_perm = window.webkitNotifications.checkPermission(); // check if permission granted
        if (has_perm == 0) { // 0 means has permission to show desktop notification
            var notification = window.webkitNotifications.createNotification(
                'http://g4e.com/logo.png',
                'Welcome!',
                'Here is the notification'
            );
            notification.show();
        } else {
            window.webkitNotifications.requestPermission();
        }
    }
    
    Additionally, you may also control what happens when the notification is clicked. Google recently launched a Rich Desktop notification where one can control the style elements of the notification body, that we'll cover in some other article in the future.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    pradeep likes this.

Share This Page