【问题标题】:Node-webkit notification soundNode-webkit 通知声音
【发布时间】:2014-10-14 11:40:29
【问题描述】:

我为我的 node-webkit 应用程序创建了一个函数来触发 OS X 通知。它工作得很好,但我想知道我是否可以设置系统或自定义声音来代替经典的 iPhone 嗡嗡声?

我查看了 Mozzila (https://developer.mozilla.org/en-US/docs/Web/API/notification) 的官方通知 API 文档,没有声音选项,但是也许 node-webkit 植入了该功能(无法想象他们没有)但如果他们这样做了,我似乎找不到任何关于它的文档。

所以我的问题是,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);
    };
}

【问题讨论】:

    标签: node.js macos node-webkit


    【解决方案1】:

    已在recent node-webkit pull request and is released. 中修复/删除了不需要的 iPhone 声音

    至于生成我自己的声音,我在原始通知对象周围使用了一个包装器,这样每当我调用通知显示命令时,我也会根据需要播放声音。

    /**
     * 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;
    }
    

    要使用它,我们只需使用 new 关键字调用它:

    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.');
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多