window.webkitNotifications
chromeで動く
http://dev.shokai.org/test/webkit-notification/
$(function(){ $('input#notify').click(function(){ switch(window.webkitNotifications.checkPermission()){ case 1: window.webkitNotifications.requestPermission(); case 0: var notif = window.webkitNotifications.createNotification('shokai.jpg', "title", $('input#message').val()); notif.ondisplay = function(){ setTimeout(function(){ if(notif.cancel) notif.cancel(); }, 3000); }; notif.show(); } }); });
firefoxでも拡張いれればgrowlできる http://code.google.com/p/ff-html5notifications/
requestPermissionはクリックなどのユーザ操作イベントの後でないと動かない、つまりsetIntervalから呼び出してもダメなので、chatなどの新着の表示は
- 先にボタンやチェックボックスをクリックさせてpermissionを得る
- 新着が来た時にcreateNotificationしてshowする
の順にやらなければならない。
body.clickでも動いたので、実質ユーザが画面をロードして何か操作したらすぐに許可をブラウザ上部に表示させる事ができる
$('body').click(function(){ if(window.webkitNotifications.checkPermission() == 1){ window.webkitNotifications.requestPermission(); } });