|
| 1 | +<!DOCTYPE html> |
| 2 | + |
| 3 | +<html lang="en"> |
| 4 | + <head> |
| 5 | + <meta charset="utf-8" /> |
| 6 | + <title>Persistent notifications</title> |
| 7 | + <script src="page.js" defer></script> |
| 8 | + </head> |
| 9 | + |
| 10 | + <body> |
| 11 | + <button id="notify-me">Notify me!</button> |
| 12 | + |
| 13 | + <p>This website shows how to make and handle persistent notifications.</p> |
| 14 | + |
| 15 | + <p> |
| 16 | + It registers a service worker, whose job is to handle notifications. When |
| 17 | + the "Notify me!" button is clicked, the website does the following: |
| 18 | + </p> |
| 19 | + <ol> |
| 20 | + <li> |
| 21 | + Checks whether the website has permission to show notifications, asking |
| 22 | + for permission if necessary. |
| 23 | + </li> |
| 24 | + <li>If permission is granted, starts a one-second timer.</li> |
| 25 | + <li> |
| 26 | + When the timer expires, gets the service worker registration, waiting |
| 27 | + for it to become active if necessary. |
| 28 | + </li> |
| 29 | + <li> |
| 30 | + Calls <code>registration.showNotification()</code>, passing in two |
| 31 | + actions. |
| 32 | + </li> |
| 33 | + </ol> |
| 34 | + |
| 35 | + <p> |
| 36 | + The notification presents the actions to the user. If the user clicks the |
| 37 | + notification, then the browser starts the service worker, if it is not |
| 38 | + already running, and dispatches the |
| 39 | + <code>notificationclick</code> event. If the user selected an action, then |
| 40 | + the event object includes the selected action. |
| 41 | + </p> |
| 42 | + |
| 43 | + <p> |
| 44 | + The service worker's <code>notificationclick</code> handler checks the |
| 45 | + value of the event's <code>action</code>, and responds by opening the |
| 46 | + appropriate web page. |
| 47 | + </p> |
| 48 | + </body> |
| 49 | +</html> |
0 commit comments