繁体   English   中英

在第三方网站上推送html5通知时如何执行代码

[英]How to execute code when html5 notification is pushed on 3rd party site

我有一个使用通知的网站( gitter.im ),我想在推送通知时运行声音。 如何将此代码添加到通知中?

 (function() { var a = document.createElement('audio') a.setAttribute('autoplay', true) a.setAttribute('src', 'http://www.soundjay.com/button/beep-03.mp3') })(); 

您可以使用HTML5音频api,但在service worker中不起作用。

当浏览器收到通知时发出声音的唯一方法是通过postMessage(),您可以通知您的网页发出声音,但是只有在浏览器中打开了主站点页面时,它才起作用。

Chrome ServiceWorker postMessage

 var track = new Audio("http://oringz.com/oringz-uploads/sounds-874-gets-in-the-way.mp3"); track.play(); 

详细信息: http : //www.html5rocks.com/en/tutorials/webaudio/intro/

这似乎可行:

(function(Notif) {
  function play() {
    var a = document.createElement('audio')
    a.setAttribute('autoplay', true)
    a.setAttribute('src', 'http://www.soundjay.com/button/beep-03.mp3');
  }
  window.Notification = function(title, option) {
    var notif = new Notif(title, option);
    play();
    return notif;
  };
  for (var key in Notif) {
    if (Notif.hasOwnProperty(key)) {
      window.Notification[key] = Notif[key];
    }
  }
})(Notification);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM