comparison browser_side/notification.py @ 328:835a8ae799e7

Add notifications support, fixes bug 7.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 23 Feb 2013 16:27:32 +0100
parents
children f9130176bc8d
comparison
equal deleted inserted replaced
327:6126bd24e7dd 328:835a8ae799e7
1 from __pyjamas__ import JS
2
3
4 class Notification(object):
5 """
6 If the browser supports it, the user allowed it to and the tab is in the
7 background, send desktop notifications on messages.
8
9 Requires both Web Notifications and Page Visibility API.
10 """
11
12 def __init__(self):
13 JS("""
14 this.enabled = false;
15
16 if (!('hidden' in document))
17 document.hidden = false;
18
19 if (!('Notification' in window))
20 return;
21
22 if (Notification.permission === 'granted')
23 this.enabled = true;
24
25 else if (Notification.permission === 'default') {
26 Notification.requestPermission(function(permission){
27 if (permission !== 'granted')
28 return;
29
30 this.enabled = true;
31 });
32 }
33 """)
34
35 def notify(self, title, body, icon='/media/icons/apps/48/sat.png'):
36 JS("""
37 if (this.enabled && document.hidden) {
38 notification = Notification(title, {body: body, icon: icon});
39
40 // Probably won’t work, but it doesn’t hurt to try.
41 notification.addEventListener('click', function() {
42 window.focus();
43 });
44 }
45 """)