Я пытаюсь настроить уведомления firebase во Flutter Web. Я следил за этой средней статьей , и мне удалось выполнить базовую настройку c. Но когда пользователь нажимает кнопку «Разрешить предоставление разрешений», я получаю эту ошибку.
Expected a value of type 'FirebaseError', but got one of type 'DomException'
Это мой index.html
файл
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="description" content="Lorem ipsum">
<meta name="robots" content="noindex">
<!-- iOS meta tags & icons -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="Flutter web notifications">
<meta id="flutterweb-theme" name="theme-color" content="#393557">
<link rel="icon" type="image/png" href="/icons/Icon-192.png">
<link rel="apple-touch-icon" href="/icons/Icon-192.png">
<title>Flutter web notifications</title>
<link rel="manifest" href="./manifest.json">
</head>
<body id="app-container">
<!-- This script installs service_worker.js to provide PWA functionality to
application. For more information, see:
https://developers.google.com/web/fundamentals/primers/service-workers -->
<script src="https://www.gstatic.com/firebasejs/7.9.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.9.1/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.9.1/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.9.1/firebase-messaging.js"></script>
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
// navigator.serviceWorker.register('/flutter_service_worker.js');
navigator.serviceWorker.register("./firebase-messaging-sw.js");
});
}
</script>
<script src="https://www.gstatic.com/firebasejs/7.11.0/firebase-analytics.js"></script>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "API_KEY",
authDomain: "AUTH_DOMAIN",
databaseURL: "DATABASE_URL",
projectId: "PROJECT_ID",
storageBucket: "STORAGE_BUCKET",
messagingSenderId: "MESSAGING_SENDER_ID",
appId: "APP_ID",
measurementId: "MEASUREMENT_ID"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
</script>
<!-- <script src="/__/firebase/init.js"></script> -->
<script src="main.dart.js" type="application/javascript"></script>
</body>
</html>
Это the firebase-messaging-sw.js
importScripts("https://www.gstatic.com/firebasejs/7.9.1/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/7.9.1/firebase-messaging.js");
var firebaseConfig = {
apiKey: "API_KEY",
authDomain: "AUTH_DOMAIN",
databaseURL: "DATABASE_URL",
projectId: "PROJECT_ID",
storageBucket: "STORAGE_BUCKET",
messagingSenderId: "MESSAGING_SENDER_ID",
appId: "APP_ID",
measurementId: "MEASUREMENT_ID"
};
firebase.initializeApp(firebaseConfig);
// firebase.analytics();
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function (payload) {
const promiseChain = clients
.matchAll({
type: "window",
includeUncontrolled: true
})
.then(windowClients => {
for (let i = 0; i < windowClients.length; i++) {
const windowClient = windowClients[i];
windowClient.postMessage(payload);
}
})
.then(() => {
return registration.showNotification("New Message");
});
return promiseChain;
});
self.addEventListener('notificationclick', function (event) {
console.log('notification received: ', event)
});
Это firebase_messaging.dart
import 'dart:async';
import 'package:firebase/firebase.dart' as firebase;
class FBMessaging {
FBMessaging._();
static FBMessaging _instance = FBMessaging._();
static FBMessaging get instance => _instance;
// firebase.Messaging _mc;
var _mc;
String _token;
final _controller = StreamController<Map<String, dynamic>>.broadcast();
Stream<Map<String, dynamic>> get stream => _controller.stream;
void close() {
_controller?.close();
}
Future<void> init() async {
_mc = firebase.messaging();
_mc.usePublicVapidKey('FCM_SERVER_KEY');
_mc.onMessage.listen((event) {
_controller.add(event?.data);
});
}
Future requestPermission() {
// await init();
return _mc.requestPermission();
}
Future<String> getToken([bool force = false]) async {
if (force || _token == null) {
// await requestPermission();
_token = await _mc.getToken();
}
return _token;
}
}
Вот как я запрашиваю разрешения у пользователя
final _messaging = FBMessaging.instance;
() async {
_messaging
.init()
.then((_) async {
await _messaging
.requestPermission()
.then((_) async {
final _token =
await _messaging
.getToken();
print('Token: $_token');
});
});
}
Но это это ошибка, которую я получаю, когда пользователь нажимает кнопку "Разрешить"
Error: Expected a value of type 'FirebaseError', but got one of type 'DomException'
at Object.throw_ [as throw] (http://localhost:44987/dart_sdk.js:4461:11)
at Object.castError (http://localhost:44987/dart_sdk.js:4432:15)
at Object.cast [as as] (http://localhost:44987/dart_sdk.js:4748:17)
at dart.AnonymousJSType.new.as (http://localhost:44987/dart_sdk.js:6186:64)
at handleThenable (http://localhost:44987/packages/firebase/src/storage.dart.lib.js:3264:96)
at handleThenable.throw (<anonymous>)
at http://localhost:44987/dart_sdk.js:36909:38
at _RootZone.runBinary (http://localhost:44987/dart_sdk.js:36762:58)
at _FutureListener.thenAwait.handleError (http://localhost:44987/dart_sdk.js:31933:48)
at handleError (http://localhost:44987/dart_sdk.js:32475:51)
at Function._propagateToListeners (http://localhost:44987/dart_sdk.js:32498:17)
at _Future.new.[_completeError] (http://localhost:44987/dart_sdk.js:32354:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:44987/dart_sdk.js:32392:31)
at Object._microtaskLoop (http://localhost:44987/dart_sdk.js:37015:13)
at _startMicrotaskLoop (http://localhost:44987/dart_sdk.js:37021:13)