OLD | NEW |
1 <h1>Desktop Notifications</h1> | 1 <h1>Desktop Notifications</h1> |
2 | 2 |
3 | 3 |
4 | 4 |
5 <p> | 5 <p> |
6 Use desktop notifications to notify users that something | 6 Use desktop notifications to notify users that something |
7 important has happened. | 7 important has happened. |
8 Notifications appear outside the browser window. | 8 Notifications appear outside the browser window. |
9 As the following snapshots show, | 9 As the following snapshots show, |
10 the details of how notifications look | 10 the details of how notifications look |
(...skipping 10 matching lines...) Expand all Loading... |
21 width="28%" style="margin:2em 0.5em 1em; border:1px solid black;" | 21 width="28%" style="margin:2em 0.5em 1em; border:1px solid black;" |
22 alt="Notifications on Ubuntu Linux"/> | 22 alt="Notifications on Ubuntu Linux"/> |
23 | 23 |
24 <p> | 24 <p> |
25 You create the notification window | 25 You create the notification window |
26 using a bit of JavaScript and, optionally, | 26 using a bit of JavaScript and, optionally, |
27 an HTML page packaged inside your extension. | 27 an HTML page packaged inside your extension. |
28 </p> | 28 </p> |
29 | 29 |
30 | 30 |
31 <h2 id="manifest">Manifest</h2> | 31 <h2 id="example">Example</h2> |
32 | 32 |
33 <p> | 33 <p>First, declare the <code>notifications</code> permission in your manifest:</p
> |
34 You can request the notification permission | 34 <pre> |
35 in the <a href="manifest.html">extension manifest</a>, | 35 { |
36 like this: | |
37 </p> | |
38 | |
39 <pre>{ | |
40 "name": "My extension", | 36 "name": "My extension", |
| 37 "manifest_version": 2, |
41 ... | 38 ... |
42 <b> "permissions": [ | 39 <b> "permissions": [ |
43 "notifications" | 40 "notifications" |
44 ]</b>, | 41 ]</b>, |
45 ... | 42 ... |
46 }</pre> | 43 // <strong>Note:</strong> Because of <a href="http://code.google.com/p/chromiu
m/issues/detail?id=134315">bug 134315</a>, you must declare any images you |
| 44 // want to use with createNotification() as a web accessible resource. |
| 45 <b> "web_accessible_resources": [ |
| 46 "48.png" |
| 47 ]</b>, |
| 48 } |
| 49 </pre> |
47 | 50 |
48 <p class="note"> | 51 <p>Then, use <code>webkitNotifications</code> object to create notifications:</p
> |
49 <b>Note:</b> Extensions that declare | |
50 the <code>notifications</code> permission | |
51 are always allowed to create notifications. | |
52 There is no need to call | |
53 <code>webkitNotifications.checkPermission()</code>. | |
54 </p> | |
55 | 52 |
56 <h2 id="communication">Communicating with other views</h2> | 53 <pre> |
| 54 // <strong>Note:</strong> There's no need to call webkitNotifications.checkPermi
ssion(). |
| 55 // Extensions that declare the <em>notifications</em> permission are always |
| 56 // allowed create notifications. |
| 57 |
| 58 // Create a simple text notification: |
| 59 var notification = webkitNotifications.createNotification( |
| 60 '48.png', // icon url - can be relative |
| 61 'Hello!', // notification title |
| 62 'Lorem ipsum...' // notification body text |
| 63 ); |
| 64 |
| 65 // Or create an HTML notification: |
| 66 var notification = webkitNotifications.createHTMLNotification( |
| 67 'notification.html' // html url - can be relative |
| 68 ); |
| 69 |
| 70 // Then show the notification. |
| 71 notification.show(); |
| 72 </pre> |
| 73 |
| 74 <h2>API Reference</h2> |
| 75 |
| 76 <p>See the <a href="http://dev.chromium.org/developers/design-documents/desktop-
notifications/api-specification">Desktop Notifications Draft Specification</a>.<
/p> |
| 77 |
| 78 |
| 79 <h2 id="communication">Communicating with Other Views</h2> |
57 | 80 |
58 <p> | 81 <p> |
59 You can communicate between a notification | 82 You can communicate between a notification |
60 and other views in your extension using | 83 and other views in your extension using |
61 <a href="extension.html#method-getBackgroundPage">getBackgroundPage()</a> and | 84 <a href="extension.html#method-getBackgroundPage">getBackgroundPage()</a> and |
62 <a href="extension.html#method-getViews">getViews()</a>. For example: | 85 <a href="extension.html#method-getViews">getViews()</a>. For example: |
63 </p> | 86 </p> |
64 | 87 |
65 <pre> | 88 <pre> |
66 // Inside a notification... | 89 // Inside a notification... |
67 chrome.extension.getBackgroundPage().doThing(); | 90 chrome.extension.getBackgroundPage().doThing(); |
68 | 91 |
69 // From the background page... | 92 // From the background page... |
70 chrome.extension.getViews({type:"notification"}).forEach(function(win) { | 93 chrome.extension.getViews({type:"notification"}).forEach(function(win) { |
71 win.doOtherThing(); | 94 win.doOtherThing(); |
72 }); | 95 }); |
73 </pre> | 96 </pre> |
74 | 97 |
75 | 98 |
76 <h2 id="examples"> Examples </h2> | 99 <h2 id="examples">More Examples</h2> |
77 | 100 |
78 <p> | 101 <p> |
79 You can find a simple example | 102 You can find a simple example |
80 of using notifications in the | 103 of using notifications in the |
81 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/api/notifications/">examples/api/notifications</a> | 104 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/api/notifications/">examples/api/notifications</a> |
82 directory. | 105 directory. |
83 For other examples | 106 For other examples |
84 and for help in viewing the source code, | 107 and for help in viewing the source code, |
85 see <a href="samples.html">Samples</a>. | 108 see <a href="samples.html">Samples</a>. |
86 </p> | 109 </p> |
87 | 110 |
88 <p> | 111 <p> |
89 Also see html5rocks.com's | 112 Also see html5rocks.com's |
90 <a href="http://www.html5rocks.com/tutorials/notifications/quick/">notifications
tutorial</a>. | 113 <a href="http://www.html5rocks.com/tutorials/notifications/quick/">notifications
tutorial</a>. |
91 Ignore the permission-related code; | 114 Ignore the permission-related code; |
92 it's unnecessary if you declare the "notifications" permission. | 115 it's unnecessary if you declare the "notifications" permission. |
93 </p> | 116 </p> |
94 | |
95 <h2 id="api">API</h2> | |
96 | |
97 <p> | |
98 The desktop notification API for extensions | |
99 is the same one that | |
100 is available to normal web pages. | |
101 As the following code shows, | |
102 you first create either a simple text notification | |
103 or an HTML notification, | |
104 and then you show the notification. | |
105 </p> | |
106 | |
107 <pre> | |
108 // Create a simple text notification: | |
109 var notification = webkitNotifications.createNotification( | |
110 '48.png', // icon url - can be relative | |
111 'Hello!', // notification title | |
112 'Lorem ipsum...' // notification body text | |
113 ); | |
114 | |
115 // Or create an HTML notification: | |
116 var notification = webkitNotifications.createHTMLNotification( | |
117 'notification.html' // html url - can be relative | |
118 ); | |
119 | |
120 // Then show the notification. | |
121 notification.show(); | |
122 </pre> | |
123 | |
124 <p>For complete API details, | |
125 see the <a href="http://dev.chromium.org/developers/design-documents/desktop-not
ifications/api-specification">Desktop notifications draft specification</a>.</p> | |
OLD | NEW |