Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node-webkit notification sound

I've made a function for my node-webkit application to trigger a OS X notification. It works perfectly but I wonder if I can set a system or custom sound instead of the classic iPhone boing sound?

I've looked up the official notification API documentation from Mozzila (https://developer.mozilla.org/en-US/docs/Web/API/notification) and there is no sound option, however maybe node-webkit implanted that function (can't imagine they didn't) but if they did, I can't seem to find any documentation about it.

So my question is, is there a sound option for notifications in node-webkit?

function notif(title ,tekst, url){
    var notice = new Notification(title, {
        body: tekst
    });

    notice.onshow = function(evt) {
        setTimeout(function(){notice.close()}, 5000);
    }

    notice.onclick = function(evt) {
        gui.Shell.openExternal(url);
        setTimeout(function(){notice.close()}, 1000);
    };
}
like image 397
Tim K Avatar asked Aug 20 '14 23:08

Tim K


1 Answers

The unwanted iPhone sound has been fixed/removed in a recent node-webkit pull request and is released.

As for generating my own sounds, I use a wrapper around the raw notifications object so that whenever I call the notification show command I also play a sound, as appropriate.

/**
 * Use composition to expand capabilities of Notifications feature.
 */
function NotificationWrapper(appIcon, title, description, soundFile) {

    /**
     * A path to a sound file, like /sounds/notification.wav
     */        
    function playSound(soundFile) {
        if(soundFile === undefined) return; 
        var audio = document.createElement('audio');
        audio.src = soundFile;
        audio.play();
        audio = undefined;
    }

    /**
     * Show the notification here.
     */
    var notification = new window.Notification(title, {
        body: description,
        icon: appIcon
    });

    /**
     * Play the sound.
     */
    playSound(soundFile);

    /**
     * Return notification object to controller so we can bind click events.
     */
    return notification;
}

To use it, we simply invoke it using the new keyword:

var myNotification = new NotificationWrapper(
    '#',    // image icon path goes here
    'node-webkit is awesome',
    'Especially now that I can use my own sounds',
    '/sounds/notification.wav'
);

myNotification.addEventListener('click', function() { 
    console.log('You clicked the notification.');
});
like image 187
jmort253 Avatar answered Sep 29 '22 13:09

jmort253