Skip to content

Instantly share code, notes, and snippets.

@t-kashima
Created September 9, 2012 06:02
Show Gist options
  • Select an option

  • Save t-kashima/3682868 to your computer and use it in GitHub Desktop.

Select an option

Save t-kashima/3682868 to your computer and use it in GitHub Desktop.
HTML5 WebkitNotifications Sample - ref: http://tech-gym.com/2011/02/html5/116.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Notification</title>
</head>
<body>
<button>Notification</button>
<script type="text/javascript" src="notification.js"></script>
</body>
</html>
document.querySelector('button').addEventListener('click', function(e) {
if (typeof window.webkitNotifications === 'undefined') { return false; }
var permission = webkitNotifications.checkPermission();
switch(permission) {
// 許可されている時
case 0:
var notification = webkitNotifications.createNotification(
'', // アイコンの画像ファイル
'通知です', // タイトル
'お知らせがきました' // 内容
);
notification.ondisplay = function() {
setTimeout(
function() {
notification.cancel();
},
2000
);
}
notification.show();
break;
// 許可されていない時
case 1:
webkitNotifications.requestPermission(function() {
// 「許可」もしくは「拒否」が押された後に呼ばれるコールバック
// どちらが押されたかを取得することはできない
});
break;
// 拒否されている時
case 2:
break;
}
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment