Skip to content

Commit 9659749

Browse files
authored
docs(messaging, web): update the documentation for how to load the service worker in Flutter 3.22 (#12836)
* doc(messaging, web): update the documentation for how to load the service worker in Flutter 3.22 * doc(messaging, web): update the documentation for how to load the service worker in Flutter 3.22
1 parent 8e0ced4 commit 9659749

File tree

8 files changed

+68
-62
lines changed

8 files changed

+68
-62
lines changed

docs/cloud-messaging/receive.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,23 @@ messaging.onBackgroundMessage((message) => {
185185

186186
The file must import both the app and messaging SDKs, initialize Firebase and expose the `messaging` variable.
187187

188-
Next, the worker must be registered. Within the entry file, **after** the `main.dart.js` file has loaded, register your worker:
188+
Next, the worker must be registered. Within the `index.html` file, register the worker by modifying the `<script>` tag which bootstraps Flutter:
189+
190+
```html
191+
<script src="flutter_bootstrap.js" async>
192+
if ('serviceWorker' in navigator) {
193+
window.addEventListener('load', function () {
194+
navigator.serviceWorker.register('firebase-messaging-sw.js', {
195+
scope: '/firebase-cloud-messaging-push-scope',
196+
});
197+
});
198+
}
199+
</script>
200+
```
201+
202+
If you are still using the old templating system, you can register the worker by modifying the `<script>` tag which bootstraps Flutter as follows:
189203

190-
```js
204+
```html
191205
<html>
192206
<body>
193207
<script>
@@ -260,6 +274,7 @@ Next, the worker must be registered. Within the entry file, **after** the `main.
260274
loadMainDartJs();
261275
}
262276
</script>
277+
</body>
263278
```
264279
265280
Next restart your Flutter application. The worker will be registered and any background messages will be handled via this file.
Loading
Loading
Loading
Loading
Loading

packages/firebase_messaging/firebase_messaging/example/web/index.html

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,34 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4+
<base href="$FLUTTER_BASE_HREF" />
5+
46
<meta charset="UTF-8" />
7+
<meta content="IE=Edge" http-equiv="X-UA-Compatible" />
8+
<meta name="description" content="A new Flutter project." />
9+
10+
<!-- iOS meta tags & icons -->
11+
<meta name="apple-mobile-web-app-capable" content="yes" />
12+
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
13+
<meta name="apple-mobile-web-app-title" content="example" />
14+
<link rel="apple-touch-icon" href="icons/Icon-192.png" />
15+
16+
<!-- Favicon -->
17+
<link rel="icon" type="image/png" href="favicon.png" />
18+
519
<title>example</title>
20+
<link rel="manifest" href="manifest.json" />
621
</head>
722
<body>
8-
<script>
9-
var serviceWorkerVersion = null;
10-
var scriptLoaded = false;
11-
function loadMainDartJs() {
12-
if (scriptLoaded) {
13-
return;
14-
}
15-
scriptLoaded = true;
16-
var scriptTag = document.createElement('script');
17-
scriptTag.src = 'main.dart.js';
18-
scriptTag.type = 'application/javascript';
19-
document.body.append(scriptTag);
20-
}
21-
23+
<script src="flutter_bootstrap.js" async>
2224
if ('serviceWorker' in navigator) {
2325
// Service workers are supported. Use them.
2426
window.addEventListener('load', function () {
2527
// Register Firebase Messaging service worker.
2628
navigator.serviceWorker.register('firebase-messaging-sw.js', {
2729
scope: '/firebase-cloud-messaging-push-scope',
2830
});
29-
30-
// Wait for registration to finish before dropping the <script> tag.
31-
// Otherwise, the browser will load the script multiple times,
32-
// potentially different versions.
33-
var serviceWorkerUrl =
34-
'flutter_service_worker.js?v=' + serviceWorkerVersion;
35-
36-
navigator.serviceWorker.register(serviceWorkerUrl).then((reg) => {
37-
function waitForActivation(serviceWorker) {
38-
serviceWorker.addEventListener('statechange', () => {
39-
if (serviceWorker.state == 'activated') {
40-
console.log('Installed new service worker.');
41-
loadMainDartJs();
42-
}
43-
});
44-
}
45-
if (!reg.active && (reg.installing || reg.waiting)) {
46-
// No active web worker and we have installed or are installing
47-
// one for the first time. Simply wait for it to activate.
48-
waitForActivation(reg.installing ?? reg.waiting);
49-
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
50-
// When the app updates the serviceWorkerVersion changes, so we
51-
// need to ask the service worker to update.
52-
console.log('New service worker available.');
53-
reg.update();
54-
waitForActivation(reg.installing);
55-
} else {
56-
// Existing service worker is still good.
57-
console.log('Loading app from service worker.');
58-
loadMainDartJs();
59-
}
60-
});
61-
62-
// If service worker doesn't succeed in a reasonable amount of time,
63-
// fallback to plaint <script> tag.
64-
setTimeout(() => {
65-
if (!scriptLoaded) {
66-
console.warn(
67-
'Failed to load app from service worker. Falling back to plain <script> tag.'
68-
);
69-
loadMainDartJs();
70-
}
71-
}, 4000);
7231
});
73-
} else {
74-
// Service workers not supported. Just drop the <script> tag.
75-
loadMainDartJs();
7632
}
7733
</script>
7834
</body>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "example",
3+
"short_name": "example",
4+
"start_url": ".",
5+
"display": "standalone",
6+
"background_color": "#0175C2",
7+
"theme_color": "#0175C2",
8+
"description": "A new Flutter project.",
9+
"orientation": "portrait-primary",
10+
"prefer_related_applications": false,
11+
"icons": [
12+
{
13+
"src": "icons/Icon-192.png",
14+
"sizes": "192x192",
15+
"type": "image/png"
16+
},
17+
{
18+
"src": "icons/Icon-512.png",
19+
"sizes": "512x512",
20+
"type": "image/png"
21+
},
22+
{
23+
"src": "icons/Icon-maskable-192.png",
24+
"sizes": "192x192",
25+
"type": "image/png",
26+
"purpose": "maskable"
27+
},
28+
{
29+
"src": "icons/Icon-maskable-512.png",
30+
"sizes": "512x512",
31+
"type": "image/png",
32+
"purpose": "maskable"
33+
}
34+
]
35+
}

0 commit comments

Comments
 (0)