Skip to Content

desktopNotification()

Show desktop notifications.

JavaScript February 19, 2018

Usage

JavaScript
desktopNotification(title, message, clickCallback, showCallback, closeCallback, errorCallback)

Parameters

title
(Required) (String) The title of the notification
Default: None

message
(Optional) (String or Object) The message of the notification or an object of options (See below)
Default: None

clickCallback
(Optional) (Function) The function to trigger when the notification is clicked
Default: None

showCallback
(Optional) (Function) The function to trigger when the notification is shown
Default: None

closeCallback
(Optional) (Function) The function to trigger when the notification is closed
Default: None

errorCallback
(Optional) (Function) The function to trigger when the notification has an error
Default: None

Parameter Notes

The message can be a string or an object with the following parameters:

dir: Direction (“auto”, “ltr”, or “rtl”)
lang: Language (Ex: “en-US”)
body: The message itself
tag: Unique notification tag to prevent duplicates
icon: The image thumbnail to show with the notification.

Request or provide clarification »

Source File

Located in /assets/js/nebula.js on line 5064.

JavaScript
nebula.desktopNotification = function(title, message, clickCallback, showCallback, closeCallback, errorCallback){
    if ( nebula.checkNotificationPermission() ){
        //Set defaults
        var defaults = {
            dir: "auto", //Direction ["auto", "ltr", "rtl"] (optional)
            lang: "en-US", //Language (optional)
            body: "", //Body message (optional)
            tag: Math.floor(Math.random()*10000)+1, //Unique tag for notification. Prevents repeat notifications of the same tag. (optional)
            icon: nebula.site.directory.template.uri + "/assets/img/meta/android-chrome-192x192.png" //Thumbnail Icon (optional)
        };

        if ( typeof message === "undefined" ){
            var message = defaults;
        } else if ( typeof message === "string" ){
            var body = message;
            message = defaults;
            message.body = body;
        } else {
            if ( typeof message.dir === "undefined" ){
                message.dir = defaults.dir;
            }
            if ( typeof message.lang === "undefined" ){
                message.lang = defaults.lang;
            }
            if ( typeof message.body === "undefined" ){
                message.body = defaults.lang;
            }
            if ( typeof message.tag === "undefined" ){
                message.tag = defaults.tag;
            }
            if ( typeof message.icon === "undefined" ){
                message.icon = defaults.icon;
            }
        }

        var instance = new Notification(title, message); //Trigger the notification

        if ( clickCallback ){
            instance.onclick = function(){
                clickCallback();
            };
        }
        if ( showCallback ){
            instance.onshow = function(e){
                showCallback();
            };
        } else {
            instance.onshow = function(e){
                setTimeout(function(){
                    instance.close();
                }, 20000);
            };
        }
        if ( closeCallback ){
            instance.onclose = function(){
                closeCallback();
            };
        }
        if ( errorCallback ){
            instance.onerror = function(){
                ga('send', 'exception', {'exDescription': '(JS) Desktop Notification error', 'exFatal': false});
                errorCallback();
            };
        }
    }

    return false;
};

Override

To override or disable this JavaScript function, simply redeclare it with the exact same function name.

JavaScript
nebula.desktopNotification = function(title, message, clickCallback, showCallback, closeCallback, errorCallback){
    //Write your own code here, leave it blank, or return false.
}