Google chrome extension 如何在chrome扩展中实现带有声音的通知弹出窗口

Google chrome extension 如何在chrome扩展中实现带有声音的通知弹出窗口,google-chrome-extension,notifications,Google Chrome Extension,Notifications,如何在chrome扩展中实现带有声音的通知弹出窗口 与一样,您可以使用以下代码作为在桌面通知中播放声音的参考,它使用 我认为自从编写了可接受的答案后,createHTMLNotification已被弃用。对于现在发生在该线程上的任何人,假设您在清单中拥有通知权限,这里有一个截至2014年1月有效的方法: background.js createNotification(); audioNotification(); function audioNotification(){ var y

如何在chrome扩展中实现带有声音的通知弹出窗口


一样,您可以使用以下代码作为在桌面通知中播放声音的参考,它使用


我认为自从编写了可接受的答案后,
createHTMLNotification
已被弃用。对于现在发生在该线程上的任何人,假设您在清单中拥有
通知
权限,这里有一个截至2014年1月有效的方法:

background.js

createNotification();
audioNotification();

function audioNotification(){
    var yourSound = new Audio('yourSound.mp3');
    yourSound.play();
}

function createNotification(){
    var opt = {type: "basic",title: "Your Title",message: "Your message",iconUrl: "your_icon.png"}
    chrome.notifications.create("notificationName",opt,function(){});

    //include this line if you want to clear the notification after 5 seconds
    setTimeout(function(){chrome.notifications.clear("notificationName",function(){});},5000);
}

这里的总体想法是,您将发送常规通知,然后使用普通JavaScript方法在创建通知后立即播放声音。当然,还有其他方法来完成和组织它,但我认为这在大多数情况下都非常清楚并且非常容易实现。

createHTMLNotification
已被弃用-@Free下面的答案现在是正确的。
// create a HTML notification:
var notification = webkitNotifications.createHTMLNotification(
    'notification.html' // html url - can be relative
);

// Then show the notification.
notification.show();
<html>
    <body>
        <p>Some Nice Text While Playing Song.. </p>
        <audio autoplay>
        <source src="http://www.html5rocks.com/en/tutorials/audio/quick/test.mp3" type="audio/mpeg" />
        <source src="http://www.html5rocks.com/en/tutorials/audio/quick/test.ogg" type="audio/ogg" />
        </audio>
    </body>
</html>
createNotification();
audioNotification();

function audioNotification(){
    var yourSound = new Audio('yourSound.mp3');
    yourSound.play();
}

function createNotification(){
    var opt = {type: "basic",title: "Your Title",message: "Your message",iconUrl: "your_icon.png"}
    chrome.notifications.create("notificationName",opt,function(){});

    //include this line if you want to clear the notification after 5 seconds
    setTimeout(function(){chrome.notifications.clear("notificationName",function(){});},5000);
}