├── .eslintrc
├── .github
└── workflows
│ └── test.yml
├── .gitignore
├── .travis.yml
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── app
├── .eslintrc
├── favicon.ico
├── images
│ ├── badge.png
│ └── icon.png
├── index.html
├── scripts
│ └── main.js
├── styles
│ └── index.css
└── sw.js
├── completed
├── 01-register-sw
│ ├── images
│ │ ├── badge.png
│ │ └── icon.png
│ ├── index.html
│ ├── scripts
│ │ └── main.js
│ ├── styles
│ │ └── index.css
│ └── sw.js
├── 02-subscription-state
│ ├── images
│ │ ├── badge.png
│ │ └── icon.png
│ ├── index.html
│ ├── scripts
│ │ └── main.js
│ ├── styles
│ │ └── index.css
│ └── sw.js
├── 03-subscribe
│ ├── images
│ │ ├── badge.png
│ │ └── icon.png
│ ├── index.html
│ ├── scripts
│ │ └── main.js
│ ├── styles
│ │ └── index.css
│ └── sw.js
├── 04-permission-denied
│ ├── images
│ │ ├── badge.png
│ │ └── icon.png
│ ├── index.html
│ ├── scripts
│ │ └── main.js
│ ├── styles
│ │ └── index.css
│ └── sw.js
├── 05-push-event
│ ├── images
│ │ ├── badge.png
│ │ └── icon.png
│ ├── index.html
│ ├── scripts
│ │ └── main.js
│ ├── styles
│ │ └── index.css
│ └── sw.js
├── 06-click
│ ├── images
│ │ ├── badge.png
│ │ └── icon.png
│ ├── index.html
│ ├── scripts
│ │ └── main.js
│ ├── styles
│ │ └── index.css
│ └── sw.js
├── 07-unsubscribe
│ ├── images
│ │ ├── badge.png
│ │ └── icon.png
│ ├── index.html
│ ├── scripts
│ │ └── main.js
│ ├── styles
│ │ └── index.css
│ └── sw.js
└── 08-push-subscription-change
│ ├── images
│ ├── badge.png
│ └── icon.png
│ ├── index.html
│ ├── scripts
│ └── main.js
│ ├── styles
│ └── index.css
│ └── sw.js
├── package-lock.json
├── package.json
└── screenshots
├── 00-push-codelab.png
├── 01-push-codelab.png
├── 02-push-codelab.png
├── 04-push-codelab.png
├── 05-push-codelab.png
├── 06-push-codelab.png
├── 07-push-codelab.png
├── 08-push-codelab.png
├── 09-push-codelab.png
├── 10-push-codelab.png
├── 11-push-codelab.png
├── 12-push-codelab.png
├── 13-push-codelab.png
└── 14-push-codelab.png
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "extends": ["eslint:recommended", "google"],
3 | "rules": {
4 | "no-var": 2,
5 | "no-console": 0,
6 | "comma-dangle": 0,
7 | "require-jsdoc": 0,
8 | "no-useless-escape": 0,
9 | "indent": 0,
10 | "prefer-const": 0
11 | },
12 | "parserOptions": {
13 | "ecmaVersion": 6
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/.github/workflows/test.yml:
--------------------------------------------------------------------------------
1 | on: [push,pull_request]
2 | name: Test
3 | jobs:
4 | test:
5 | runs-on: ubuntu-latest
6 | steps:
7 | - uses: actions/checkout@v2
8 | - name: Use Node.js
9 | uses: actions/setup-node@v2
10 | - name: Install dependencies
11 | run: npm install
12 | - name: Run test
13 | run: npm test
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *~
2 | *.pyc
3 | *.swp
4 | .idea
5 | build
6 | .DS_Store
7 | .sass-cache/
8 | node_modules/
9 | .ruby-gemset
10 | .rvmrc
11 | .grunt-gae-pid
12 | src/css/styles.css
13 | src/css/styles.min.css
14 | dist
15 | .tmp
16 | npm-debug.log
17 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: required
2 | dist: trusty
3 | language: node_js
4 | cache:
5 | directories:
6 | - node_modules
7 |
8 | node_js:
9 | - 'stable'
10 |
11 | install:
12 | - npm install
13 |
14 | # Read more here: https://docs.travis-ci.com/user/gui-and-headless-browsers/#Using-xvfb-to-Run-Tests-That-Require-a-GUI
15 | before_script:
16 | - "export DISPLAY=:99.0"
17 | - "sh -e /etc/init.d/xvfb start || echo \"Unable to start virtual display.\""
18 | - sleep 3 # give xvfb some time to start
19 |
20 | script:
21 | - npm run test
22 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | Want to contribute? Great! First, read this page (including the small print at the end).
2 |
3 | ### Before you contribute
4 | Before we can use your code, you must sign the
5 | [Google Individual Contributor License Agreement](https://developers.google.com/open-source/cla/individual?csw=1)
6 | (CLA), which you can do online. The CLA is necessary mainly because you own the
7 | copyright to your changes, even after your contribution becomes part of our
8 | codebase, so we need your permission to use and distribute your code. We also
9 | need to be sure of various other things—for instance that you'll tell us if you
10 | know that your code infringes on other people's patents. You don't have to sign
11 | the CLA until after you've submitted your code for review and a member has
12 | approved it, but you must do it before we can put your code into our codebase.
13 | Before you start working on a larger contribution, you should get in touch with
14 | us first through the issue tracker with your idea so that we can help out and
15 | possibly guide you. Coordinating up front makes it much easier to avoid
16 | frustration later on.
17 |
18 | ### Code reviews
19 | All submissions, including submissions by project members, require review. We
20 | use Github pull requests for this purpose.
21 |
22 | ### The small print
23 | Contributions made by corporations are covered by a different agreement than
24 | the one above, the Software Grant and Corporate Contributor License Agreement.
25 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | All image and audio files (including *.png, *.jpg, *.svg, *.mp3, *.wav
2 | and *.ogg) are licensed under the CC-BY-NC license. All other files are
3 | licensed under the Apache 2 license.
4 |
5 | =======================================================================
6 |
7 |
8 | Apache License
9 | --------------
10 |
11 | Apache License
12 | Version 2.0, January 2004
13 | http://www.apache.org/licenses/
14 |
15 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
16 |
17 | 1. Definitions.
18 |
19 | "License" shall mean the terms and conditions for use, reproduction,
20 | and distribution as defined by Sections 1 through 9 of this document.
21 |
22 | "Licensor" shall mean the copyright owner or entity authorized by
23 | the copyright owner that is granting the License.
24 |
25 | "Legal Entity" shall mean the union of the acting entity and all
26 | other entities that control, are controlled by, or are under common
27 | control with that entity. For the purposes of this definition,
28 | "control" means (i) the power, direct or indirect, to cause the
29 | direction or management of such entity, whether by contract or
30 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
31 | outstanding shares, or (iii) beneficial ownership of such entity.
32 |
33 | "You" (or "Your") shall mean an individual or Legal Entity
34 | exercising permissions granted by this License.
35 |
36 | "Source" form shall mean the preferred form for making modifications,
37 | including but not limited to software source code, documentation
38 | source, and configuration files.
39 |
40 | "Object" form shall mean any form resulting from mechanical
41 | transformation or translation of a Source form, including but
42 | not limited to compiled object code, generated documentation,
43 | and conversions to other media types.
44 |
45 | "Work" shall mean the work of authorship, whether in Source or
46 | Object form, made available under the License, as indicated by a
47 | copyright notice that is included in or attached to the work
48 | (an example is provided in the Appendix below).
49 |
50 | "Derivative Works" shall mean any work, whether in Source or Object
51 | form, that is based on (or derived from) the Work and for which the
52 | editorial revisions, annotations, elaborations, or other modifications
53 | represent, as a whole, an original work of authorship. For the purposes
54 | of this License, Derivative Works shall not include works that remain
55 | separable from, or merely link (or bind by name) to the interfaces of,
56 | the Work and Derivative Works thereof.
57 |
58 | "Contribution" shall mean any work of authorship, including
59 | the original version of the Work and any modifications or additions
60 | to that Work or Derivative Works thereof, that is intentionally
61 | submitted to Licensor for inclusion in the Work by the copyright owner
62 | or by an individual or Legal Entity authorized to submit on behalf of
63 | the copyright owner. For the purposes of this definition, "submitted"
64 | means any form of electronic, verbal, or written communication sent
65 | to the Licensor or its representatives, including but not limited to
66 | communication on electronic mailing lists, source code control systems,
67 | and issue tracking systems that are managed by, or on behalf of, the
68 | Licensor for the purpose of discussing and improving the Work, but
69 | excluding communication that is conspicuously marked or otherwise
70 | designated in writing by the copyright owner as "Not a Contribution."
71 |
72 | "Contributor" shall mean Licensor and any individual or Legal Entity
73 | on behalf of whom a Contribution has been received by Licensor and
74 | subsequently incorporated within the Work.
75 |
76 | 2. Grant of Copyright License. Subject to the terms and conditions of
77 | this License, each Contributor hereby grants to You a perpetual,
78 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
79 | copyright license to reproduce, prepare Derivative Works of,
80 | publicly display, publicly perform, sublicense, and distribute the
81 | Work and such Derivative Works in Source or Object form.
82 |
83 | 3. Grant of Patent License. Subject to the terms and conditions of
84 | this License, each Contributor hereby grants to You a perpetual,
85 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
86 | (except as stated in this section) patent license to make, have made,
87 | use, offer to sell, sell, import, and otherwise transfer the Work,
88 | where such license applies only to those patent claims licensable
89 | by such Contributor that are necessarily infringed by their
90 | Contribution(s) alone or by combination of their Contribution(s)
91 | with the Work to which such Contribution(s) was submitted. If You
92 | institute patent litigation against any entity (including a
93 | cross-claim or counterclaim in a lawsuit) alleging that the Work
94 | or a Contribution incorporated within the Work constitutes direct
95 | or contributory patent infringement, then any patent licenses
96 | granted to You under this License for that Work shall terminate
97 | as of the date such litigation is filed.
98 |
99 | 4. Redistribution. You may reproduce and distribute copies of the
100 | Work or Derivative Works thereof in any medium, with or without
101 | modifications, and in Source or Object form, provided that You
102 | meet the following conditions:
103 |
104 | (a) You must give any other recipients of the Work or
105 | Derivative Works a copy of this License; and
106 |
107 | (b) You must cause any modified files to carry prominent notices
108 | stating that You changed the files; and
109 |
110 | (c) You must retain, in the Source form of any Derivative Works
111 | that You distribute, all copyright, patent, trademark, and
112 | attribution notices from the Source form of the Work,
113 | excluding those notices that do not pertain to any part of
114 | the Derivative Works; and
115 |
116 | (d) If the Work includes a "NOTICE" text file as part of its
117 | distribution, then any Derivative Works that You distribute must
118 | include a readable copy of the attribution notices contained
119 | within such NOTICE file, excluding those notices that do not
120 | pertain to any part of the Derivative Works, in at least one
121 | of the following places: within a NOTICE text file distributed
122 | as part of the Derivative Works; within the Source form or
123 | documentation, if provided along with the Derivative Works; or,
124 | within a display generated by the Derivative Works, if and
125 | wherever such third-party notices normally appear. The contents
126 | of the NOTICE file are for informational purposes only and
127 | do not modify the License. You may add Your own attribution
128 | notices within Derivative Works that You distribute, alongside
129 | or as an addendum to the NOTICE text from the Work, provided
130 | that such additional attribution notices cannot be construed
131 | as modifying the License.
132 |
133 | You may add Your own copyright statement to Your modifications and
134 | may provide additional or different license terms and conditions
135 | for use, reproduction, or distribution of Your modifications, or
136 | for any such Derivative Works as a whole, provided Your use,
137 | reproduction, and distribution of the Work otherwise complies with
138 | the conditions stated in this License.
139 |
140 | 5. Submission of Contributions. Unless You explicitly state otherwise,
141 | any Contribution intentionally submitted for inclusion in the Work
142 | by You to the Licensor shall be under the terms and conditions of
143 | this License, without any additional terms or conditions.
144 | Notwithstanding the above, nothing herein shall supersede or modify
145 | the terms of any separate license agreement you may have executed
146 | with Licensor regarding such Contributions.
147 |
148 | 6. Trademarks. This License does not grant permission to use the trade
149 | names, trademarks, service marks, or product names of the Licensor,
150 | except as required for reasonable and customary use in describing the
151 | origin of the Work and reproducing the content of the NOTICE file.
152 |
153 | 7. Disclaimer of Warranty. Unless required by applicable law or
154 | agreed to in writing, Licensor provides the Work (and each
155 | Contributor provides its Contributions) on an "AS IS" BASIS,
156 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
157 | implied, including, without limitation, any warranties or conditions
158 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
159 | PARTICULAR PURPOSE. You are solely responsible for determining the
160 | appropriateness of using or redistributing the Work and assume any
161 | risks associated with Your exercise of permissions under this License.
162 |
163 | 8. Limitation of Liability. In no event and under no legal theory,
164 | whether in tort (including negligence), contract, or otherwise,
165 | unless required by applicable law (such as deliberate and grossly
166 | negligent acts) or agreed to in writing, shall any Contributor be
167 | liable to You for damages, including any direct, indirect, special,
168 | incidental, or consequential damages of any character arising as a
169 | result of this License or out of the use or inability to use the
170 | Work (including but not limited to damages for loss of goodwill,
171 | work stoppage, computer failure or malfunction, or any and all
172 | other commercial damages or losses), even if such Contributor
173 | has been advised of the possibility of such damages.
174 |
175 | 9. Accepting Warranty or Additional Liability. While redistributing
176 | the Work or Derivative Works thereof, You may choose to offer,
177 | and charge a fee for, acceptance of support, warranty, indemnity,
178 | or other liability obligations and/or rights consistent with this
179 | License. However, in accepting such obligations, You may act only
180 | on Your own behalf and on Your sole responsibility, not on behalf
181 | of any other Contributor, and only if You agree to indemnify,
182 | defend, and hold each Contributor harmless for any liability
183 | incurred by, or claims asserted against, such Contributor by reason
184 | of your accepting any such warranty or additional liability.
185 |
186 | END OF TERMS AND CONDITIONS
187 |
188 | All image and audio files (including *.png, *.jpg, *.svg, *.mp3, *.wav
189 | and *.ogg) are licensed under the CC-BY-NC license. All other files are
190 | licensed under the Apache 2 license.
191 |
192 | CC-BY-NC License
193 | ----------------
194 |
195 | Attribution-NonCommercial-ShareAlike 4.0 International
196 |
197 | =======================================================================
198 |
199 | Creative Commons Corporation ("Creative Commons") is not a law firm and
200 | does not provide legal services or legal advice. Distribution of
201 | Creative Commons public licenses does not create a lawyer-client or
202 | other relationship. Creative Commons makes its licenses and related
203 | information available on an "as-is" basis. Creative Commons gives no
204 | warranties regarding its licenses, any material licensed under their
205 | terms and conditions, or any related information. Creative Commons
206 | disclaims all liability for damages resulting from their use to the
207 | fullest extent possible.
208 |
209 | Using Creative Commons Public Licenses
210 |
211 | Creative Commons public licenses provide a standard set of terms and
212 | conditions that creators and other rights holders may use to share
213 | original works of authorship and other material subject to copyright
214 | and certain other rights specified in the public license below. The
215 | following considerations are for informational purposes only, are not
216 | exhaustive, and do not form part of our licenses.
217 |
218 | Considerations for licensors: Our public licenses are
219 | intended for use by those authorized to give the public
220 | permission to use material in ways otherwise restricted by
221 | copyright and certain other rights. Our licenses are
222 | irrevocable. Licensors should read and understand the terms
223 | and conditions of the license they choose before applying it.
224 | Licensors should also secure all rights necessary before
225 | applying our licenses so that the public can reuse the
226 | material as expected. Licensors should clearly mark any
227 | material not subject to the license. This includes other CC-
228 | licensed material, or material used under an exception or
229 | limitation to copyright. More considerations for licensors:
230 | wiki.creativecommons.org/Considerations_for_licensors
231 |
232 | Considerations for the public: By using one of our public
233 | licenses, a licensor grants the public permission to use the
234 | licensed material under specified terms and conditions. If
235 | the licensor's permission is not necessary for any reason--for
236 | example, because of any applicable exception or limitation to
237 | copyright--then that use is not regulated by the license. Our
238 | licenses grant only permissions under copyright and certain
239 | other rights that a licensor has authority to grant. Use of
240 | the licensed material may still be restricted for other
241 | reasons, including because others have copyright or other
242 | rights in the material. A licensor may make special requests,
243 | such as asking that all changes be marked or described.
244 | Although not required by our licenses, you are encouraged to
245 | respect those requests where reasonable. More_considerations
246 | for the public:
247 | wiki.creativecommons.org/Considerations_for_licensees
248 |
249 | =======================================================================
250 |
251 | Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
252 | Public License
253 |
254 | By exercising the Licensed Rights (defined below), You accept and agree
255 | to be bound by the terms and conditions of this Creative Commons
256 | Attribution-NonCommercial-ShareAlike 4.0 International Public License
257 | ("Public License"). To the extent this Public License may be
258 | interpreted as a contract, You are granted the Licensed Rights in
259 | consideration of Your acceptance of these terms and conditions, and the
260 | Licensor grants You such rights in consideration of benefits the
261 | Licensor receives from making the Licensed Material available under
262 | these terms and conditions.
263 |
264 |
265 | Section 1 -- Definitions.
266 |
267 | a. Adapted Material means material subject to Copyright and Similar
268 | Rights that is derived from or based upon the Licensed Material
269 | and in which the Licensed Material is translated, altered,
270 | arranged, transformed, or otherwise modified in a manner requiring
271 | permission under the Copyright and Similar Rights held by the
272 | Licensor. For purposes of this Public License, where the Licensed
273 | Material is a musical work, performance, or sound recording,
274 | Adapted Material is always produced where the Licensed Material is
275 | synched in timed relation with a moving image.
276 |
277 | b. Adapter's License means the license You apply to Your Copyright
278 | and Similar Rights in Your contributions to Adapted Material in
279 | accordance with the terms and conditions of this Public License.
280 |
281 | c. BY-NC-SA Compatible License means a license listed at
282 | creativecommons.org/compatiblelicenses, approved by Creative
283 | Commons as essentially the equivalent of this Public License.
284 |
285 | d. Copyright and Similar Rights means copyright and/or similar rights
286 | closely related to copyright including, without limitation,
287 | performance, broadcast, sound recording, and Sui Generis Database
288 | Rights, without regard to how the rights are labeled or
289 | categorized. For purposes of this Public License, the rights
290 | specified in Section 2(b)(1)-(2) are not Copyright and Similar
291 | Rights.
292 |
293 | e. Effective Technological Measures means those measures that, in the
294 | absence of proper authority, may not be circumvented under laws
295 | fulfilling obligations under Article 11 of the WIPO Copyright
296 | Treaty adopted on December 20, 1996, and/or similar international
297 | agreements.
298 |
299 | f. Exceptions and Limitations means fair use, fair dealing, and/or
300 | any other exception or limitation to Copyright and Similar Rights
301 | that applies to Your use of the Licensed Material.
302 |
303 | g. License Elements means the license attributes listed in the name
304 | of a Creative Commons Public License. The License Elements of this
305 | Public License are Attribution, NonCommercial, and ShareAlike.
306 |
307 | h. Licensed Material means the artistic or literary work, database,
308 | or other material to which the Licensor applied this Public
309 | License.
310 |
311 | i. Licensed Rights means the rights granted to You subject to the
312 | terms and conditions of this Public License, which are limited to
313 | all Copyright and Similar Rights that apply to Your use of the
314 | Licensed Material and that the Licensor has authority to license.
315 |
316 | j. Licensor means the individual(s) or entity(ies) granting rights
317 | under this Public License.
318 |
319 | k. NonCommercial means not primarily intended for or directed towards
320 | commercial advantage or monetary compensation. For purposes of
321 | this Public License, the exchange of the Licensed Material for
322 | other material subject to Copyright and Similar Rights by digital
323 | file-sharing or similar means is NonCommercial provided there is
324 | no payment of monetary compensation in connection with the
325 | exchange.
326 |
327 | l. Share means to provide material to the public by any means or
328 | process that requires permission under the Licensed Rights, such
329 | as reproduction, public display, public performance, distribution,
330 | dissemination, communication, or importation, and to make material
331 | available to the public including in ways that members of the
332 | public may access the material from a place and at a time
333 | individually chosen by them.
334 |
335 | m. Sui Generis Database Rights means rights other than copyright
336 | resulting from Directive 96/9/EC of the European Parliament and of
337 | the Council of 11 March 1996 on the legal protection of databases,
338 | as amended and/or succeeded, as well as other essentially
339 | equivalent rights anywhere in the world.
340 |
341 | n. You means the individual or entity exercising the Licensed Rights
342 | under this Public License. Your has a corresponding meaning.
343 |
344 |
345 | Section 2 -- Scope.
346 |
347 | a. License grant.
348 |
349 | 1. Subject to the terms and conditions of this Public License,
350 | the Licensor hereby grants You a worldwide, royalty-free,
351 | non-sublicensable, non-exclusive, irrevocable license to
352 | exercise the Licensed Rights in the Licensed Material to:
353 |
354 | a. reproduce and Share the Licensed Material, in whole or
355 | in part, for NonCommercial purposes only; and
356 |
357 | b. produce, reproduce, and Share Adapted Material for
358 | NonCommercial purposes only.
359 |
360 | 2. Exceptions and Limitations. For the avoidance of doubt, where
361 | Exceptions and Limitations apply to Your use, this Public
362 | License does not apply, and You do not need to comply with
363 | its terms and conditions.
364 |
365 | 3. Term. The term of this Public License is specified in Section
366 | 6(a).
367 |
368 | 4. Media and formats; technical modifications allowed. The
369 | Licensor authorizes You to exercise the Licensed Rights in
370 | all media and formats whether now known or hereafter created,
371 | and to make technical modifications necessary to do so. The
372 | Licensor waives and/or agrees not to assert any right or
373 | authority to forbid You from making technical modifications
374 | necessary to exercise the Licensed Rights, including
375 | technical modifications necessary to circumvent Effective
376 | Technological Measures. For purposes of this Public License,
377 | simply making modifications authorized by this Section 2(a)
378 | (4) never produces Adapted Material.
379 |
380 | 5. Downstream recipients.
381 |
382 | a. Offer from the Licensor -- Licensed Material. Every
383 | recipient of the Licensed Material automatically
384 | receives an offer from the Licensor to exercise the
385 | Licensed Rights under the terms and conditions of this
386 | Public License.
387 |
388 | b. Additional offer from the Licensor -- Adapted Material.
389 | Every recipient of Adapted Material from You
390 | automatically receives an offer from the Licensor to
391 | exercise the Licensed Rights in the Adapted Material
392 | under the conditions of the Adapter's License You apply.
393 |
394 | c. No downstream restrictions. You may not offer or impose
395 | any additional or different terms or conditions on, or
396 | apply any Effective Technological Measures to, the
397 | Licensed Material if doing so restricts exercise of the
398 | Licensed Rights by any recipient of the Licensed
399 | Material.
400 |
401 | 6. No endorsement. Nothing in this Public License constitutes or
402 | may be construed as permission to assert or imply that You
403 | are, or that Your use of the Licensed Material is, connected
404 | with, or sponsored, endorsed, or granted official status by,
405 | the Licensor or others designated to receive attribution as
406 | provided in Section 3(a)(1)(A)(i).
407 |
408 | b. Other rights.
409 |
410 | 1. Moral rights, such as the right of integrity, are not
411 | licensed under this Public License, nor are publicity,
412 | privacy, and/or other similar personality rights; however, to
413 | the extent possible, the Licensor waives and/or agrees not to
414 | assert any such rights held by the Licensor to the limited
415 | extent necessary to allow You to exercise the Licensed
416 | Rights, but not otherwise.
417 |
418 | 2. Patent and trademark rights are not licensed under this
419 | Public License.
420 |
421 | 3. To the extent possible, the Licensor waives any right to
422 | collect royalties from You for the exercise of the Licensed
423 | Rights, whether directly or through a collecting society
424 | under any voluntary or waivable statutory or compulsory
425 | licensing scheme. In all other cases the Licensor expressly
426 | reserves any right to collect such royalties, including when
427 | the Licensed Material is used other than for NonCommercial
428 | purposes.
429 |
430 |
431 | Section 3 -- License Conditions.
432 |
433 | Your exercise of the Licensed Rights is expressly made subject to the
434 | following conditions.
435 |
436 | a. Attribution.
437 |
438 | 1. If You Share the Licensed Material (including in modified
439 | form), You must:
440 |
441 | a. retain the following if it is supplied by the Licensor
442 | with the Licensed Material:
443 |
444 | i. identification of the creator(s) of the Licensed
445 | Material and any others designated to receive
446 | attribution, in any reasonable manner requested by
447 | the Licensor (including by pseudonym if
448 | designated);
449 |
450 | ii. a copyright notice;
451 |
452 | iii. a notice that refers to this Public License;
453 |
454 | iv. a notice that refers to the disclaimer of
455 | warranties;
456 |
457 | v. a URI or hyperlink to the Licensed Material to the
458 | extent reasonably practicable;
459 |
460 | b. indicate if You modified the Licensed Material and
461 | retain an indication of any previous modifications; and
462 |
463 | c. indicate the Licensed Material is licensed under this
464 | Public License, and include the text of, or the URI or
465 | hyperlink to, this Public License.
466 |
467 | 2. You may satisfy the conditions in Section 3(a)(1) in any
468 | reasonable manner based on the medium, means, and context in
469 | which You Share the Licensed Material. For example, it may be
470 | reasonable to satisfy the conditions by providing a URI or
471 | hyperlink to a resource that includes the required
472 | information.
473 | 3. If requested by the Licensor, You must remove any of the
474 | information required by Section 3(a)(1)(A) to the extent
475 | reasonably practicable.
476 |
477 | b. ShareAlike.
478 |
479 | In addition to the conditions in Section 3(a), if You Share
480 | Adapted Material You produce, the following conditions also apply.
481 |
482 | 1. The Adapter's License You apply must be a Creative Commons
483 | license with the same License Elements, this version or
484 | later, or a BY-NC-SA Compatible License.
485 |
486 | 2. You must include the text of, or the URI or hyperlink to, the
487 | Adapter's License You apply. You may satisfy this condition
488 | in any reasonable manner based on the medium, means, and
489 | context in which You Share Adapted Material.
490 |
491 | 3. You may not offer or impose any additional or different terms
492 | or conditions on, or apply any Effective Technological
493 | Measures to, Adapted Material that restrict exercise of the
494 | rights granted under the Adapter's License You apply.
495 |
496 |
497 | Section 4 -- Sui Generis Database Rights.
498 |
499 | Where the Licensed Rights include Sui Generis Database Rights that
500 | apply to Your use of the Licensed Material:
501 |
502 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right
503 | to extract, reuse, reproduce, and Share all or a substantial
504 | portion of the contents of the database for NonCommercial purposes
505 | only;
506 |
507 | b. if You include all or a substantial portion of the database
508 | contents in a database in which You have Sui Generis Database
509 | Rights, then the database in which You have Sui Generis Database
510 | Rights (but not its individual contents) is Adapted Material,
511 | including for purposes of Section 3(b); and
512 |
513 | c. You must comply with the conditions in Section 3(a) if You Share
514 | all or a substantial portion of the contents of the database.
515 |
516 | For the avoidance of doubt, this Section 4 supplements and does not
517 | replace Your obligations under this Public License where the Licensed
518 | Rights include other Copyright and Similar Rights.
519 |
520 |
521 | Section 5 -- Disclaimer of Warranties and Limitation of Liability.
522 |
523 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE
524 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS
525 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF
526 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS,
527 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION,
528 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR
529 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS,
530 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT
531 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT
532 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU.
533 |
534 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE
535 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
536 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT,
537 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES,
538 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR
539 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN
540 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR
541 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR
542 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU.
543 |
544 | c. The disclaimer of warranties and limitation of liability provided
545 | above shall be interpreted in a manner that, to the extent
546 | possible, most closely approximates an absolute disclaimer and
547 | waiver of all liability.
548 |
549 |
550 | Section 6 -- Term and Termination.
551 |
552 | a. This Public License applies for the term of the Copyright and
553 | Similar Rights licensed here. However, if You fail to comply with
554 | this Public License, then Your rights under this Public License
555 | terminate automatically.
556 |
557 | b. Where Your right to use the Licensed Material has terminated under
558 | Section 6(a), it reinstates:
559 |
560 | 1. automatically as of the date the violation is cured, provided
561 | it is cured within 30 days of Your discovery of the
562 | violation; or
563 |
564 | 2. upon express reinstatement by the Licensor.
565 |
566 | For the avoidance of doubt, this Section 6(b) does not affect any
567 | right the Licensor may have to seek remedies for Your violations
568 | of this Public License.
569 |
570 | c. For the avoidance of doubt, the Licensor may also offer the
571 | Licensed Material under separate terms or conditions or stop
572 | distributing the Licensed Material at any time; however, doing so
573 | will not terminate this Public License.
574 |
575 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public
576 | License.
577 |
578 |
579 | Section 7 -- Other Terms and Conditions.
580 |
581 | a. The Licensor shall not be bound by any additional or different
582 | terms or conditions communicated by You unless expressly agreed.
583 |
584 | b. Any arrangements, understandings, or agreements regarding the
585 | Licensed Material not stated herein are separate from and
586 | independent of the terms and conditions of this Public License.
587 |
588 |
589 | Section 8 -- Interpretation.
590 |
591 | a. For the avoidance of doubt, this Public License does not, and
592 | shall not be interpreted to, reduce, limit, restrict, or impose
593 | conditions on any use of the Licensed Material that could lawfully
594 | be made without permission under this Public License.
595 |
596 | b. To the extent possible, if any provision of this Public License is
597 | deemed unenforceable, it shall be automatically reformed to the
598 | minimum extent necessary to make it enforceable. If the provision
599 | cannot be reformed, it shall be severed from this Public License
600 | without affecting the enforceability of the remaining terms and
601 | conditions.
602 |
603 | c. No term or condition of this Public License will be waived and no
604 | failure to comply consented to unless expressly agreed to by the
605 | Licensor.
606 |
607 | d. Nothing in this Public License constitutes or may be interpreted
608 | as a limitation upon, or waiver of, any privileges and immunities
609 | that apply to the Licensor or You, including from the legal
610 | processes of any jurisdiction or authority.
611 |
612 | =======================================================================
613 |
614 | Creative Commons is not a party to its public licenses.
615 | Notwithstanding, Creative Commons may elect to apply one of its public
616 | licenses to material it publishes and in those instances will be
617 | considered the "Licensor." Except for the limited purpose of indicating
618 | that material is shared under a Creative Commons public license or as
619 | otherwise permitted by the Creative Commons policies published at
620 | creativecommons.org/policies, Creative Commons does not authorize the
621 | use of the trademark "Creative Commons" or any other trademark or logo
622 | of Creative Commons without its prior written consent including,
623 | without limitation, in connection with any unauthorized modifications
624 | to any of its public licenses or any other arrangements,
625 | understandings, or agreements concerning use of licensed material. For
626 | the avoidance of doubt, this paragraph does not form part of the public
627 | licenses.
628 |
629 | Creative Commons may be contacted at creativecommons.org.
630 |
631 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Push Notifications codelab
2 |
3 | Code for the Web Fundamentals [Push Notifications codelab](https://codelabs.developers.google.com/codelabs/push-notifications/).
4 |
5 | In this codelab, you'll learn how to add Push Notifications to web applications. This will enable you to re-engage users with breaking news and information about
6 | new content.
7 |
8 | You'll also learn the basics of Service Workers.
9 |
10 | ## What you'll learn
11 |
12 | * Service Worker basics: installation and event handling
13 | * How to set up a Google Cloud Messaging (GCM) account
14 | * How to add a web manifest
15 | * Techniques for requesting GCM to send a notification to a web client
16 | * Notification display
17 | * Notification click handling
18 |
19 | Example code for each step of the codelab is available from the [completed](completed/) directory.
20 |
21 | ## License
22 |
23 | Copyright 2015 Google, Inc.
24 |
25 | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
26 |
27 | http://www.apache.org/licenses/LICENSE-2.0
28 |
29 | Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
30 |
--------------------------------------------------------------------------------
/app/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "rules": {
3 | "no-unused-vars": 0
4 | }
5 | }
6 |
--------------------------------------------------------------------------------
/app/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/app/favicon.ico
--------------------------------------------------------------------------------
/app/images/badge.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/app/images/badge.png
--------------------------------------------------------------------------------
/app/images/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/app/images/icon.png
--------------------------------------------------------------------------------
/app/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
Welcome to the push messaging codelab. The button below needs to be
24 | fixed to support subscribing to push.
25 |
26 |
29 |
30 |
31 |
Once you've subscribed your user, you'd send their subscription to your
32 | server to store in a database so that when you want to send a message
33 | you can lookup the subscription and send a message to it.
34 |
To simplify things for this code lab copy the following details
35 | into the Push Companion
36 | Site and it'll send a push message for you, using the application
37 | server keys on the site - so make sure they match.
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/app/scripts/main.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, es6 */
21 |
22 | 'use strict';
23 |
24 | const applicationServerPublicKey = '';
25 |
26 | const pushButton = document.querySelector('.js-push-btn');
27 |
28 | let isSubscribed = false;
29 | let swRegistration = null;
30 |
31 | function urlB64ToUint8Array(base64String) {
32 | const padding = '='.repeat((4 - base64String.length % 4) % 4);
33 | const base64 = (base64String + padding)
34 | .replace(/\-/g, '+')
35 | .replace(/_/g, '/');
36 |
37 | const rawData = window.atob(base64);
38 | const outputArray = new Uint8Array(rawData.length);
39 |
40 | for (let i = 0; i < rawData.length; ++i) {
41 | outputArray[i] = rawData.charCodeAt(i);
42 | }
43 | return outputArray;
44 | }
45 |
--------------------------------------------------------------------------------
/app/styles/index.css:
--------------------------------------------------------------------------------
1 | html {
2 | height: 100%;
3 | }
4 |
5 | html, body {
6 | width: 100%;
7 | padding: 0;
8 | margin: 0;
9 | }
10 |
11 | body {
12 | min-height: auto;
13 | box-sizing: border-box;
14 | }
15 |
16 | header {
17 | padding: 115px 0 32px 0;
18 | background-color: #00bcd4;
19 | color: white;
20 | }
21 |
22 | main, header > h1 {
23 | padding: 0 16px;
24 | max-width: 760px;
25 | box-sizing: border-box;
26 | margin: 0 auto;
27 | }
28 |
29 | main {
30 | margin: 24px auto;
31 | box-sizing: border-box;
32 | }
33 |
34 | pre {
35 | white-space: pre-wrap;
36 | background-color: #EEEEEE;
37 | padding: 16px;
38 | }
39 |
40 | pre code {
41 | word-break: break-word;
42 | }
43 |
44 | .is-invisible {
45 | opacity: 0;
46 | }
47 |
48 | .subscription-details {
49 | transition: opacity 1s;
50 | }
51 |
52 | @media (max-width: 600px) {
53 | header > h1 {
54 | font-size: 36px;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/app/sw.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, serviceworker, es6 */
21 |
22 | 'use strict';
23 |
--------------------------------------------------------------------------------
/completed/01-register-sw/images/badge.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/01-register-sw/images/badge.png
--------------------------------------------------------------------------------
/completed/01-register-sw/images/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/01-register-sw/images/icon.png
--------------------------------------------------------------------------------
/completed/01-register-sw/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Push Codelab
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
Push Codelab
20 |
21 |
22 |
23 |
Welcome to the push messaging codelab. The button below needs to be
24 | fixed to support subscribing to push.
25 |
26 |
29 |
30 |
31 |
Once you've subscribed your user, you'd send their subscription to your
32 | server to store in a database so that when you want to send a message
33 | you can lookup the subscription and send a message to it.
34 |
To simplify things for this code lab copy the following details
35 | into the Push Companion
36 | Site and it'll send a push message for you, using the application
37 | server keys on the site - so make sure they match.
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/completed/01-register-sw/scripts/main.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, es6 */
21 | /* eslint-disable no-unused-vars */
22 |
23 | 'use strict';
24 |
25 | const applicationServerPublicKey = '';
26 |
27 | const pushButton = document.querySelector('.js-push-btn');
28 |
29 | let isSubscribed = false;
30 | let swRegistration = null;
31 |
32 | function urlB64ToUint8Array(base64String) {
33 | const padding = '='.repeat((4 - base64String.length % 4) % 4);
34 | const base64 = (base64String + padding)
35 | .replace(/\-/g, '+')
36 | .replace(/_/g, '/');
37 |
38 | const rawData = window.atob(base64);
39 | const outputArray = new Uint8Array(rawData.length);
40 |
41 | for (let i = 0; i < rawData.length; ++i) {
42 | outputArray[i] = rawData.charCodeAt(i);
43 | }
44 | return outputArray;
45 | }
46 |
47 | if ('serviceWorker' in navigator && 'PushManager' in window) {
48 | console.log('Service Worker and Push is supported');
49 |
50 | navigator.serviceWorker.register('sw.js')
51 | .then(function(swReg) {
52 | console.log('Service Worker is registered', swReg);
53 |
54 | swRegistration = swReg;
55 | })
56 | .catch(function(error) {
57 | console.error('Service Worker Error', error);
58 | });
59 | } else {
60 | console.warn('Push messaging is not supported');
61 | pushButton.textContent = 'Push Not Supported';
62 | }
63 |
--------------------------------------------------------------------------------
/completed/01-register-sw/styles/index.css:
--------------------------------------------------------------------------------
1 | html {
2 | height: 100%;
3 | }
4 |
5 | html, body {
6 | width: 100%;
7 | padding: 0;
8 | margin: 0;
9 | }
10 |
11 | body {
12 | min-height: auto;
13 | box-sizing: border-box;
14 | }
15 |
16 | header {
17 | padding: 115px 0 32px 0;
18 | background-color: #00bcd4;
19 | color: white;
20 | }
21 |
22 | main, header > h1 {
23 | padding: 0 16px;
24 | max-width: 760px;
25 | box-sizing: border-box;
26 | margin: 0 auto;
27 | }
28 |
29 | main {
30 | margin: 24px auto;
31 | box-sizing: border-box;
32 | }
33 |
34 | pre {
35 | white-space: pre-wrap;
36 | background-color: #EEEEEE;
37 | padding: 16px;
38 | }
39 |
40 | pre code {
41 | word-break: break-word;
42 | }
43 |
44 | .is-invisible {
45 | opacity: 0;
46 | }
47 |
48 | .subscription-details {
49 | transition: opacity 1s;
50 | }
51 |
52 | @media (max-width: 600px) {
53 | header > h1 {
54 | font-size: 36px;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/completed/01-register-sw/sw.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, serviceworker, es6 */
21 |
22 | 'use strict';
23 |
--------------------------------------------------------------------------------
/completed/02-subscription-state/images/badge.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/02-subscription-state/images/badge.png
--------------------------------------------------------------------------------
/completed/02-subscription-state/images/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/02-subscription-state/images/icon.png
--------------------------------------------------------------------------------
/completed/02-subscription-state/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Push Codelab
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
Push Codelab
20 |
21 |
22 |
23 |
Welcome to the push messaging codelab. The button below needs to be
24 | fixed to support subscribing to push.
25 |
26 |
29 |
30 |
31 |
Once you've subscribed your user, you'd send their subscription to your
32 | server to store in a database so that when you want to send a message
33 | you can lookup the subscription and send a message to it.
34 |
To simplify things for this code lab copy the following details
35 | into the Push Companion
36 | Site and it'll send a push message for you, using the application
37 | server keys on the site - so make sure they match.
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/completed/02-subscription-state/scripts/main.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, es6 */
21 | /* eslint-disable no-unused-vars */
22 |
23 | 'use strict';
24 |
25 | const applicationServerPublicKey = 'BCW6JPG-T7Jx0bYKMhAbL6j3DL3VTTib7dwvBjQ' +
26 | 'C_496a12auzzKFnjgFjCsys_YtWkeMLhogfSlyM0CaIktx7o';
27 |
28 | const pushButton = document.querySelector('.js-push-btn');
29 |
30 | let isSubscribed = false;
31 | let swRegistration = null;
32 |
33 | function urlB64ToUint8Array(base64String) {
34 | const padding = '='.repeat((4 - base64String.length % 4) % 4);
35 | const base64 = (base64String + padding)
36 | .replace(/\-/g, '+')
37 | .replace(/_/g, '/');
38 |
39 | const rawData = window.atob(base64);
40 | const outputArray = new Uint8Array(rawData.length);
41 |
42 | for (let i = 0; i < rawData.length; ++i) {
43 | outputArray[i] = rawData.charCodeAt(i);
44 | }
45 | return outputArray;
46 | }
47 |
48 | function updateBtn() {
49 | if (isSubscribed) {
50 | pushButton.textContent = 'Disable Push Messaging';
51 | } else {
52 | pushButton.textContent = 'Enable Push Messaging';
53 | }
54 |
55 | pushButton.disabled = false;
56 | }
57 |
58 | function initializeUI() {
59 | // Set the initial subscription value
60 | swRegistration.pushManager.getSubscription()
61 | .then(function(subscription) {
62 | isSubscribed = !(subscription === null);
63 |
64 | if (isSubscribed) {
65 | console.log('User IS subscribed.');
66 | } else {
67 | console.log('User is NOT subscribed.');
68 | }
69 |
70 | updateBtn();
71 | });
72 | }
73 |
74 | if ('serviceWorker' in navigator && 'PushManager' in window) {
75 | console.log('Service Worker and Push is supported');
76 |
77 | navigator.serviceWorker.register('sw.js')
78 | .then(function(swReg) {
79 | console.log('Service Worker is registered', swReg);
80 |
81 | swRegistration = swReg;
82 | initializeUI();
83 | })
84 | .catch(function(error) {
85 | console.error('Service Worker Error', error);
86 | });
87 | } else {
88 | console.warn('Push messaging is not supported');
89 | pushButton.textContent = 'Push Not Supported';
90 | }
91 |
--------------------------------------------------------------------------------
/completed/02-subscription-state/styles/index.css:
--------------------------------------------------------------------------------
1 | html {
2 | height: 100%;
3 | }
4 |
5 | html, body {
6 | width: 100%;
7 | padding: 0;
8 | margin: 0;
9 | }
10 |
11 | body {
12 | min-height: auto;
13 | box-sizing: border-box;
14 | }
15 |
16 | header {
17 | padding: 115px 0 32px 0;
18 | background-color: #00bcd4;
19 | color: white;
20 | }
21 |
22 | main, header > h1 {
23 | padding: 0 16px;
24 | max-width: 760px;
25 | box-sizing: border-box;
26 | margin: 0 auto;
27 | }
28 |
29 | main {
30 | margin: 24px auto;
31 | box-sizing: border-box;
32 | }
33 |
34 | pre {
35 | white-space: pre-wrap;
36 | background-color: #EEEEEE;
37 | padding: 16px;
38 | }
39 |
40 | pre code {
41 | word-break: break-word;
42 | }
43 |
44 | .is-invisible {
45 | opacity: 0;
46 | }
47 |
48 | .subscription-details {
49 | transition: opacity 1s;
50 | }
51 |
52 | @media (max-width: 600px) {
53 | header > h1 {
54 | font-size: 36px;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/completed/02-subscription-state/sw.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, serviceworker, es6 */
21 |
22 | 'use strict';
23 |
--------------------------------------------------------------------------------
/completed/03-subscribe/images/badge.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/03-subscribe/images/badge.png
--------------------------------------------------------------------------------
/completed/03-subscribe/images/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/03-subscribe/images/icon.png
--------------------------------------------------------------------------------
/completed/03-subscribe/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Push Codelab
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
Push Codelab
20 |
21 |
22 |
23 |
Welcome to the push messaging codelab. The button below needs to be
24 | fixed to support subscribing to push.
25 |
26 |
29 |
30 |
31 |
Once you've subscribed your user, you'd send their subscription to your
32 | server to store in a database so that when you want to send a message
33 | you can lookup the subscription and send a message to it.
34 |
To simplify things for this code lab copy the following details
35 | into the Push Companion
36 | Site and it'll send a push message for you, using the application
37 | server keys on the site - so make sure they match.
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/completed/03-subscribe/scripts/main.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, es6 */
21 |
22 | 'use strict';
23 |
24 | const applicationServerPublicKey = 'BCW6JPG-T7Jx0bYKMhAbL6j3DL3VTTib7dwvBjQ' +
25 | 'C_496a12auzzKFnjgFjCsys_YtWkeMLhogfSlyM0CaIktx7o';
26 |
27 | const pushButton = document.querySelector('.js-push-btn');
28 |
29 | let isSubscribed = false;
30 | let swRegistration = null;
31 |
32 | function urlB64ToUint8Array(base64String) {
33 | const padding = '='.repeat((4 - base64String.length % 4) % 4);
34 | const base64 = (base64String + padding)
35 | .replace(/\-/g, '+')
36 | .replace(/_/g, '/');
37 |
38 | const rawData = window.atob(base64);
39 | const outputArray = new Uint8Array(rawData.length);
40 |
41 | for (let i = 0; i < rawData.length; ++i) {
42 | outputArray[i] = rawData.charCodeAt(i);
43 | }
44 | return outputArray;
45 | }
46 |
47 | function updateBtn() {
48 | if (isSubscribed) {
49 | pushButton.textContent = 'Disable Push Messaging';
50 | } else {
51 | pushButton.textContent = 'Enable Push Messaging';
52 | }
53 |
54 | pushButton.disabled = false;
55 | }
56 |
57 | function updateSubscriptionOnServer(subscription) {
58 | // TODO: Send subscription to application server
59 |
60 | const subscriptionJson = document.querySelector('.js-subscription-json');
61 | const subscriptionDetails =
62 | document.querySelector('.js-subscription-details');
63 |
64 | if (subscription) {
65 | subscriptionJson.textContent = JSON.stringify(subscription);
66 | subscriptionDetails.classList.remove('is-invisible');
67 | } else {
68 | subscriptionDetails.classList.add('is-invisible');
69 | }
70 | }
71 |
72 | function subscribeUser() {
73 | const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey);
74 | swRegistration.pushManager.subscribe({
75 | userVisibleOnly: true,
76 | applicationServerKey: applicationServerKey
77 | })
78 | .then(function(subscription) {
79 | console.log('User is subscribed');
80 |
81 | updateSubscriptionOnServer(subscription);
82 |
83 | isSubscribed = true;
84 |
85 | updateBtn();
86 | })
87 | .catch(function(err) {
88 | console.log('Failed to subscribe the user: ', err);
89 | updateBtn();
90 | });
91 | }
92 |
93 | function initializeUI() {
94 | pushButton.addEventListener('click', function() {
95 | pushButton.disabled = true;
96 | if (isSubscribed) {
97 | // TODO: Unsubscribe user
98 | } else {
99 | subscribeUser();
100 | }
101 | });
102 |
103 | // Set the initial subscription value
104 | swRegistration.pushManager.getSubscription()
105 | .then(function(subscription) {
106 | isSubscribed = !(subscription === null);
107 |
108 | updateSubscriptionOnServer(subscription);
109 |
110 | if (isSubscribed) {
111 | console.log('User IS subscribed.');
112 | } else {
113 | console.log('User is NOT subscribed.');
114 | }
115 |
116 | updateBtn();
117 | });
118 | }
119 |
120 | if ('serviceWorker' in navigator && 'PushManager' in window) {
121 | console.log('Service Worker and Push is supported');
122 |
123 | navigator.serviceWorker.register('sw.js')
124 | .then(function(swReg) {
125 | console.log('Service Worker is registered', swReg);
126 |
127 | swRegistration = swReg;
128 | initializeUI();
129 | })
130 | .catch(function(error) {
131 | console.error('Service Worker Error', error);
132 | });
133 | } else {
134 | console.warn('Push messaging is not supported');
135 | pushButton.textContent = 'Push Not Supported';
136 | }
137 |
--------------------------------------------------------------------------------
/completed/03-subscribe/styles/index.css:
--------------------------------------------------------------------------------
1 | html {
2 | height: 100%;
3 | }
4 |
5 | html, body {
6 | width: 100%;
7 | padding: 0;
8 | margin: 0;
9 | }
10 |
11 | body {
12 | min-height: auto;
13 | box-sizing: border-box;
14 | }
15 |
16 | header {
17 | padding: 115px 0 32px 0;
18 | background-color: #00bcd4;
19 | color: white;
20 | }
21 |
22 | main, header > h1 {
23 | padding: 0 16px;
24 | max-width: 760px;
25 | box-sizing: border-box;
26 | margin: 0 auto;
27 | }
28 |
29 | main {
30 | margin: 24px auto;
31 | box-sizing: border-box;
32 | }
33 |
34 | pre {
35 | white-space: pre-wrap;
36 | background-color: #EEEEEE;
37 | padding: 16px;
38 | }
39 |
40 | pre code {
41 | word-break: break-word;
42 | }
43 |
44 | .is-invisible {
45 | opacity: 0;
46 | }
47 |
48 | .subscription-details {
49 | transition: opacity 1s;
50 | }
51 |
52 | @media (max-width: 600px) {
53 | header > h1 {
54 | font-size: 36px;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/completed/03-subscribe/sw.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, serviceworker, es6 */
21 |
22 | 'use strict';
23 |
--------------------------------------------------------------------------------
/completed/04-permission-denied/images/badge.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/04-permission-denied/images/badge.png
--------------------------------------------------------------------------------
/completed/04-permission-denied/images/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/04-permission-denied/images/icon.png
--------------------------------------------------------------------------------
/completed/04-permission-denied/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Push Codelab
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
Push Codelab
20 |
21 |
22 |
23 |
Welcome to the push messaging codelab. The button below needs to be
24 | fixed to support subscribing to push.
25 |
26 |
29 |
30 |
31 |
Once you've subscribed your user, you'd send their subscription to your
32 | server to store in a database so that when you want to send a message
33 | you can lookup the subscription and send a message to it.
34 |
To simplify things for this code lab copy the following details
35 | into the Push Companion
36 | Site and it'll send a push message for you, using the application
37 | server keys on the site - so make sure they match.
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/completed/04-permission-denied/scripts/main.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, es6 */
21 |
22 | 'use strict';
23 |
24 | const applicationServerPublicKey = 'BCW6JPG-T7Jx0bYKMhAbL6j3DL3VTTib7dwvBjQ' +
25 | 'C_496a12auzzKFnjgFjCsys_YtWkeMLhogfSlyM0CaIktx7o';
26 |
27 | const pushButton = document.querySelector('.js-push-btn');
28 |
29 | let isSubscribed = false;
30 | let swRegistration = null;
31 |
32 | function urlB64ToUint8Array(base64String) {
33 | const padding = '='.repeat((4 - base64String.length % 4) % 4);
34 | const base64 = (base64String + padding)
35 | .replace(/\-/g, '+')
36 | .replace(/_/g, '/');
37 |
38 | const rawData = window.atob(base64);
39 | const outputArray = new Uint8Array(rawData.length);
40 |
41 | for (let i = 0; i < rawData.length; ++i) {
42 | outputArray[i] = rawData.charCodeAt(i);
43 | }
44 | return outputArray;
45 | }
46 |
47 | function updateBtn() {
48 | if (Notification.permission === 'denied') {
49 | pushButton.textContent = 'Push Messaging Blocked.';
50 | pushButton.disabled = true;
51 | updateSubscriptionOnServer(null);
52 | return;
53 | }
54 |
55 | if (isSubscribed) {
56 | pushButton.textContent = 'Disable Push Messaging';
57 | } else {
58 | pushButton.textContent = 'Enable Push Messaging';
59 | }
60 |
61 | pushButton.disabled = false;
62 | }
63 |
64 | function updateSubscriptionOnServer(subscription) {
65 | // TODO: Send subscription to application server
66 |
67 | const subscriptionJson = document.querySelector('.js-subscription-json');
68 | const subscriptionDetails =
69 | document.querySelector('.js-subscription-details');
70 |
71 | if (subscription) {
72 | subscriptionJson.textContent = JSON.stringify(subscription);
73 | subscriptionDetails.classList.remove('is-invisible');
74 | } else {
75 | subscriptionDetails.classList.add('is-invisible');
76 | }
77 | }
78 |
79 | function subscribeUser() {
80 | const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey);
81 | swRegistration.pushManager.subscribe({
82 | userVisibleOnly: true,
83 | applicationServerKey: applicationServerKey
84 | })
85 | .then(function(subscription) {
86 | console.log('User is subscribed.');
87 |
88 | updateSubscriptionOnServer(subscription);
89 |
90 | isSubscribed = true;
91 |
92 | updateBtn();
93 | })
94 | .catch(function(err) {
95 | console.log('Failed to subscribe the user: ', err);
96 | updateBtn();
97 | });
98 | }
99 |
100 | function initializeUI() {
101 | pushButton.addEventListener('click', function() {
102 | pushButton.disabled = true;
103 | if (isSubscribed) {
104 | // TODO: Unsubscribe user
105 | } else {
106 | subscribeUser();
107 | }
108 | });
109 |
110 | // Set the initial subscription value
111 | swRegistration.pushManager.getSubscription()
112 | .then(function(subscription) {
113 | isSubscribed = !(subscription === null);
114 |
115 | updateSubscriptionOnServer(subscription);
116 |
117 | if (isSubscribed) {
118 | console.log('User IS subscribed.');
119 | } else {
120 | console.log('User is NOT subscribed.');
121 | }
122 |
123 | updateBtn();
124 | });
125 | }
126 |
127 | if ('serviceWorker' in navigator && 'PushManager' in window) {
128 | console.log('Service Worker and Push is supported');
129 |
130 | navigator.serviceWorker.register('sw.js')
131 | .then(function(swReg) {
132 | console.log('Service Worker is registered', swReg);
133 |
134 | swRegistration = swReg;
135 | initializeUI();
136 | })
137 | .catch(function(error) {
138 | console.error('Service Worker Error', error);
139 | });
140 | } else {
141 | console.warn('Push messaging is not supported');
142 | pushButton.textContent = 'Push Not Supported';
143 | }
144 |
--------------------------------------------------------------------------------
/completed/04-permission-denied/styles/index.css:
--------------------------------------------------------------------------------
1 | html {
2 | height: 100%;
3 | }
4 |
5 | html, body {
6 | width: 100%;
7 | padding: 0;
8 | margin: 0;
9 | }
10 |
11 | body {
12 | min-height: auto;
13 | box-sizing: border-box;
14 | }
15 |
16 | header {
17 | padding: 115px 0 32px 0;
18 | background-color: #00bcd4;
19 | color: white;
20 | }
21 |
22 | main, header > h1 {
23 | padding: 0 16px;
24 | max-width: 760px;
25 | box-sizing: border-box;
26 | margin: 0 auto;
27 | }
28 |
29 | main {
30 | margin: 24px auto;
31 | box-sizing: border-box;
32 | }
33 |
34 | pre {
35 | white-space: pre-wrap;
36 | background-color: #EEEEEE;
37 | padding: 16px;
38 | }
39 |
40 | pre code {
41 | word-break: break-word;
42 | }
43 |
44 | .is-invisible {
45 | opacity: 0;
46 | }
47 |
48 | .subscription-details {
49 | transition: opacity 1s;
50 | }
51 |
52 | @media (max-width: 600px) {
53 | header > h1 {
54 | font-size: 36px;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/completed/04-permission-denied/sw.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, serviceworker, es6 */
21 |
22 | 'use strict';
23 |
--------------------------------------------------------------------------------
/completed/05-push-event/images/badge.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/05-push-event/images/badge.png
--------------------------------------------------------------------------------
/completed/05-push-event/images/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/05-push-event/images/icon.png
--------------------------------------------------------------------------------
/completed/05-push-event/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Push Codelab
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
Push Codelab
20 |
21 |
22 |
23 |
Welcome to the push messaging codelab. The button below needs to be
24 | fixed to support subscribing to push.
25 |
26 |
29 |
30 |
31 |
Once you've subscribed your user, you'd send their subscription to your
32 | server to store in a database so that when you want to send a message
33 | you can lookup the subscription and send a message to it.
34 |
To simplify things for this code lab copy the following details
35 | into the Push Companion
36 | Site and it'll send a push message for you, using the application
37 | server keys on the site - so make sure they match.
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/completed/05-push-event/scripts/main.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, es6 */
21 |
22 | 'use strict';
23 |
24 | const applicationServerPublicKey = 'BCW6JPG-T7Jx0bYKMhAbL6j3DL3VTTib7dwvBjQ' +
25 | 'C_496a12auzzKFnjgFjCsys_YtWkeMLhogfSlyM0CaIktx7o';
26 |
27 | const pushButton = document.querySelector('.js-push-btn');
28 |
29 | let isSubscribed = false;
30 | let swRegistration = null;
31 |
32 | function urlB64ToUint8Array(base64String) {
33 | const padding = '='.repeat((4 - base64String.length % 4) % 4);
34 | const base64 = (base64String + padding)
35 | .replace(/\-/g, '+')
36 | .replace(/_/g, '/');
37 |
38 | const rawData = window.atob(base64);
39 | const outputArray = new Uint8Array(rawData.length);
40 |
41 | for (let i = 0; i < rawData.length; ++i) {
42 | outputArray[i] = rawData.charCodeAt(i);
43 | }
44 | return outputArray;
45 | }
46 |
47 | function updateBtn() {
48 | if (Notification.permission === 'denied') {
49 | pushButton.textContent = 'Push Messaging Blocked.';
50 | pushButton.disabled = true;
51 | updateSubscriptionOnServer(null);
52 | return;
53 | }
54 |
55 | if (isSubscribed) {
56 | pushButton.textContent = 'Disable Push Messaging';
57 | } else {
58 | pushButton.textContent = 'Enable Push Messaging';
59 | }
60 |
61 | pushButton.disabled = false;
62 | }
63 |
64 | function updateSubscriptionOnServer(subscription) {
65 | // TODO: Send subscription to application server
66 |
67 | const subscriptionJson = document.querySelector('.js-subscription-json');
68 | const subscriptionDetails =
69 | document.querySelector('.js-subscription-details');
70 |
71 | if (subscription) {
72 | subscriptionJson.textContent = JSON.stringify(subscription);
73 | subscriptionDetails.classList.remove('is-invisible');
74 | } else {
75 | subscriptionDetails.classList.add('is-invisible');
76 | }
77 | }
78 |
79 | function subscribeUser() {
80 | const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey);
81 | swRegistration.pushManager.subscribe({
82 | userVisibleOnly: true,
83 | applicationServerKey: applicationServerKey
84 | })
85 | .then(function(subscription) {
86 | console.log('User is subscribed');
87 |
88 | updateSubscriptionOnServer(subscription);
89 |
90 | isSubscribed = true;
91 |
92 | updateBtn();
93 | })
94 | .catch(function(err) {
95 | console.log('Failed to subscribe the user: ', err);
96 | updateBtn();
97 | });
98 | }
99 |
100 | function initializeUI() {
101 | pushButton.addEventListener('click', function() {
102 | pushButton.disabled = true;
103 | if (isSubscribed) {
104 | // TODO: Unsubscribe user
105 | } else {
106 | subscribeUser();
107 | }
108 | });
109 |
110 | // Set the initial subscription value
111 | swRegistration.pushManager.getSubscription()
112 | .then(function(subscription) {
113 | isSubscribed = !(subscription === null);
114 |
115 | updateSubscriptionOnServer(subscription);
116 |
117 | if (isSubscribed) {
118 | console.log('User IS subscribed.');
119 | } else {
120 | console.log('User is NOT subscribed.');
121 | }
122 |
123 | updateBtn();
124 | });
125 | }
126 |
127 | if ('serviceWorker' in navigator && 'PushManager' in window) {
128 | console.log('Service Worker and Push is supported');
129 |
130 | navigator.serviceWorker.register('sw.js')
131 | .then(function(swReg) {
132 | console.log('Service Worker is registered', swReg);
133 |
134 | swRegistration = swReg;
135 | initializeUI();
136 | })
137 | .catch(function(error) {
138 | console.error('Service Worker Error', error);
139 | });
140 | } else {
141 | console.warn('Push messaging is not supported');
142 | pushButton.textContent = 'Push Not Supported';
143 | }
144 |
--------------------------------------------------------------------------------
/completed/05-push-event/styles/index.css:
--------------------------------------------------------------------------------
1 | html {
2 | height: 100%;
3 | }
4 |
5 | html, body {
6 | width: 100%;
7 | padding: 0;
8 | margin: 0;
9 | }
10 |
11 | body {
12 | min-height: auto;
13 | box-sizing: border-box;
14 | }
15 |
16 | header {
17 | padding: 115px 0 32px 0;
18 | background-color: #00bcd4;
19 | color: white;
20 | }
21 |
22 | main, header > h1 {
23 | padding: 0 16px;
24 | max-width: 760px;
25 | box-sizing: border-box;
26 | margin: 0 auto;
27 | }
28 |
29 | main {
30 | margin: 24px auto;
31 | box-sizing: border-box;
32 | }
33 |
34 | pre {
35 | white-space: pre-wrap;
36 | background-color: #EEEEEE;
37 | padding: 16px;
38 | }
39 |
40 | pre code {
41 | word-break: break-word;
42 | }
43 |
44 | .is-invisible {
45 | opacity: 0;
46 | }
47 |
48 | .subscription-details {
49 | transition: opacity 1s;
50 | }
51 |
52 | @media (max-width: 600px) {
53 | header > h1 {
54 | font-size: 36px;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/completed/05-push-event/sw.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, serviceworker, es6 */
21 |
22 | 'use strict';
23 |
24 | self.addEventListener('push', function(event) {
25 | console.log('[Service Worker] Push Received.');
26 | console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);
27 |
28 | const title = 'Push Codelab';
29 | const options = {
30 | body: 'Yay it works.',
31 | icon: 'images/icon.png',
32 | badge: 'images/badge.png'
33 | };
34 |
35 | event.waitUntil(self.registration.showNotification(title, options));
36 | });
37 |
--------------------------------------------------------------------------------
/completed/06-click/images/badge.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/06-click/images/badge.png
--------------------------------------------------------------------------------
/completed/06-click/images/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/06-click/images/icon.png
--------------------------------------------------------------------------------
/completed/06-click/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Push Codelab
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
Push Codelab
20 |
21 |
22 |
23 |
Welcome to the push messaging codelab. The button below needs to be
24 | fixed to support subscribing to push.
25 |
26 |
29 |
30 |
31 |
Once you've subscribed your user, you'd send their subscription to your
32 | server to store in a database so that when you want to send a message
33 | you can lookup the subscription and send a message to it.
34 |
To simplify things for this code lab copy the following details
35 | into the Push Companion
36 | Site and it'll send a push message for you, using the application
37 | server keys on the site - so make sure they match.
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/completed/06-click/scripts/main.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, es6 */
21 |
22 | 'use strict';
23 |
24 | const applicationServerPublicKey = 'BCW6JPG-T7Jx0bYKMhAbL6j3DL3VTTib7dwvBjQ' +
25 | 'C_496a12auzzKFnjgFjCsys_YtWkeMLhogfSlyM0CaIktx7o';
26 |
27 | const pushButton = document.querySelector('.js-push-btn');
28 |
29 | let isSubscribed = false;
30 | let swRegistration = null;
31 |
32 | function urlB64ToUint8Array(base64String) {
33 | const padding = '='.repeat((4 - base64String.length % 4) % 4);
34 | const base64 = (base64String + padding)
35 | .replace(/\-/g, '+')
36 | .replace(/_/g, '/');
37 |
38 | const rawData = window.atob(base64);
39 | const outputArray = new Uint8Array(rawData.length);
40 |
41 | for (let i = 0; i < rawData.length; ++i) {
42 | outputArray[i] = rawData.charCodeAt(i);
43 | }
44 | return outputArray;
45 | }
46 |
47 | function updateBtn() {
48 | if (Notification.permission === 'denied') {
49 | pushButton.textContent = 'Push Messaging Blocked.';
50 | pushButton.disabled = true;
51 | updateSubscriptionOnServer(null);
52 | return;
53 | }
54 |
55 | if (isSubscribed) {
56 | pushButton.textContent = 'Disable Push Messaging';
57 | } else {
58 | pushButton.textContent = 'Enable Push Messaging';
59 | }
60 |
61 | pushButton.disabled = false;
62 | }
63 |
64 | function updateSubscriptionOnServer(subscription) {
65 | // TODO: Send subscription to application server
66 |
67 | const subscriptionJson = document.querySelector('.js-subscription-json');
68 | const subscriptionDetails =
69 | document.querySelector('.js-subscription-details');
70 |
71 | if (subscription) {
72 | subscriptionJson.textContent = JSON.stringify(subscription);
73 | subscriptionDetails.classList.remove('is-invisible');
74 | } else {
75 | subscriptionDetails.classList.add('is-invisible');
76 | }
77 | }
78 |
79 | function subscribeUser() {
80 | const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey);
81 | swRegistration.pushManager.subscribe({
82 | userVisibleOnly: true,
83 | applicationServerKey: applicationServerKey
84 | })
85 | .then(function(subscription) {
86 | console.log('User is subscribed.');
87 |
88 | updateSubscriptionOnServer(subscription);
89 |
90 | isSubscribed = true;
91 |
92 | updateBtn();
93 | })
94 | .catch(function(err) {
95 | console.log('Failed to subscribe the user: ', err);
96 | updateBtn();
97 | });
98 | }
99 |
100 | function initializeUI() {
101 | pushButton.addEventListener('click', function() {
102 | pushButton.disabled = true;
103 | if (isSubscribed) {
104 | // TODO: Unsubscribe user
105 | } else {
106 | subscribeUser();
107 | }
108 | });
109 |
110 | // Set the initial subscription value
111 | swRegistration.pushManager.getSubscription()
112 | .then(function(subscription) {
113 | isSubscribed = !(subscription === null);
114 |
115 | updateSubscriptionOnServer(subscription);
116 |
117 | if (isSubscribed) {
118 | console.log('User IS subscribed.');
119 | } else {
120 | console.log('User is NOT subscribed.');
121 | }
122 |
123 | updateBtn();
124 | });
125 | }
126 |
127 | if ('serviceWorker' in navigator && 'PushManager' in window) {
128 | console.log('Service Worker and Push is supported');
129 |
130 | navigator.serviceWorker.register('sw.js')
131 | .then(function(swReg) {
132 | console.log('Service Worker is registered', swReg);
133 |
134 | swRegistration = swReg;
135 | initializeUI();
136 | })
137 | .catch(function(error) {
138 | console.error('Service Worker Error', error);
139 | });
140 | } else {
141 | console.warn('Push messaging is not supported');
142 | pushButton.textContent = 'Push Not Supported';
143 | }
144 |
--------------------------------------------------------------------------------
/completed/06-click/styles/index.css:
--------------------------------------------------------------------------------
1 | html {
2 | height: 100%;
3 | }
4 |
5 | html, body {
6 | width: 100%;
7 | padding: 0;
8 | margin: 0;
9 | }
10 |
11 | body {
12 | min-height: auto;
13 | box-sizing: border-box;
14 | }
15 |
16 | header {
17 | padding: 115px 0 32px 0;
18 | background-color: #00bcd4;
19 | color: white;
20 | }
21 |
22 | main, header > h1 {
23 | padding: 0 16px;
24 | max-width: 760px;
25 | box-sizing: border-box;
26 | margin: 0 auto;
27 | }
28 |
29 | main {
30 | margin: 24px auto;
31 | box-sizing: border-box;
32 | }
33 |
34 | pre {
35 | white-space: pre-wrap;
36 | background-color: #EEEEEE;
37 | padding: 16px;
38 | }
39 |
40 | pre code {
41 | word-break: break-word;
42 | }
43 |
44 | .is-invisible {
45 | opacity: 0;
46 | }
47 |
48 | .subscription-details {
49 | transition: opacity 1s;
50 | }
51 |
52 | @media (max-width: 600px) {
53 | header > h1 {
54 | font-size: 36px;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/completed/06-click/sw.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, serviceworker, es6 */
21 |
22 | 'use strict';
23 |
24 | self.addEventListener('push', function(event) {
25 | console.log('[Service Worker] Push Received.');
26 | console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);
27 |
28 | const title = 'Push Codelab';
29 | const options = {
30 | body: 'Yay it works.',
31 | icon: 'images/icon.png',
32 | badge: 'images/badge.png'
33 | };
34 |
35 | event.waitUntil(self.registration.showNotification(title, options));
36 | });
37 |
38 | self.addEventListener('notificationclick', function(event) {
39 | console.log('[Service Worker] Notification click Received.');
40 |
41 | event.notification.close();
42 |
43 | event.waitUntil(
44 | clients.openWindow('https://developers.google.com/web/')
45 | );
46 | });
47 |
--------------------------------------------------------------------------------
/completed/07-unsubscribe/images/badge.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/07-unsubscribe/images/badge.png
--------------------------------------------------------------------------------
/completed/07-unsubscribe/images/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/07-unsubscribe/images/icon.png
--------------------------------------------------------------------------------
/completed/07-unsubscribe/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Push Codelab
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
Push Codelab
20 |
21 |
22 |
23 |
Welcome to the push messaging codelab. The button below needs to be
24 | fixed to support subscribing to push.
25 |
26 |
29 |
30 |
31 |
Once you've subscribed your user, you'd send their subscription to your
32 | server to store in a database so that when you want to send a message
33 | you can lookup the subscription and send a message to it.
34 |
To simplify things for this code lab copy the following details
35 | into the Push Companion
36 | Site and it'll send a push message for you, using the application
37 | server keys on the site - so make sure they match.
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/completed/07-unsubscribe/scripts/main.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, es6 */
21 |
22 | 'use strict';
23 |
24 | const applicationServerPublicKey = 'BCW6JPG-T7Jx0bYKMhAbL6j3DL3VTTib7dwvBjQ' +
25 | 'C_496a12auzzKFnjgFjCsys_YtWkeMLhogfSlyM0CaIktx7o';
26 |
27 | const pushButton = document.querySelector('.js-push-btn');
28 |
29 | let isSubscribed = false;
30 | let swRegistration = null;
31 |
32 | function urlB64ToUint8Array(base64String) {
33 | const padding = '='.repeat((4 - base64String.length % 4) % 4);
34 | const base64 = (base64String + padding)
35 | .replace(/\-/g, '+')
36 | .replace(/_/g, '/');
37 |
38 | const rawData = window.atob(base64);
39 | const outputArray = new Uint8Array(rawData.length);
40 |
41 | for (let i = 0; i < rawData.length; ++i) {
42 | outputArray[i] = rawData.charCodeAt(i);
43 | }
44 | return outputArray;
45 | }
46 |
47 | function updateBtn() {
48 | if (Notification.permission === 'denied') {
49 | pushButton.textContent = 'Push Messaging Blocked.';
50 | pushButton.disabled = true;
51 | updateSubscriptionOnServer(null);
52 | return;
53 | }
54 |
55 | if (isSubscribed) {
56 | pushButton.textContent = 'Disable Push Messaging';
57 | } else {
58 | pushButton.textContent = 'Enable Push Messaging';
59 | }
60 |
61 | pushButton.disabled = false;
62 | }
63 |
64 | function updateSubscriptionOnServer(subscription) {
65 | // TODO: Send subscription to application server
66 |
67 | const subscriptionJson = document.querySelector('.js-subscription-json');
68 | const subscriptionDetails =
69 | document.querySelector('.js-subscription-details');
70 |
71 | if (subscription) {
72 | subscriptionJson.textContent = JSON.stringify(subscription);
73 | subscriptionDetails.classList.remove('is-invisible');
74 | } else {
75 | subscriptionDetails.classList.add('is-invisible');
76 | }
77 | }
78 |
79 | function subscribeUser() {
80 | const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey);
81 | swRegistration.pushManager.subscribe({
82 | userVisibleOnly: true,
83 | applicationServerKey: applicationServerKey
84 | })
85 | .then(function(subscription) {
86 | console.log('User is subscribed.');
87 |
88 | updateSubscriptionOnServer(subscription);
89 |
90 | isSubscribed = true;
91 |
92 | updateBtn();
93 | })
94 | .catch(function(err) {
95 | console.log('Failed to subscribe the user: ', err);
96 | updateBtn();
97 | });
98 | }
99 |
100 | function unsubscribeUser() {
101 | swRegistration.pushManager.getSubscription()
102 | .then(function(subscription) {
103 | if (subscription) {
104 | return subscription.unsubscribe();
105 | }
106 | })
107 | .catch(function(error) {
108 | console.log('Error unsubscribing', error);
109 | })
110 | .then(function() {
111 | updateSubscriptionOnServer(null);
112 |
113 | console.log('User is unsubscribed.');
114 | isSubscribed = false;
115 |
116 | updateBtn();
117 | });
118 | }
119 |
120 | function initializeUI() {
121 | pushButton.addEventListener('click', function() {
122 | pushButton.disabled = true;
123 | if (isSubscribed) {
124 | unsubscribeUser();
125 | } else {
126 | subscribeUser();
127 | }
128 | });
129 |
130 | // Set the initial subscription value
131 | swRegistration.pushManager.getSubscription()
132 | .then(function(subscription) {
133 | isSubscribed = !(subscription === null);
134 |
135 | updateSubscriptionOnServer(subscription);
136 |
137 | if (isSubscribed) {
138 | console.log('User IS subscribed.');
139 | } else {
140 | console.log('User is NOT subscribed.');
141 | }
142 |
143 | updateBtn();
144 | });
145 | }
146 |
147 | if ('serviceWorker' in navigator && 'PushManager' in window) {
148 | console.log('Service Worker and Push is supported');
149 |
150 | navigator.serviceWorker.register('sw.js')
151 | .then(function(swReg) {
152 | console.log('Service Worker is registered', swReg);
153 |
154 | swRegistration = swReg;
155 | initializeUI();
156 | })
157 | .catch(function(error) {
158 | console.error('Service Worker Error', error);
159 | });
160 | } else {
161 | console.warn('Push messaging is not supported');
162 | pushButton.textContent = 'Push Not Supported';
163 | }
164 |
--------------------------------------------------------------------------------
/completed/07-unsubscribe/styles/index.css:
--------------------------------------------------------------------------------
1 | html {
2 | height: 100%;
3 | }
4 |
5 | html, body {
6 | width: 100%;
7 | padding: 0;
8 | margin: 0;
9 | }
10 |
11 | body {
12 | min-height: auto;
13 | box-sizing: border-box;
14 | }
15 |
16 | header {
17 | padding: 115px 0 32px 0;
18 | background-color: #00bcd4;
19 | color: white;
20 | }
21 |
22 | main, header > h1 {
23 | padding: 0 16px;
24 | max-width: 760px;
25 | box-sizing: border-box;
26 | margin: 0 auto;
27 | }
28 |
29 | main {
30 | margin: 24px auto;
31 | box-sizing: border-box;
32 | }
33 |
34 | pre {
35 | white-space: pre-wrap;
36 | background-color: #EEEEEE;
37 | padding: 16px;
38 | }
39 |
40 | pre code {
41 | word-break: break-word;
42 | }
43 |
44 | .is-invisible {
45 | opacity: 0;
46 | }
47 |
48 | .subscription-details {
49 | transition: opacity 1s;
50 | }
51 |
52 | @media (max-width: 600px) {
53 | header > h1 {
54 | font-size: 36px;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/completed/07-unsubscribe/sw.js:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Push Notifications codelab
4 | * Copyright 2015 Google Inc. All rights reserved.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | *
18 | */
19 |
20 | /* eslint-env browser, serviceworker, es6 */
21 |
22 | 'use strict';
23 |
24 | self.addEventListener('push', function(event) {
25 | console.log('[Service Worker] Push Received.');
26 | console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);
27 |
28 | const title = 'Push Codelab';
29 | const options = {
30 | body: 'Yay it works.',
31 | icon: 'images/icon.png',
32 | badge: 'images/badge.png'
33 | };
34 |
35 | event.waitUntil(self.registration.showNotification(title, options));
36 | });
37 |
38 | self.addEventListener('notificationclick', function(event) {
39 | console.log('[Service Worker] Notification click Received.');
40 |
41 | event.notification.close();
42 |
43 | event.waitUntil(
44 | clients.openWindow('https://developers.google.com/web/')
45 | );
46 | });
47 |
--------------------------------------------------------------------------------
/completed/08-push-subscription-change/images/badge.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/08-push-subscription-change/images/badge.png
--------------------------------------------------------------------------------
/completed/08-push-subscription-change/images/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GoogleChromeLabs/web-push-codelab/469a70b1eb195eeb27f5901ab58bd8452f015d9a/completed/08-push-subscription-change/images/icon.png
--------------------------------------------------------------------------------
/completed/08-push-subscription-change/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Push Codelab
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
Push Codelab
20 |
21 |
22 |
23 |
Welcome to the push messaging codelab. The button below needs to be
24 | fixed to support subscribing to push.
25 |
26 |
29 |
30 |
31 |
Once you've subscribed your user, you'd send their subscription to your
32 | server to store in a database so that when you want to send a message
33 | you can lookup the subscription and send a message to it.
34 |
To simplify things for this code lab copy the following details
35 | into the Push Companion
36 | Site and it'll send a push message for you, using the application
37 | server keys on the site - so make sure they match.