Skip to Content
Menu

desktopNotification()

Show desktop notifications.

JavaScript February 7, 2021

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 »

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/utilities.js on line 883.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.desktopNotification = function(title, message = false, clickCallback, showCallback, closeCallback, errorCallback){
        if ( nebula.checkNotificationPermission() ){
            //Set defaults
            let defaults = {
                dir: "auto", //Direction ["auto", "ltr", "rtl"] (optional)
                lang: "en-US", //Language (optional)
                body: "", //Body message (optional)
                tag: Math.floor(Math.random()*10_000)+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 ( !message ){
                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;
                }
            }
    
            let 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();
                    }, 20_000);
                };
            }
            if ( closeCallback ){
                instance.onclose = function(){
                    closeCallback();
                };
            }
            if ( errorCallback ){
                instance.onerror = function(){
                    gtag('event', 'Exception', {
                        message: '(JS) Desktop Notification error',
                        fatal: false
                    });
                    errorCallback();
                };
            }
        }
    
        return false;
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).

    JavaScript

    For non-module import functions:

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


    For dynamically imported module function overrides:

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


    Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:

    jQuery(document).on('nebula_module_loaded', function(module){
        //Note that the module variable is also available to know which module specifically was imported
        if ( typeof nebula.desktopNotification === 'function' ){
            nebula.desktopNotification = function(title, message, clickCallback, showCallback, closeCallback, errorCallback){
                //Write your own code here, leave it blank, or return false.
            }
    	}
    });