Chromeのみ。
GmailがChrome Extensionなしで新着通知してくれるようになったのでやってみた。
- 許可を得ないと機能しない。
- 許可を得るためにはユーザのアクションが必要。(自動的に許可を求めることはできない)
ということで、初めてボタンをクリックした時は許可を与えてやるべし。
それから、もう一回ボタンをクリックするべし。
<input id="showNotification" type="button" value="show notification" onclick="showNotification()">
<script>
if (!window.webkitNotifications) {
document.getElementById('showNotification').disabled = "disabled";
}
function showNotification() {
if (window.webkitNotifications.checkPermission() == 0) {
var icon = 'http://2.bp.blogspot.com/_-552tFB6UQU/S6zfGefaPdI/AAAAAAAAARo/Yg6h_8NobXY/20100326003_samsung_galaxy_s.jpg';
var title = 'Chrome Notification Sample';
var message = 'This works only with Google Chrome Web Browser.';
var n = window.webkitNotifications.createNotification(icon, title, message);
n.ondisplay = function() {
setTimeout(function() { n.cancel(); }, 5000);
};
n.show();
} else {
window.webkitNotifications.requestPermission();
}
}
</script>

0 コメント:
コメントを投稿