I am trying to implement FCM for a flutter web project.
When I run the project in the localhost, it works fine. But when I upload it to firebase hosting (After running flutter build web), I am getting errors.
On the main.dart file, I am trying to get the device token:
final fcmToken = await FirebaseMessaging.instance.getToken(
vapidKey:
"my_key");
However, I am getting this error in the console:
main.dart.js:4430 Uncaught MissingPluginException(No implementation found for method Messaging#getToken on channel plugins.flutter.io/firebase_messaging)
I added the firebase messaging package in my pubspec.yaml file:
firebase_messaging: ^14.2.1
Additionally, I added the service worker code as explained here: https://firebase.flutter.dev/docs/messaging/usage/
Here is my index.html script code:
<script>
window.addEventListener("load", function (ev) {
// Download main.dart.js
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/firebase-messaging-sw.js");
}
_flutter.loader
.loadEntrypoint({
serviceWorker: {
serviceWorkerVersion: serviceWorkerVersion,
},
})
.then(function (engineInitializer) {
return engineInitializer.initializeEngine();
})
.then(function (appRunner) {
return appRunner.runApp();
});
});
</script>
It looks like a problem with the firebase_messaging package, but I'm using the latest one. Also what I can't understand is why it's working in localhost but not production.
Thoughts anyone?