Created
September 9, 2012 06:02
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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