firebase / flutterfire

πŸ”₯ A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.72k stars 3.97k forks source link

Flutter web - Firebase cloud messaging - running OK on localhost, not when I upload to firebase hosting #10333

Closed davidsomekh closed 1 year ago

davidsomekh commented 1 year ago

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?

davidsomekh commented 1 year ago

Flutter doctor report:

flutter doctor -v [√] Flutter (Channel stable, 3.3.8, on Microsoft Windows [Version 10.0.19045.2486], locale en-GB) β€’ Flutter version 3.3.8 on channel stable at C:\flutter β€’ Upstream repository https://github.com/flutter/flutter.git β€’ Framework revision 52b3dc25f6 (3 months ago), 2022-11-09 12:09:26 +0800 β€’ Engine revision 857bd6b74c β€’ Dart version 2.18.4 β€’ DevTools version 2.15.0

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3) β€’ Android SDK at C:\Users\david.somekh\AppData\Local\Android\sdk β€’ Platform android-33, build-tools 30.0.3 β€’ ANDROID_HOME = C:\Users\david.somekh\AppData\Local\Android\sdk β€’ Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java β€’ Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6842174) β€’ All Android licenses accepted.

[√] Chrome - develop for the web β€’ Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Professional 2022 17.2.0) β€’ Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Professional β€’ Visual Studio Professional 2022 version 17.2.32505.173 β€’ Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 4.2) β€’ Android Studio at C:\Program Files\Android\Android Studio β€’ Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter β€’ Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart β€’ Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6842174)

[√] VS Code (version 1.74.3) β€’ VS Code at C:\Users\david.somekh\AppData\Local\Programs\Microsoft VS Code

[√] Connected device (3 available) β€’ Windows (desktop) β€’ windows β€’ windows-x64 β€’ Microsoft Windows [Version 10.0.19045.2486] β€’ Chrome (web) β€’ chrome β€’ web-javascript β€’ Google Chrome 109.0.5414.76 β€’ Edge (web) β€’ edge β€’ web-javascript β€’ Microsoft Edge 109.0.1518.61

[√] HTTP Host Availability β€’ All required HTTP hosts are available

β€’ No issues found! PS C:\apps\keep> flutter doctor -v [√] Flutter (Channel stable, 3.3.8, on Microsoft Windows [Version 10.0.19045.2486], locale en-GB) β€’ Flutter version 3.3.8 on channel stable at C:\flutter β€’ Upstream repository https://github.com/flutter/flutter.git β€’ Framework revision 52b3dc25f6 (3 months ago), 2022-11-09 12:09:26 +0800 β€’ Engine revision 857bd6b74c β€’ Dart version 2.18.4 β€’ DevTools version 2.15.0

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3) β€’ Android SDK at C:\Users\david.somekh\AppData\Local\Android\sdk β€’ Platform android-33, build-tools 30.0.3 β€’ ANDROID_HOME = C:\Users\david.somekh\AppData\Local\Android\sdk β€’ Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java β€’ Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6842174) β€’ All Android licenses accepted.

[√] Chrome - develop for the web β€’ Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Professional 2022 17.2.0) β€’ Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Professional β€’ Visual Studio Professional 2022 version 17.2.32505.173 β€’ Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 4.2) β€’ Android Studio at C:\Program Files\Android\Android Studio β€’ Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter β€’ Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart β€’ Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6842174)

[√] VS Code (version 1.74.3) β€’ VS Code at C:\Users\david.somekh\AppData\Local\Programs\Microsoft VS Code β€’ Flutter extension version 3.58.0

[√] Connected device (3 available) β€’ Windows (desktop) β€’ windows β€’ windows-x64 β€’ Microsoft Windows [Version 10.0.19045.2486] β€’ Chrome (web) β€’ chrome β€’ web-javascript β€’ Google Chrome 109.0.5414.76 β€’ Edge (web) β€’ edge β€’ web-javascript β€’ Microsoft Edge 109.0.1518.61

[√] HTTP Host Availability β€’ All required HTTP hosts are available

β€’ No issues found!

darshankawar commented 1 year ago

Additionally, I added the service worker code as explained here: https://firebase.flutter.dev/docs/messaging/usage/

@davidsomekh This document link is archive and not updated anymore. Can you try https://firebase.google.com/docs/cloud-messaging/flutter/client#web, follow the instructions from there and see if they help in your case ?

davidsomekh commented 1 year ago

@darshankawar

Yes, I tried also the setup with these instructions - same result.

davidsomekh commented 1 year ago

Here is my pubspec.yaml:

cupertino_icons: ^1.0.2 firebase_auth: ^4.1.2 google_sign_in: ^5.0.3 firebase_core: ^2.2.0 shared_preferences: ^2.0.16 flutter_local_notifications: ^9.3.1 cloud_firestore: ^4.0.5 font_awesome_flutter: ^10.2.1 provider: ^6.0.4 cloud_functions: ^4.0.7 rxdart: ^0.27.7 firebase_messaging: ^14.2.1

darshankawar commented 1 year ago

Thanks for the update. I tried with plugin example and uploaded to firebase hosting which worked properly without any issues. Can you try the same ? Looking at the error, it seems to be a config issue rather than the plugin, because it is unable to register the said method.

davidsomekh commented 1 year ago

Running flutter clean solved the problem - you can close the issue