-
Notifications
You must be signed in to change notification settings - Fork 49
/
notifications.bs
1247 lines (931 loc) · 49.2 KB
/
notifications.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class=metadata>
Group: WHATWG
H1: Notifications API
Shortname: notifications
Text Macro: TWITTER notifyapi
Text Macro: LATESTRD 2023-07
Abstract: This standard defines an API to display notifications to the end user, typically outside the top-level browsing context's viewport. It is designed to be compatible with existing notification systems, while remaining platform-independent.
Translation: ja https://triple-underscore.github.io/notifications-ja.html
Translation: zh-Hans https://htmlspecs.com/notifications/
</pre>
<pre class=anchors>
urlPrefix: https://w3c.github.io/vibration/
urlPrefix: #dfn-; type: dfn
text: perform vibration
text: validate and normalize
urlPrefix: #idl-def-; type: interface
text: VibratePattern; url: vibratepattern
urlPrefix: https://tc39.github.io/ecma262/
urlPrefix: #sec-object.; type: dfn
text: freeze
url: #sec-list-and-record-specification-type; type: dfn; text: Record
</pre>
<h2 id=terminology>Terminology</h2>
<p>This specification depends on the Infra Standard. [[!INFRA]]
<p>Some terms used in this specification are defined in the DOM, Fetch, High Resolution Time, HTML,
IDL, Service Workers, URL, and Vibration API Standards.
[[!DOM]]
[[!FETCH]]
[[!HR-TIME]]
[[!HTML]]
[[!WEBIDL]]
[[!SERVICE-WORKERS]]
[[!URL]]
[[!VIBRATION]]
<h2 id=notifications>Notifications</h2>
<p>A <dfn id=concept-notification>notification</dfn> is an abstract
representation of something that happened, such as the delivery of a message.
<p>A <a for=/>notification</a> has an associated
<dfn export for=notification id=service-worker-registration>service worker registration</dfn> (null
or a <a for=/>service worker registration</a>). It is initially null.
<p>A <a for=/>notification</a> has an associated <dfn for=notification id=concept-title>title</dfn> (a string).
<p>A <a for=/>notification</a> has an associated
<dfn for=notification id=concept-direction>direction</dfn> ("<code>auto</code>", "<code>ltr</code>",
or "<code>rtl</code>").
<p>A <a for=/>notification</a> has an associated
<dfn for=notification id=concept-language>language</dfn> (a string).
<p>A <a for=/>notification</a> has an associated <dfn for=notification id=body>body</dfn> (a
string).
<p>A <a for=/>notification</a> has an associated <dfn for=notification id=tag>tag</dfn> (a string).
<p>A <a for=/>notification</a> has an associated <dfn for=notification id=data>data</dfn> (a
<a for=/>Record</a>).
<p>A <a for=/>notification</a> has an associated <dfn for=notification id=timestamp>timestamp</dfn>
(an {{EpochTimeStamp}}).
<p class=note>Timestamps can be used to indicate the time at which a notification
is actual. For example, this could be in the past when a notification is used for
a message that couldn't immediately be delivered because the device was offline,
or in the future for a meeting that is about to start.
<p>A <a for=/>notification</a> has an associated
<dfn for=notification id=concept-origin>origin</dfn> (an <a for=/>origin</a>).
<p>A <a for=/>notification</a> has an associated
<dfn for=notification id=renotify-preference-flag>renotify preference</dfn> (a boolean). It is
initially false. When true, indicates that the end user should be alerted after the
<a for=/>notification show steps</a> have run with a new notification that has the same
<a for=notification>tag</a> as an existing notification.
<p>A <a for=/>notification</a> has an associated
<dfn for=notification id=silent-preference-flag>silent preference</dfn> (null or a boolean). It is
initially null. When true, indicates that no sounds or vibrations should be made. When null,
indicates that producing sounds or vibrations should be left to platform conventions.
<p>A <a for=/>notification</a> has an associated
<dfn for=notification id=require-interaction-preference-flag>require interaction preference</dfn>
(a boolean). It is initially false. When true, indicates that on devices with a sufficiently large
screen, the notification should remain readily available until the end user activates or dismisses
the notification.
<p>A <a for=/>notification</a> <em>can</em> have these associated graphics: an
<dfn for=notification id=image-url>image URL</dfn>,
<dfn for=notification id=icon-url>icon URL</dfn>, and
<dfn for=notification id=badge-url>badge URL</dfn>; and their corresponding
<a for=notification>image resource</a>, <a for=notification>icon resource</a>, and
<a for=notification>badge resource</a>.
<p>An <dfn for=notification id=image-resource>image resource</dfn> is a picture shown as part of the
content of the <a for=/>notification</a>, and should be displayed with higher visual priority than the
<a for=notification>icon resource</a> and <a for=notification>badge resource</a>, though it may be
displayed in fewer circumstances.
<p>An <dfn for=notification id=icon-resource>icon resource</dfn> is an image that reinforces the
<a for=/>notification</a> (such as an icon, or a photo of the sender).
<p>A <dfn for=notification id=badge-resource>badge resource</dfn> is an icon representing the web
application, or the category of the <a for=/>notification</a> if the web application sends a wide variety
of <a>notifications</a>. It <em>may</em> be used to represent the <a for=/>notification</a> when there is
not enough space to display the <a for=/>notification</a> itself. It <em>may</em> also be displayed inside
the <a for=/>notification</a>, but then it should have less visual priority than the
<a for=notification>image resource</a> and <a for=notification>icon resource</a>.
<p>A <a for=/>notification</a> has an associated
<dfn for=notification id=vibration-pattern>vibration pattern</dfn> (a <a for=/>list</a>). It is
initially « ».
<p class=note>Developers are encouraged to not convey information through an
<a for=notification lt="image resource">image</a>, <a for=notification lt="icon resource">icon</a>,
<a for=notification lt="badge resource">badge</a>, or <a for=notification>vibration pattern</a> that
is not otherwise accessible to the end user, especially since notification platforms that do not
support these features might ignore them.
<p>A <a for=/>notification</a> has associated <dfn export for=notification id=actions>actions</dfn>
(a <a for=/>list</a> of zero or more <a for=/>notification actions</a>). A
<dfn export id=action>notification action</dfn> represents a choice for an end user. Each
<a for=/>notification action</a> has an associated:
<dl>
<dt><dfn for="notification action" id=action-name>name</dfn>
<dd>A string.
<dt><dfn for="notification action" id=action-title>title</dfn>
<dd>A string.
<dt><dfn for="notification action" id=action-icon-url>icon URL</dfn>
<dd>Null or a <a for=/>URL</a>. It is initially null.
<dt><dfn for="notification action" id=action-icon-resource>icon resource</dfn>
<dd>Null or a resource. It is initially null.
</dl>
<p>Users may activate actions, as alternatives to activating the notification itself. The
<dfn>maximum number of actions</dfn> supported is an <a>implementation-defined</a> integer of zero
or more, within the constraints of the notification platform.
<p class=note>Since display of actions is platform-dependent, developers are encouraged to make sure
that any action an end user can invoke from a notification is also available within the web
application.
<p class="note no-backref">Some platforms might modify an
<a for="notification action">icon resource</a> to better match the platform's visual style before
displaying it to the end user, for example by rounding the corners or painting it in a specific
color. Developers are encouraged to use an icon that handles such cases gracefully and does not lose
important information through, e.g., loss of color or clipped corners.
<p>A <dfn>non-persistent notification</dfn> is a <a for=/>notification</a> whose
<a for=notification>service worker registration</a> is null.
<p tracking-vector>A <dfn>persistent notification</dfn> is a <a for=/>notification</a> whose
<a for=notification>service worker registration</a> is non-null.
<hr>
<div algorithm>
<p>To <dfn>create a notification with a settings object</dfn>, given a string <var>title</var>,
{{NotificationOptions}} <a for=/>dictionary</a> <var>options</var>, and
<a for=/>environment settings object</a> <var>settings</var>:
<ol>
<li><p>Let <var>origin</var> be <var>settings</var>'s
<a for="environment settings object">origin</a>.
<li><p>Let <var>baseURL</var> be <var>settings</var>'s
<a for="environment settings object">API base URL</a>.
<li><p>Let <var>fallbackTimestamp</var> be the number of milliseconds from the <a>Unix epoch</a> to
<var>settings</var>'s <a for="environment settings object">current wall time</a>, rounded to the
nearest integer.
<li><p>Return the result of <a>creating a notification</a> given <var>title</var>,
<var>options</var>, <var>origin</var>, <var>baseURL</var>, and <var>fallbackTimestamp</var>.
</ol>
</div>
<div algorithm>
<p>To <dfn export>create a notification</dfn>, given a string <var>title</var>,
{{NotificationOptions}} <a for=/>dictionary</a> <var>options</var>, an <a for=/>origin</a>
<var>origin</var>, a <a for=/>URL</a> <var>baseURL</var>, and an {{EpochTimeStamp}}
<var>fallbackTimestamp</var>:
<ol>
<li><p>Let <var>notification</var> be a new <a for=/>notification</a>.
<li><p>If <var>options</var>["{{NotificationOptions/silent}}"] is true and
<var>options</var>["{{NotificationOptions/vibrate}}"] <a for=map>exists</a>, then <a>throw</a> a
{{TypeError}}.
<li><p>If <var>options</var>["{{NotificationOptions/renotify}}"] is true and
<var>options</var>["{{NotificationOptions/tag}}"] is the empty string, then <a>throw</a> a
{{TypeError}}.
<li><p>Set <var>notification</var>'s <a for=notification>data</a> to
<a abstract-op>StructuredSerializeForStorage</a>(<var>options</var>["{{NotificationOptions/data}}"]).
<li><p>Set <var>notification</var>'s <a for=notification>title</a> to <var>title</var>.
<li><p>Set <var>notification</var>'s <a for=notification>direction</a> to
<var>options</var>["{{NotificationOptions/dir}}"].
<li><p>Set <var>notification</var>'s <a for=notification>language</a> to
<var>options</var>["{{NotificationOptions/lang}}"].
<li><p>Set <var>notification</var>'s <a for=notification>origin</a> to <var>origin</var>.
<li><p>Set <var>notification</var>'s <a for=notification>body</a> to
<var>options</var>["{{NotificationOptions/body}}"].
<li><p>Set <var>notification</var>'s <a for=notification>tag</a> to
<var>options</var>["{{NotificationOptions/tag}}"].
<li><p>If <var>options</var>["{{NotificationOptions/image}}"] <a for=map>exists</a>, then
<a lt="URL parser">parse</a> it using <var>baseURL</var>, and if that does not return failure, set
<var>notification</var>'s <a>image URL</a> to the return value. (Otherwise
<var>notification</var>'s <a>image URL</a> is not set.)
<li><p>If <var>options</var>["{{NotificationOptions/icon}}"] <a for=map>exists</a>, then
<a lt="URL parser">parse</a> it using <var>baseURL</var>, and if that does not return failure, set
<var>notification</var>'s <a for=notification>icon URL</a> to the return value. (Otherwise
<var>notification</var>'s <a for=notification>icon URL</a> is not set.)
<li><p>If <var>options</var>["{{NotificationOptions/badge}}"] <a for=map>exists</a>, then
<a lt="URL parser">parse</a> it using <var>baseURL</var>, and if that does not return failure, set
<var>notification</var>'s <a for=notification>badge URL</a> to the return value. (Otherwise
<var>notification</var>'s <a for=notification>badge URL</a> is not set.)
<li><p>If <var>options</var>["{{NotificationOptions/vibrate}}"] <a for=map>exists</a>, then
<a>validate and normalize</a> it and set <var>notification</var>'s
<a for=notification>vibration pattern</a> to the return value.
<li><p>If <var>options</var>["{{NotificationOptions/timestamp}}"] <a for=map>exists</a>, then set
<var>notification</var>'s <a for=notification>timestamp</a> to the value. Otherwise, set
<var>notification</var>'s <a for=notification>timestamp</a> to <var>fallbackTimestamp</var>.
<li><p>Set <var>notification</var>'s <a for=notification>renotify preference</a> to
<var>options</var>["{{NotificationOptions/renotify}}"].
<li><p>Set <var>notification</var>'s <a for=notification>silent preference</a> to
<var>options</var>["{{NotificationOptions/silent}}"].
<li><p>Set <var>notification</var>'s <a for=notification>require interaction preference</a> to
<var>options</var>["{{NotificationOptions/requireInteraction}}"].
<li><p>Set <var>notification</var>'s <a for=notification>actions</a> to « ».
<li>
<p>For each <var>entry</var> in <var>options</var>["{{NotificationOptions/actions}}"], up to the
<a>maximum number of actions</a> supported (skip any excess entries):
<ol>
<li><p>Let <var>action</var> be a new <a for=/>notification action</a>.
<li><p>Set <var>action</var>'s <a for="notification action">name</a> to
<var>entry</var>["{{NotificationAction/action}}"].
<li><p>Set <var>action</var>'s <a for="notification action">title</a> to
<var>entry</var>["{{NotificationAction/title}}"].
<li><p>If <var>entry</var>["{{NotificationAction/icon}}"] <a for=map>exists</a>, then
<a lt="URL parser">parse</a> it using <var>baseURL</var>, and if that does not return failure,
set <var>action</var>'s <a for="notification action">icon URL</a> to the return value. (Otherwise
<var>action</var>'s <a for="notification action">icon URL</a> remains null.)
<li><p>Append <var>action</var> to <var>notification</var>'s <a for=notification>actions</a>.
</ol>
<li><p>Return <var>notification</var>.
</ol>
</div>
<h3 id=lifetime-and-ui-integrations>Lifetime and UI integration</h3>
<p>The user agent must keep a <dfn>list of notifications</dfn>, which is a <a for=/>list</a> of zero
or more <a>notifications</a>.
<p>User agents should run the <a>close steps</a> for a
<a>non-persistent notification</a> a couple of seconds after they have been
created.
<p>User agents should not display <a>non-persistent notification</a> in a
platform's "notification center" (if available).
<p>User agents should persist <a>persistent notifications</a> until they are
removed from the <a>list of notifications</a>.
<p class=example id=example-38bbd8ee>A <a>persistent notification</a> could have the
{{Notification/close()}} method invoked of one of its {{Notification}} objects.
<p>User agents should display <a>persistent notifications</a> in a platform's
"notification center" (if available).
<h3 id="permissions-integration">Permissions integration</h3>
<p>The Notifications API is a <a>powerful feature</a> which is identified by the
<a for="powerful feature">name</a> "<dfn export permission><code>notifications</code></dfn>".
[[!Permissions]]
<div algorithm>
<p>To <dfn>get the notifications permission state</dfn>, run these steps:
<ol>
<li><p>Let <var>permissionState</var> be the result of <a>getting the current permission state</a>
with "<a permission><code>notifications</code></a>".
<li><p>If <var>permissionState</var> is "<code>prompt</code>", then return "<code>default</code>".
<li><p>Return <var>permissionState</var>.
</ol>
</div>
<h3 id=direction>Direction</h3>
<p>This section is written in terms equivalent to those used in the Rendering
section of HTML. [[!HTML]]
<!-- keep this in sync with
https://html.spec.whatwg.org/multipage/rendering.html#text-rendered-in-native-user-interfaces -->
<p>User agents are expected to honor the Unicode semantics of the text of a
<a for=/>notification</a>'s <a for=notification>title</a>, <a for=notification>body</a>, and the
<a for="notification action">title</a> of each of its <a for=notification>actions</a>. Each is
expected to be treated as an independent set of one or more bidirectional algorithm
paragraphs when displayed, as defined by the bidirectional algorithm's rules P1,
P2, and P3, including, for instance, supporting the paragraph-breaking behavior
of U+000A LINE FEED (LF) characters. For each paragraph of the
<a for=notification>title</a>, <a for=notification>body</a> and the
<a for="notification action">title</a> of each of the <a for=notification>actions</a>, the
<a for=/>notification</a>'s
<a for=notification>direction</a> provides the higher-level override of
rules P2 and P3 if it has a value other than "<code>auto</code>". [[!BIDI]]
<p>The <a for=/>notification</a>'s <a for=notification>direction</a> also determines the relative
order in which the <a for=/>notification</a>'s <a for=notification>actions</a> should be displayed
to the end user, if the notification platform displays them side by side.
<h3 id=language>Language</h3>
<!-- keep this in sync with
https://html.spec.whatwg.org/multipage/dom.html#attr-lang -->
<p>The <a for=/>notification</a>'s <a for=notification>language</a> specifies the primary language
for the <a for=/>notification</a>'s <a for=notification>title</a>, <a for=notification>body</a> and
the <a for=notification>title</a> of each of its <a for=notification>actions</a>. Its value is a
string. The empty string indicates that the primary language is unknown. Any other string must be
interpreted as a language tag. Validity or well-formedness are not enforced. [[!BCP47]]
<p class=note>Developers are encouraged to only use valid language tags.
<h3 id=resources>Resources</h3>
<div algorithm>
<p>The <dfn>fetch steps</dfn> for a given <a for=/>notification</a> <var>notification</var> are:
<ol>
<!-- XXX image fetching is underspecified: https://github.com/whatwg/html/issues/4474 -->
<li>
<p>If the notification platform supports images, <a for=/>fetch</a>
<var>notification</var>'s <a>image URL</a>, if <a>image URL</a> is set.
<p class=note>The intent is to fetch this resource similar to an
<a href="https://html.spec.whatwg.org/multipage/images.html#update-the-image-data"><code><img></code></a>,
but this <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=24055">needs abstracting</a>.
<p>Then, <a>in parallel</a>:
<ol>
<li><p>Wait for the <a for=/>response</a>.
<li><p>If the <a for=/>response</a>'s <a>internal response</a>'s <a for=response>type</a> is
"<code>default</code>", then attempt to decode the resource as image.
<li><p>If the image format is supported, set <var>notification</var>'s
<a for=notification>image resource</a> to the decoded resource. (Otherwise
<var>notification</var> has no <a for=notification>image resource</a>.)
</ol>
<li>
<p>If the notification platform supports icons, <a for=/>fetch</a>
<var>notification</var>'s <a for=notification>icon URL</a>, if <a for=notification>icon URL</a>
is set.
<p class=note>The intent is to fetch this resource similar to an
<a href="https://html.spec.whatwg.org/multipage/images.html#update-the-image-data"><code><img></code></a>,
but this <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=24055">needs abstracting</a>.
<p>Then, <a>in parallel</a>:
<ol>
<li><p>Wait for the <a for=/>response</a>.
<li><p>If the <a for=/>response</a>'s <a>internal response</a>'s <a for=response>type</a> is
"<code>default</code>", then attempt to decode the resource as image.
<li><p>If the image format is supported, set <var>notification</var>'s
<a for=notification>icon resource</a> to the decoded resource. (Otherwise
<var>notification</var> has no <a for=notification>icon resource</a>.)
</ol>
</li>
<li>
<p>If the notification platform supports badges, <a for=/>fetch</a> <var>notification</var>'s
<a for=notification>badge URL</a>, if <a for=notification>badge URL</a> is set.
<p class=note>The intent is to fetch this resource similar to an
<a href="https://html.spec.whatwg.org/multipage/images.html#update-the-image-data"><code><img></code></a>,
but this <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=24055">needs abstracting</a>.
<p>Then, <a>in parallel</a>:
<ol>
<li><p>Wait for the <a for=/>response</a>.
<li><p>If the <a for=/>response</a>'s <a>internal response</a>'s <a for=response>type</a> is
"<code>default</code>", then attempt to decode the resource as image.
<li><p>If the image format is supported, set <var>notification</var>'s
<a for=notification>badge resource</a> to the decoded resource. (Otherwise
<var>notification</var> has no <a for=notification>badge resource</a>.)
</ol>
</li>
<li>
<p>If the notification platform supports actions and action icons, then for each <var>action</var>
in <var>notification</var>'s <a for=notification>actions</a>: if
<a for="notification action">icon URL</a> is non-null, <a for=/>fetch</a> <var>action</var>'s
<a for="notification action">icon URL</a>.
<p class=note>The intent is to fetch this resource similar to an
<a href="https://html.spec.whatwg.org/multipage/images.html#update-the-image-data"><code><img></code></a>,
but this <a href="https://github.com/whatwg/html/issues/4474">needs abstracting</a>.
<p>Then, <a>in parallel</a>:
<ol>
<li><p>Wait for the <a for=/>response</a>.
<li><p>If the <a for=/>response</a>'s <a>internal response</a>'s <a for=response>type</a> is
"<code>default</code>", then attempt to decode the resource as image.
<li><p>If the image format is supported, then set <var>action</var>'s
<a for="notification action">icon resource</a> to the decoded resource. (Otherwise
<var>action</var>'s <a for="notification action">icon resource</a> remains null.)
</ol>
</li>
</ol>
</div>
<h3 id=showing-a-notification>Showing a notification</h3>
<div algorithm>
<p>The <dfn export>notification show steps</dfn> for a given <a for=/>notification</a>
<var>notification</var> are:
<!-- These steps are invoked from "in parallel" steps -->
<ol>
<li><p>Run the <a>fetch steps</a> for <var>notification</var>.
<li><p>Wait for any <a for=/ lt=fetch>fetches</a> to complete and <var>notification</var>'s
<a for=notification>image resource</a>, <a for=notification>icon resource</a>, and
<a for=notification>badge resource</a> to be set (if any), as well as the
<a for="notification action">icon resources</a> for the <var>notification</var>'s
<a for=notification>actions</a> (if any).
<li><p>Let <var>shown</var> be false.
<li><p>Let <var>oldNotification</var> be the <a for=/>notification</a> in the
<a>list of notifications</a> whose <a for=notification>tag</a> is not the empty string and is
<var>notification</var>'s <a for=notification>tag</a>, and whose <a for=notification>origin</a> is
<a>same origin</a> with <var>notification</var>'s <a for=notification>origin</a>, if any, and null
otherwise.
<li>
<p>If <var>oldNotification</var> is non-null, then:
<ol>
<li><p><a>Handle close events</a> with <var>oldNotification</var>.
<li>
<p>If the notification platform supports replacement, then:
<ol>
<li><p><a for=list>Replace</a> <var>oldNotification</var> with <var>notification</var>, in the
<a>list of notifications</a>.
<li><p>Set <var>shown</var> to true.
</ol>
<p class="note no-backref">Notification platforms are strongly encouraged to support native
replacement as it leads to a better user experience.
<li><p>Otherwise, <a for=list>remove</a> <var>oldNotification</var> from the
<a>list of notifications</a>.
</ol>
<li>
<p>If <var>shown</var> is false, then:
<ol>
<li><p><a for=list>Append</a> <var>notification</var> to the <a>list of notifications</a>.
<li><p>Display <var>notification</var> on the device (e.g., by calling the appropriate
notification platform API).
</ol>
<li><p>If <var>shown</var> is false or <var>oldNotification</var> is non-null, and
<var>notification</var>'s <a for=notification>renotify preference</a> is true, then run the
<a>alert steps</a> for <var>notification</var>.
<li><p>If <var>notification</var> is a <a>non-persistent notification</a>, then <a>queue a task</a>
to <a>fire an event</a> named <code>show</code> on the {{Notification}} object representing
<var>notification</var>.
</ol>
</div>
<h3 id=activating-a-notification>Activating a notification</h3>
<div algorithm="activate steps">
<p>When a <a for=/>notification</a> <var>notification</var>, or one of its
<a for=notification>actions</a>, is activated by the end user, assuming the underlying notification
platform supports activation, the user agent must (unless otherwise specified) run these steps:
<ol>
<li>
<p>If <var>notification</var> is a <a>persistent notification</a>, then:
<ol>
<li><p>Let <var>action</var> be the empty string.
<li><p>If one of <var>notification</var>'s <a for=notification>actions</a> was activated by the
user, then set <var>action</var> to that <a for=/>notification action</a>'s
<a for="notification action">name</a>.
<li><a>Fire a service worker notification event</a> named "<code>notificationclick</code>" given
<var>notification</var> and <var>action</var>.
</ol>
<li>
<p>Otherwise, <a>queue a task</a> to run these steps:
<ol>
<li>
<p>Let <var>intoFocus</var> be the result of <a lt="fire an event">firing an event</a> named
<code>click</code> on the {{Notification}} object representing <var>notification</var>, with its
{{Event/cancelable}} attribute initialized to true.
<p class="note">User agents are encouraged to make {{Window/focus()}} work from within the event
listener for the event named <code>click</code>.
<li><p>If <var>intoFocus</var> is true, then the user agent should bring the
<var>notification</var>'s related <a for=/>browsing context</a>'s viewport into focus.
</ol>
</ol>
<p class="note">Throughout the web platform "activate" is intentionally misnamed as "click".
</div>
<h3 id=closing-a-notification>Closing a notification</h3>
<p>When a <a for=/>notification</a> is closed, either by the underlying notification platform or by
the end user, the <a>close steps</a> for it must be run.
<div algorithm>
<p>The <dfn>close steps</dfn> for a given <a for=/>notification</a> <var>notification</var> are:
<ol>
<li><p>If the <a>list of notifications</a> does not <a for=list>contain</a>
<var>notification</var>, then abort these steps.
<li><p><a>Handle close events</a> with <var>notification</var>.
<li><p><a for=list>Remove</a> <var>notification</var> from the <a>list of notifications</a>.
</ol>
</div>
<div algorithm>
<p>To <dfn>handle close events</dfn> given a <a for=/>notification</a> <var>notification</var>:
<ol>
<li><p>If <var>notification</var> is a <a>persistent notification</a> and <var>notification</var>
was closed by the end user, then <a>fire a service worker notification event</a> named
"<code>notificationclose</code>" given <var>notification</var>.
<li><p>If <var>notification</var> is a <a>non-persistent notification</a>, then <a>queue a task</a>
to <a>fire an event</a> named <code>close</code> on the {{Notification}} object representing
<var>notification</var>.
</ol>
</div>
<h3 id=alerting-the-user>Alerting the end user</h3>
<div algorithm>
<p>The <dfn>alert steps</dfn> for alerting the end user about a given <a for=/>notification</a>
<var>notification</var> are:
<ol>
<li><p><a>Perform vibration</a> using <var>notification</var>'s
<a for=notification>vibration pattern</a>, if any.
</ol>
</div>
<h2 id=api>API</h2>
<pre class=idl>
[Exposed=(Window,Worker)]
interface Notification : EventTarget {
constructor(DOMString title, optional NotificationOptions options = {});
static readonly attribute NotificationPermission permission;
[Exposed=Window] static Promise<NotificationPermission> requestPermission(optional NotificationPermissionCallback deprecatedCallback);
static readonly attribute unsigned long maxActions;
attribute EventHandler onclick;
attribute EventHandler onshow;
attribute EventHandler onerror;
attribute EventHandler onclose;
readonly attribute DOMString title;
readonly attribute NotificationDirection dir;
readonly attribute DOMString lang;
readonly attribute DOMString body;
readonly attribute DOMString tag;
readonly attribute USVString image;
readonly attribute USVString icon;
readonly attribute USVString badge;
[SameObject] readonly attribute FrozenArray<unsigned long> vibrate;
readonly attribute EpochTimeStamp timestamp;
readonly attribute boolean renotify;
readonly attribute boolean? silent;
readonly attribute boolean requireInteraction;
[SameObject] readonly attribute any data;
[SameObject] readonly attribute FrozenArray<NotificationAction> actions;
undefined close();
};
dictionary NotificationOptions {
NotificationDirection dir = "auto";
DOMString lang = "";
DOMString body = "";
DOMString tag = "";
USVString image;
USVString icon;
USVString badge;
VibratePattern vibrate;
EpochTimeStamp timestamp;
boolean renotify = false;
boolean? silent = null;
boolean requireInteraction = false;
any data = null;
sequence<NotificationAction> actions = [];
};
enum NotificationPermission {
"default",
"denied",
"granted"
};
enum NotificationDirection {
"auto",
"ltr",
"rtl"
};
dictionary NotificationAction {
required DOMString action;
required DOMString title;
USVString icon;
};
callback NotificationPermissionCallback = undefined (NotificationPermission permission);
</pre>
<p>A <a>non-persistent notification</a> is represented by one {{Notification}}
object and can be created through {{Notification}}'s
<a constructor lt="Notification(title, options)">constructor</a>.
<p>A <a>persistent notification</a> is represented by zero or more
{{Notification}} objects and can be created through the
{{ServiceWorkerRegistration/showNotification()}} method.
<h3 id=garbage-collection>Garbage collection</h3>
<p>A {{Notification}} object must not be garbage collected while the <a>list of notifications</a>
<a for=list>contains</a> its corresponding <a for=/>notification</a> and it has an
<a for=/>event listener</a> whose <b>type</b> is <code>click</code>, <code>show</code>,
<code>close</code>, or <code>error</code>.
<h3 id=constructors>Constructors</h3>
<div algorithm>
<p>The
<dfn constructor for=Notification lt="Notification(title, options)"><code>new Notification(<var>title</var>, <var>options</var>)</code></dfn>
constructor steps are:
<ol>
<li><p>If <a>this</a>'s <a>relevant global object</a> is a {{ServiceWorkerGlobalScope}} object,
then <a>throw</a> a {{TypeError}}.
<li>
<p>If <var>options</var>["{{NotificationOptions/actions}}"] is not <a for=list>empty</a>, then
<a>throw</a> a {{TypeError}}.
<p class=note><a>Actions</a> are only supported for <a>persistent notifications</a>.
<li><p>Let <var>notification</var> be the result of
<a>creating a notification with a settings object</a> given <var>title</var>, <var>options</var>,
and <a>this</a>'s <a>relevant settings object</a>.
<li><p>Associate <a>this</a> with <var>notification</var>.
<li>
<p>Run these steps <a>in parallel</a>:
<ol>
<li><p>If the result of <a>getting the notifications permission state</a> is not
"<code>granted</code>", then <a>queue a task</a> to <a>fire an event</a> named
<code>error</code> on <a>this</a>, and abort these steps.
<li><p>Run the <a for=/>notification show steps</a> for <var>notification</var>.
</ol>
</ol>
</div>
<h3 id=static-members>Static members</h3>
<div algorithm>
<p>The static <dfn attribute for=Notification><code>permission</code></dfn> getter steps are to
return the result of <a>getting the notifications permission state</a>.
</div>
<div class=note>
<p>If you edit standards please refrain from copying the above. Synchronous permissions are like
synchronous IO, a bad idea.
<p>Developers are encouraged to use the Permissions {{Permissions/query()}} method
instead. [[Permissions]]
<pre class="example" id="permissions-query-example">
const permission = await navigator.permissions.query({name: "notifications"});
if (permission.state === "granted") {
// We have permission to use the API…
}
</pre>
</div>
<div algorithm>
<p>The static
<dfn method for=Notification><code>requestPermission(<var>deprecatedCallback</var>)</code></dfn>
method steps are:
<ol>
<li><p>Let <var>global</var> be the <a>current global object</a>.
<li><p>Let <var>promise</var> be <a for=/>a new promise</a> in <a>this</a>'s <a>relevant Realm</a>.
<li>
<p>Run these steps <a>in parallel</a>:
<ol>
<li><p>Let <var>permissionState</var> be the result of
<a>requesting permission to use</a> "<a permission><code>notifications</code></a>".
<li>
<p><a>Queue a global task</a> on the <a>DOM manipulation task source</a> given <var>global</var>
to run these steps:
<ol>
<li><p>If <var>deprecatedCallback</var> is given, then <a for=/>invoke</a>
<var>deprecatedCallback</var> with « <var>permissionState</var> » and "<code>report</code>".
<li><p><a for=/>Resolve</a> <var>promise</var> with <var>permissionState</var>.
</ol>
</ol>
<li><p>Return <var>promise</var>.
</ol>
</div>
<p class="warning">Notifications are the one instance thus far where asking the end user upfront
makes sense. Specifications for other APIs should not use this pattern and instead employ one of the
<a href="http://robert.ocallahan.org/2011/06/permissions-for-web-applications_30.html">many more suitable alternatives</a>.
<p>The static <dfn attribute for=Notification><code>maxActions</code></dfn> getter steps are to
return the <a>maximum number of actions</a> supported.
<h3 id=object-members>Object members</h3>
<p>The following are the <a>event handlers</a> (and their corresponding
<a>event handler event types</a>) that must be supported as attributes by the
{{Notification}} object.
<table>
<thead>
<tr>
<th><a>event handler</a>
<th><a>event handler event type</a>
<tbody>
<tr>
<td><dfn attribute for=Notification><code>onclick</code></dfn>
<td><code>click</code>
<tr>
<td><dfn attribute for=Notification><code>onshow</code></dfn>
<td><code>show</code>
<tr>
<td><dfn attribute for=Notification><code>onerror</code></dfn>
<td><code>error</code>
<tr>
<td><dfn attribute for=Notification><code>onclose</code></dfn>
<td><code>close</code>
</table>
<p>The <dfn method for=Notification><code>close()</code></dfn> method steps are to run the
<a>close steps</a> for <a>this</a>'s <a for=/>notification</a>.
<p>The <dfn attribute for=Notification><code>title</code></dfn> getter steps are to return
<a>this</a>'s <a for=/>notification</a>'s <a for=notification>title</a>.
<p>The <dfn attribute for=Notification><code>dir</code></dfn> getter steps are to return
<a>this</a>'s <a for=/>notification</a>'s <a for=notification>direction</a>.
<p>The <dfn attribute for=Notification><code>lang</code></dfn> getter steps are to return
<a>this</a>'s <a for=/>notification</a>'s <a for=notification>language</a>.
<p>The <dfn attribute for=Notification><code>body</code></dfn> getter steps are to return
<a>this</a>'s <a for=/>notification</a>'s <a for=notification>body</a>.
<p>The <dfn attribute for=Notification><code>tag</code></dfn> getter steps are to return
<a>this</a>'s <a for=/>notification</a>'s <a for=notification>tag</a>.
<p>The <dfn attribute dfn-for=Notification><code>image</code></dfn> getter steps are:
<ol>
<li><p>If there is no <a>this</a>'s <a for=/>notification</a>'s <a for=notification>image URL</a>, then
return the empty string.
<li><p>Return <a>this</a>'s <a for=/>notification</a>'s <a for=notification>image URL</a>,
<a lt="URL serializer">serialized</a>.
</ol>
<p>The <dfn attribute for=Notification><code>icon</code></dfn> getter steps are:
<ol>
<li><p>If there is no <a>this</a>'s <a for=/>notification</a>'s <a for=notification>icon URL</a>, then
return the empty string.
<li><p>Return <a>this</a>'s <a for=/>notification</a>'s <a for=notification>icon URL</a>,
<a lt="URL serializer">serialized</a>.
</ol>
<p>The <dfn attribute for=Notification><code>badge</code></dfn> getter steps are:
<ol>
<li><p>If there is no <a>this</a>'s <a for=/>notification</a>'s <a for=notification>badge URL</a>, then
return the empty string.
<li><p>Return <a>this</a>'s <a for=/>notification</a>'s <a for=notification>badge URL</a>,
<a lt="URL serializer">serialized</a>.
</ol>
<p>The <dfn attribute for=Notification><code>vibrate</code></dfn> getter steps are to return
<a>this</a>'s <a for=/>notification</a>'s <a for=notification>vibration pattern</a>.
<p>The <dfn attribute for=Notification><code>timestamp</code></dfn> getter steps are to return
<a>this</a>'s <a for=/>notification</a>'s <a for=notification>timestamp</a>.
<p>The <dfn attribute for=Notification><code>renotify</code></dfn> getter steps are to return
<a>this</a>'s <a for=/>notification</a>'s <a for=notification>renotify preference</a>.
<p>The <dfn attribute for=Notification><code>silent</code></dfn> getter steps are to return
<a>this</a>'s <a for=/>notification</a>'s <a for=notification>silent preference</a>.
<p>The <dfn attribute for=Notification><code>requireInteraction</code></dfn> getter steps are to
return <a>this</a>'s <a for=/>notification</a>'s <a for=notification>require interaction preference</a>.
<p>The <dfn attribute for=Notification><code>data</code></dfn> getter steps are to return
<a abstract-op>StructuredDeserialize</a>(<a>this</a>'s <a for=/>notification</a>'s
<a for=notification>data</a>, <a>this</a>'s <a>relevant Realm</a>). If this throws an exception,
then return null.
<!-- Relies on SameObject becoming Cached as proposed -->
<p>The <dfn attribute for=Notification><code>actions</code></dfn> getter steps are:
<ol>
<li><p>Let <var>frozenActions</var> be an empty list of type {{NotificationAction}}.
<li>
<p><a for=list>For each</a> <var>entry</var> of <a>this</a>'s <a for=/>notification</a>'s
<a for=notification>actions</a>:
<ol>
<li><p>Let <var>action</var> be a new {{NotificationAction}}.
<li><p>Set <var>action</var>["{{NotificationAction/action}}"] to <var>entry</var>'s
<a for="notification action">name</a>.
<li><p>Set <var>action</var>["{{NotificationAction/title}}"] to <var>entry</var>'s
<a for="notification action">title</a>.
<li><p>If <var>entry</var>'s <a for="notification action">icon URL</a> is non-null, then set
<var>action</var>["{{NotificationAction/icon}}"] to <var>entry</var>'s
<a for="notification action">icon URL</a>, <a lt="URL serializer">serialized</a>.
<!-- XXX IDL dictionaries are usually returned by value, so don't need to be
immutable. But FrozenArray reifies the dictionaries to mutable JS
objects accessed by reference, so we explicitly freeze them.
It would be better to do this with IDL primitives instead of JS - see
https://www.w3.org/Bugs/Public/show_bug.cgi?id=29004 -->
<li><p>Call <a lt="freeze">Object.freeze</a> on <var>action</var>, to
prevent accidental mutation by scripts.
<li><p>Append <var>action</var> to <var>frozenActions</var>.
</ol>
<li><p>Return the result of <a>create a frozen array</a> from <var>frozenActions</var>.
</ol>
<h3 id=examples>Examples</h3>
<h4 id=using-events>Using events from a page</h4>
<p><a lt="non-persistent notification">Non-persistent</a> {{Notification}}
objects dispatch events during their lifecycle, which developers can use to
generate desired behaviors.
<p>The <code>click</code> event dispatches when the end user activates a notification.
<pre class=example id=example-08e8ecea>
var not = new Notification("Gebrünn Gebrünn by Paul Kalkbrenner", { icon: "newsong.svg", tag: "song" });
not.onclick = function() { displaySong(this); };</pre>
<h4 id=using-actions>Using actions from a service worker</h4>
<p><a lt="persistent notification">Persistent notifications</a> fire
<code>notificationclick</code> events on the {{ServiceWorkerGlobalScope}}.
<p>Here a service worker shows a notification with a single "Archive"
<a for=/>notification action</a>, allowing end users to perform this common task from the
notification without having to open the website (for example, the notification platform might show a
button on the notification). The end user can also activate the main body of the notification to
open their inbox.
<pre class=example id=example-50e7c86c>
self.registration.showNotification("New mail from Alice", {
actions: [{action: 'archive', title: "Archive"}]
});
self.addEventListener('notificationclick', function(event) {
event.notification.close();
if (event.action === 'archive') {
silentlyArchiveEmail();
} else {
clients.openWindow("/inbox");
}
}, false);</pre>
<h4 id=tags-example>Using the <code>tag</code> member for multiple instances</h4>
<p>Web applications frequently operate concurrently in multiple instances, such as when an end user
opens a mail application in multiple browser tabs. Since the desktop is a shared resource, the
notifications API provides a way for these instances to easily coordinate, by using the
<code>tag</code> member.
<p>Notifications which represent the same conceptual event can be tagged in the same way, and when
both are shown, the end user will only receive one notification.
<pre class=example id=example-2e2c735a>
Instance 1 | Instance 2
|
// Instance notices there is new mail. |