LoginSignup
1
2

More than 5 years have passed since last update.

JavaScriptでデスクトップ通知を行うためのライブラリ. showメソッドにiconも追加しておきたいところ.

Last updated at Posted at 2012-03-16
Notification.js
Notification = {
    initialized : false,
    NOT_ALLOWED: 1,
    NOT_SUPPORTED: 2,

    // 初期化. 利用前に必ず実行すること
    init: function() {
        if( typeof webkitNotifications === 'undefined' ) {
            return false;
        }
        this.initialized = true;
        if( typeof webkitNotifications.PERMISSION_ALLOWED === 'undefined' ) {
            webkitNotifications.PERMISSION_ALLOWED = 0;
            webkitNotifications.PERMISSION_NOT_ALLOWED = 1;
            webkitNotifications.PERMISSION_DENIED = 2;
        }
        if( typeof webkitNotifications.permissionLevel === 'undefined' ) {
            webkitNotifications.__defineGetter__('permissionLevel', function() {
                return this.checkPermission();
            });
            webkitNotifications.__defineSetter__('permissionLevel', function() {
            });
        }
        if( !webkitNotifications.createWebNotification ) {
            webkitNotifications.createWebNotification = webkitNotifications.createHTMLNotification;
        }
        return true;
    },

    // 作成したNotificationオブジェクトを返す
    create: function(title, message, icon) {
                if( !this.initialized ) {
                    if( !this.init() ) {
                        // ブラウザがNotification APIに対応していない.
                        throw Notification.NOT_SUPPORTED;
                    }
                }
                if( webkitNotifications.permissionLevel !== webkitNotifications.PERMISSION_ALLOWED ) {
                    //許可されていない
                    throw Notification.NOT_ALLOWED;
                } else {
                    return webkitNotifications.createNotification(icon, title, message);
                }
            },

    // ユーザにダイアログを表示する許可をもらう.
    // この関数はユーザが発生させたマウスイベント等の中でのみ正しく実行される.
    ask: function(f) {
             console.log("デスクトップ通知の許可をユーザに求めます。");
             webkitNotifications.requestPermission(f||function(){});
         },

    // Method show Notification.
    show: function(title, message, timeout) {
        timeout = timeout || 3000;    // default timeout is 3sec(=3,000msec)
        try {
            var notification = Notification.create(title, message, "");
            notification.ondisplay = function() {
                setTimeout(function(){
                    notification.cancel();
                    console.log("Notification close because timeout");
                }, timeout);
            };
            notification.show();
            console.log("Notification showing");
        } catch(e) {
            switch(e) {
                case Notification.NOT_ALLOWED:
                    Notification.ask();
                    break;
            }
        }
    }
};

1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2