-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.html
368 lines (365 loc) · 12.8 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>
Badging API
</title>
<script src="https://www.w3.org/Tools/respec/respec-w3c" class=
"remove"></script>
<script class='remove'>
var respecConfig = {
specStatus: "ED",
previousPublishDate: "2023-04-06",
shortName: "badging",
prevVersion: "FPWD",
previousMaturity: "WD",
github: {
repoURL: "w3c/badging",
branch: "main"
},
group: "webapps",
editors: [{
name: "Marcos Cáceres",
company: "Apple Inc.",
companyURL: "https://apple.com",
w3cid: 39125,
},
{
name: "Matt Giuca",
company: "Google Inc.",
companyURL: "https://google.com",
retiredDate: "2022-12-21",
},
{
name: "Jay Harris",
company: "Google Inc.",
companyURL: "https://google.com",
retiredDate: "2019-12-31"
},
{
name: "Diego González",
url: "https://twitter.com/diekus",
company: "Microsoft",
companyURL: "https://microsoft.com",
w3cid: 66757
}],
mdn: true,
xref: "web-platform",
};
</script>
</head>
<body data-cite="appmanifest">
<section id="abstract">
<p>
This specification defines an API that allows [=installed web
applications=] to set an application badge, which is usually shown
alongside the application's icon on the device's home screen or
application dock.
</p>
</section>
<section id="sotd">
<p>
This is a work in progress.
</p>
</section>
<section class="informative">
<h2>
Usage examples
</h2>
<pre class="example javascript" title=
"Showing unread count on the app icon">
async function updateMailBadge() {
// Check if the API is supported.
if (!navigator.setAppBadge) return;
const unreadCount = await getUnreadMailCount();
// Try to set the app badge.
try {
await navigator.setAppBadge(unreadCount);
} catch (e) {
// The badge is not supported, or the user has prevented the app
// from setting a badge.
}
}
</pre>
<p>
The badge might show up on the application's icon in the operating
system. If multiple API calls within the same application [=badge/set=]
or [=badge/clear=] a badge, the most recent one takes effect, and may
continue being seen even after an application is closed.
</p>
<pre class="example javascript" title=
"Showing ready status on the app icon">
async function showPlayerTurn(playerTurnId) {
if (playerTurnId === localPlayerId)
await navigator.setAppBadge();
else
await navigator.clearAppBadge();
}
</pre>
<p data-cite="notifications">
On some operating systems [=badge/setting=] a badge can require
permission from the user. In this case, a developer need to query the
"[=notifications=]" permissions status before [=badge/setting=] a
badge. If the permission is not granted, the developer will need to
prompt for permission via the
{{Notification}}.{{Notification/requestPermission()}}.
</p>
<pre class="example javascript" title="Checking for permission">
async function checkPermission() {
permission = await navigator.permissions.query({
name: "notifications",
});
const button = document.getElementById("permission-button");
if (permission.state === "prompt") {
// Prompt the user to grant permission.
button.hidden = false;
button.addEventListener("click", async () => {
await Notification.requestPermission();
checkPermission();
}, { once: true });
return;
}
button.hidden = true;
}
</pre>
</section>
<section>
<h2>
Model
</h2>
<p>
A <dfn>badge</dfn> is intended as a mechanism for [=installed web
applications=] to notify the user that there is some new activity that
might require their attention, or as a way of indicating a small amount
of information, such as an unread count.
</p>
<p>
A [=badge=] can have one of the following <dfn data-dfn-for=
"badge">values</dfn>:
</p>
<dl>
<dt>
The special value <dfn>"nothing"</dfn>:
</dt>
<dd>
Indicates that there is no badge currently [=badge/set=].
</dd>
<dt>
The special value <dfn>"flag"</dfn>:
</dt>
<dd>
Indicates that the badge is [=badge/set=], but contains no specific
value.
</dd>
<dt>
A <dfn>number</dfn> value:
</dt>
<dd>
Indicates that the badge is [=badge/set=] to a numerical value
greater than `0`.
</dd>
</dl>
<p>
An [=installed web application=] has an associated [=badge=], which is
initialized to [="nothing"=].
</p>
<p>
The user agent MAY (re)[=set=] an application's badge to [="nothing"=]
at its discretion (for example, following system conventions).
</p>
</section>
<section id="presentation">
<h2>
Displaying a badge
</h2>
<p>
When the application's badge is <dfn data-dfn-for="badge" data-lt=
"setting">set</dfn>, the [=user agent=] or operating system SHOULD
display the application's [=badge=] alongside the primary iconic
representation of the application in the user's operating system (for
example, as a small overlay on top of the application's icon on the
home screen on a device).
</p>
<p data-cite="permissions notifications">
A user agent MAY require [=express permission=] from the user to
[=badge/set=] the badge. When a user agent requires such
[=permission=], it SHOULD tie the permission grant to the
"[=notifications=]" permission.
</p>
<p>
When the [=badge=] is [=badge/set=] to [="flag"=], the [=user agent=]
or operating system SHOULD display an indicator with a non-specific
symbol (for example, a colored circle).
</p>
<p>
When a [=badge=]'s value is [=badge/set=] to [="nothing"=], the [=user
agent=] or operating system SHOULD <dfn data-dfn-for="badge"
data-local-lt="clearing|cleared">clear</dfn> the [=badge=] by no longer
displaying it.
</p>
<p>
When the [=badge=] is [=badge/set=] to a [=number=], the [=user agent=]
or operating system:
</p>
<ul>
<li>SHOULD format and display the number according to the user's font
and formatting preferences. For example, as a number in a circle on top
of the application's icon on the home screen on a device.
</li>
<li>MAY simplify or degrade a badge's [=number=] [=badge/value=] in any
way that matches the platform's conventions for representing numbers in
a badge. For example, a platform might choose to display a badge with a
value of '100' as '99+'.
</li>
<li>SHOULD localize the number according to the user's locale
preferences. For example, the [=badge/value=] '7' should be displayed
as '7' in the locale 'en' (English) but as '٧' in 'ar' (Arabic).
</li>
<li>MAY treat a [=number=] [=badge/value=] as [="flag"=], or whatever
representation is most appropriate for the platform, if the platform
does not support displaying [=numbers=] in a [=badge=].
</li>
</ul>
</section>
<section data-dfn-for="NavigatorBadge">
<h2>
Extensions to the `Navigator` and `WorkerNavigator` interfaces
</h2>
<pre class="idl">
[SecureContext]
interface mixin NavigatorBadge {
Promise<undefined> setAppBadge(
optional [EnforceRange] unsigned long long contents
);
Promise<undefined> clearAppBadge();
};
Navigator includes NavigatorBadge;
WorkerNavigator includes NavigatorBadge;
</pre>
<p>
User agents that never display application badges SHOULD NOT
[=interface/include=] {{NavigatorBadge}}.
</p>
<h3>
<dfn>setAppBadge()</dfn> method
</h3>
<p>
When the {{NavigatorBadge/setAppBadge()}} method is called, the user
agent MUST [=set the application badge=] of [=this=] to value of the
|contents:unsigned long long| argument.
</p>
<h3>
<dfn>clearAppBadge()</dfn> method
</h3>
<p>
When the {{NavigatorBadge/clearAppBadge()}} method is called, the user
agent MUST [=set the application badge=] of [=this=] to 0.
</p>
</section>
<section data-cite="permissions notifications">
<h2>
Setting the application badge
</h2>
<p>
To <dfn>set the application badge</dfn> of platform object
|context:platform object| to an optional {{unsigned long long}}
|contents:unsigned long long| value:
</p>
<ol class="algorithm">
<li>Let |global| be |context|'s [=relevant global object=].
</li>
<li>If |global| is a {{Window}} object, then:
<ol>
<li>Let |document| be |global|'s [=associated `Document`=].
</li>
<li>If |document| is not [=Document/fully active=], return [=a
promise rejected with=] a {{"InvalidStateError"}} {{DOMException}}.
</li>
</ol>
</li>
<li>Let |promise| be [=a new promise=].
</li>
<li>[=In parallel=]:
<ol>
<li>If [=this=]'s [=relevant settings object=]'s [=origin=] is not
[=same origin=] with [=this=]'s [=relevant settings object=]'s
[=environment/top-level origin=], [=queue a global task=] on the
[=DOM manipulation task source=] given |global| to [=reject=]
|promise| with a {{"SecurityError"}} and terminate this algorithm.
</li>
<li>If the [=user agent=] requires [=express permission=] to
[=badge/set=] the application badge, then:
<ol>
<li>Let |permissionState| be the result of [=getting the
current permission state=] with "[=notifications=]".
</li>
<li>If |permissionState| is not {{PermissionState/"granted"}},
[=queue a global task=] on the [=user interaction task source=]
given |global| to [=reject=] |promise| with a
{{NotAllowedError}} and terminate this algorithm.
</li>
</ol>
</li>
<li>Switching on |contents|, if it happens to be the case that:
<dl class="switch">
<dt>
|contents| was not passed:
</dt>
<dd>
[=badge/Set=] |badge| to [="flag"=].
</dd>
<dt>
|contents| is 0:
</dt>
<dd>
[=badge/Set=] |badge| to [="nothing"=].
</dd>
<dt>
|contents|:
</dt>
<dd>
[=badge/Set=] |badge| to |contents|.
</dd>
</dl>
</li>
<li>[=Queue a global task=] on the [=DOM manipulation task source=]
given |global| to [=resolve=] |promise| with `undefined`.
</li>
</ol>
</li>
<li>Return |promise|.
</li>
</ol>
</section>
<section>
<h2>
Privacy considerations
</h2>
<p>
The API is write-only by design. There is no way for a site to read
back the value of a badge that was previously set, to ensure that the
application badge cannot be used as a storage or fingerprinting
mechanism.
</p>
</section>
<section>
<h2>
Security considerations
</h2>
<p>
The [=user agent=] or operating system MAY [=badge/clear=] a [=badge=]
at its discretion, and to follow any system conventions (for example,
when the system is reset).
</p>
<aside class="issue" data-number="68"></aside>
</section>
<section class="informative">
<h2>
Accessibility considerations
</h2>
<div class="issue" data-number="24"></div>
</section>
<section id="conformance"></section>
</body>
</html>