Skip to content

Commit c42b57c

Browse files
test(messaging): update tests app for messaging web (#12332)
1 parent b586a5d commit c42b57c

File tree

3 files changed

+67
-44
lines changed

3 files changed

+67
-44
lines changed

tests/integration_test/firebase_messaging/firebase_messaging_e2e_test.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:integration_test/integration_test.dart';
1212
import 'package:tests/firebase_options.dart';
1313

1414
// ignore: do_not_use_environment
15-
const bool skipManualTests = bool.fromEnvironment('CI');
15+
const bool skipTestsOnCI = bool.fromEnvironment('CI');
1616

1717
void main() {
1818
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
@@ -110,7 +110,8 @@ void main() {
110110
AuthorizationStatus.notDetermined,
111111
);
112112
},
113-
skip: !kIsWeb,
113+
// This requires interaction with the browser's permission dialog, it no longer returns `notDetermined` on web
114+
skip: true,
114115
);
115116
});
116117

@@ -146,7 +147,7 @@ void main() {
146147
expect(result, isA<String>());
147148
});
148149
},
149-
skip: skipManualTests,
150+
skip: skipTestsOnCI,
150151
); // only run for manual testing
151152

152153
group('deleteToken()', () {
@@ -162,7 +163,7 @@ void main() {
162163
expect(token2, isA<String>());
163164
expect(token1, isNot(token2));
164165
},
165-
skip: skipManualTests,
166+
skip: skipTestsOnCI,
166167
); // only run for manual testing
167168
});
168169

tests/web/firebase-messaging-sw.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
importScripts("https://www.gstatic.com/firebasejs/10.7.0/firebase-app-compat.js");
2+
importScripts("https://www.gstatic.com/firebasejs/10.7.0/firebase-messaging-compat.js");
3+
4+
firebase.initializeApp({
5+
apiKey: 'AIzaSyB7wZb2tO1-Fs6GbDADUSTs2Qs3w08Hovw',
6+
appId: '1:406099696497:web:87e25e51afe982cd3574d0',
7+
messagingSenderId: '406099696497',
8+
projectId: 'flutterfire-e2e-tests',
9+
authDomain: 'flutterfire-e2e-tests.firebaseapp.com',
10+
databaseURL:
11+
'https://flutterfire-e2e-tests-default-rtdb.europe-west1.firebasedatabase.app',
12+
storageBucket: 'flutterfire-e2e-tests.appspot.com',
13+
measurementId: 'G-JN95N1JV2E',
14+
});
15+
// Necessary to receive background messages:
16+
const messaging = firebase.messaging();
17+
18+
// Optional:
19+
messaging.onBackgroundMessage((m) => {
20+
console.log("onBackgroundMessage", m);
21+
});

tests/web/index.html

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,33 @@
3333
<link rel="manifest" href="manifest.json">
3434
</head>
3535
<body>
36-
<!-- This script installs service_worker.js to provide PWA functionality to
37-
application. For more information, see:
38-
https://developers.google.com/web/fundamentals/primers/service-workers -->
39-
<script>
40-
var serviceWorkerVersion = null;
41-
var scriptLoaded = false;
42-
function loadMainDartJs() {
43-
if (scriptLoaded) {
44-
return;
36+
<script>
37+
var serviceWorkerVersion = null;
38+
var scriptLoaded = false;
39+
function loadMainDartJs() {
40+
if (scriptLoaded) {
41+
return;
42+
}
43+
scriptLoaded = true;
44+
var scriptTag = document.createElement('script');
45+
scriptTag.src = 'main.dart.js';
46+
scriptTag.type = 'application/javascript';
47+
document.body.append(scriptTag);
4548
}
46-
scriptLoaded = true;
47-
var scriptTag = document.createElement('script');
48-
scriptTag.src = 'main.dart.js';
49-
scriptTag.type = 'application/javascript';
50-
document.body.append(scriptTag);
51-
}
5249

53-
if ('serviceWorker' in navigator) {
54-
// Service workers are supported. Use them.
55-
window.addEventListener('load', function () {
56-
// Wait for registration to finish before dropping the <script> tag.
57-
// Otherwise, the browser will load the script multiple times,
58-
// potentially different versions.
59-
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
60-
navigator.serviceWorker.register(serviceWorkerUrl)
61-
.then((reg) => {
50+
if ('serviceWorker' in navigator) {
51+
// Service workers are supported. Use them.
52+
window.addEventListener('load', function () {
53+
// Register Firebase Messaging service worker.
54+
navigator.serviceWorker.register('firebase-messaging-sw.js');
55+
56+
// Wait for registration to finish before dropping the <script> tag.
57+
// Otherwise, the browser will load the script multiple times,
58+
// potentially different versions.
59+
var serviceWorkerUrl =
60+
'flutter_service_worker.js?v=' + serviceWorkerVersion;
61+
62+
navigator.serviceWorker.register(serviceWorkerUrl).then((reg) => {
6263
function waitForActivation(serviceWorker) {
6364
serviceWorker.addEventListener('statechange', () => {
6465
if (serviceWorker.state == 'activated') {
@@ -70,7 +71,7 @@
7071
if (!reg.active && (reg.installing || reg.waiting)) {
7172
// No active web worker and we have installed or are installing
7273
// one for the first time. Simply wait for it to activate.
73-
waitForActivation(reg.installing || reg.waiting);
74+
waitForActivation(reg.installing ?? reg.waiting);
7475
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
7576
// When the app updates the serviceWorkerVersion changes, so we
7677
// need to ask the service worker to update.
@@ -84,21 +85,21 @@
8485
}
8586
});
8687

87-
// If service worker doesn't succeed in a reasonable amount of time,
88-
// fallback to plaint <script> tag.
89-
setTimeout(() => {
90-
if (!scriptLoaded) {
91-
console.warn(
92-
'Failed to load app from service worker. Falling back to plain <script> tag.',
93-
);
94-
loadMainDartJs();
95-
}
96-
}, 4000);
97-
});
98-
} else {
99-
// Service workers not supported. Just drop the <script> tag.
100-
loadMainDartJs();
101-
}
88+
// If service worker doesn't succeed in a reasonable amount of time,
89+
// fallback to plaint <script> tag.
90+
setTimeout(() => {
91+
if (!scriptLoaded) {
92+
console.warn(
93+
'Failed to load app from service worker. Falling back to plain <script> tag.'
94+
);
95+
loadMainDartJs();
96+
}
97+
}, 4000);
98+
});
99+
} else {
100+
// Service workers not supported. Just drop the <script> tag.
101+
loadMainDartJs();
102+
}
102103
</script>
103104
</body>
104105
</html>

0 commit comments

Comments
 (0)