view browser_side/notification.py @ 415:fadbba1d793f

server_side: added support for SSL and related parameters: Full parameters list: -t, --connection_type= 'http', 'https' or 'both' (to launch both servers). [default: both] -p, --port= The port number to listen HTTP on. [default: 8080] -s, --port_https= The port number to listen HTTPS on. [default: 8443] -c, --ssl_certificate= PEM certificate with both private and public parts. [default: libervia.pem] -r, --redirect_to_https= automatically redirect from HTTP to HTTPS. [default: 0] -w, --security_warning= warn user that he is about to connect on HTTP. [default: 1]
author souliane <souliane@mailoo.org>
date Tue, 18 Mar 2014 15:59:38 +0100
parents 835a8ae799e7
children f9130176bc8d
line wrap: on
line source

from __pyjamas__ import JS


class Notification(object):
    """
    If the browser supports it, the user allowed it to and the tab is in the
    background, send desktop notifications on messages.

    Requires both Web Notifications and Page Visibility API.
    """

    def __init__(self):
        JS("""
        this.enabled = false;

        if (!('hidden' in document))
            document.hidden = false;

        if (!('Notification' in window))
            return;

        if (Notification.permission === 'granted')
            this.enabled = true;

        else if (Notification.permission === 'default') {
            Notification.requestPermission(function(permission){
                if (permission !== 'granted')
                    return;

                this.enabled = true;
            });
        }
        """)

    def notify(self, title, body, icon='/media/icons/apps/48/sat.png'):
        JS("""
        if (this.enabled && document.hidden) {
            notification = Notification(title, {body: body, icon: icon});

            // Probably won’t work, but it doesn’t hurt to try.
            notification.addEventListener('click', function() {
                window.focus();
            });
        }
        """)