admin管理员组

文章数量:1295912

I'm using flutter web with firebase_messaging

I tried to follow the documentation's steps:

I created a VAPID:

I'm getting the token with

    await FirebaseMessaging.instance.requestPermission(provisional: true);
    final fmcToken = await FirebaseMessaging.instance.getToken(
      vapidKey: kIsWeb
          ? 'xxx'
          : null,
    );
    print('token: $fmcToken');

:

which gives me a token:

Launching lib/main.dart on Chrome in debug mode...
This app is linked to the debug service: ws://127.0.0.1:63406/1xasoU1wJzY=/ws
Debug service listening on ws://127.0.0.1:63406/1xasoU1wJzY=/ws
Connecting to VM Service at ws://127.0.0.1:63406/1xasoU1wJzY=/ws
token: xxx

I'm then using it to send a test notification:

But I'm not receiving any notification.

I did allowed the notifications:

I also included the web/firebase-messaging-sw.js:

importScripts(".11.1/firebase-app-compat.js");
importScripts(".11.1/firebase-messaging-compat.js");

firebase.initializeApp({
  apiKey: 'xxx',
  appId: 'xxx',
  messagingSenderId: 'xxx',
  projectId: 'xxx',
  authDomain: 'xxx',
  storageBucket: 'xxx',
  measurementId: 'xxx',
});
// Necessary to receive background messages:
const messaging = firebase.messaging();

// Optional:
messaging.onBackgroundMessage((m) => {
  console.log("onBackgroundMessage", m);
});

and here is my web/index.html:

 <body>
    <script>
      window.addEventListener("load", function (ev) {
        // Download main.dart.js
        _flutter.loader.loadEntrypoint({
          serviceWorker: {
            serviceWorkerVersion: serviceWorkerVersion,
          },
          onEntrypointLoaded: function (engineInitializer) {
            engineInitializer.initializeEngine().then(function (appRunner) {
              appRunner.runApp();
            });
          },
        });
      });
    </script>
    <script>
      if ("serviceWorker" in navigator) {
        window.addEventListener("load", function () {
          navigator.serviceWorker.register("firebase-messaging-sw.js", {
            scope: "/firebase-cloud-messaging-push-scope",
          });
        });
      }
    </script>

What am I missing?

I'm using flutter web with firebase_messaging

I tried to follow the documentation's steps:

I created a VAPID:

I'm getting the token with

    await FirebaseMessaging.instance.requestPermission(provisional: true);
    final fmcToken = await FirebaseMessaging.instance.getToken(
      vapidKey: kIsWeb
          ? 'xxx'
          : null,
    );
    print('token: $fmcToken');

:

which gives me a token:

Launching lib/main.dart on Chrome in debug mode...
This app is linked to the debug service: ws://127.0.0.1:63406/1xasoU1wJzY=/ws
Debug service listening on ws://127.0.0.1:63406/1xasoU1wJzY=/ws
Connecting to VM Service at ws://127.0.0.1:63406/1xasoU1wJzY=/ws
token: xxx

I'm then using it to send a test notification:

But I'm not receiving any notification.

I did allowed the notifications:

I also included the web/firebase-messaging-sw.js:

importScripts("https://www.gstatic/firebasejs/10.11.1/firebase-app-compat.js");
importScripts("https://www.gstatic/firebasejs/10.11.1/firebase-messaging-compat.js");

firebase.initializeApp({
  apiKey: 'xxx',
  appId: 'xxx',
  messagingSenderId: 'xxx',
  projectId: 'xxx',
  authDomain: 'xxx',
  storageBucket: 'xxx',
  measurementId: 'xxx',
});
// Necessary to receive background messages:
const messaging = firebase.messaging();

// Optional:
messaging.onBackgroundMessage((m) => {
  console.log("onBackgroundMessage", m);
});

and here is my web/index.html:

 <body>
    <script>
      window.addEventListener("load", function (ev) {
        // Download main.dart.js
        _flutter.loader.loadEntrypoint({
          serviceWorker: {
            serviceWorkerVersion: serviceWorkerVersion,
          },
          onEntrypointLoaded: function (engineInitializer) {
            engineInitializer.initializeEngine().then(function (appRunner) {
              appRunner.runApp();
            });
          },
        });
      });
    </script>
    <script>
      if ("serviceWorker" in navigator) {
        window.addEventListener("load", function () {
          navigator.serviceWorker.register("firebase-messaging-sw.js", {
            scope: "/firebase-cloud-messaging-push-scope",
          });
        });
      }
    </script>

What am I missing?

Share Improve this question edited Feb 12 at 15:55 Frank van Puffelen 600k85 gold badges889 silver badges859 bronze badges Recognized by Google Cloud Collective asked Feb 12 at 2:56 Valentin VignalValentin Vignal 8,2784 gold badges40 silver badges95 bronze badges 2
  • Please do not upload images of code/data/errors. - Please edit and provide the code/data/errors as text. - Furthermore this looks like an SSL issue on your localhost – DarkBee Commented Feb 12 at 8:30
  • I edited the post to include more information and add the code/logs that was in the screenshots. Can you elaborate a bit on "this looks like an SSL issue on your localhost"? What does that mean and how can I troubleshoot/fix it ? – Valentin Vignal Commented Feb 12 at 15:03
Add a comment  | 

1 Answer 1

Reset to default 0

Do you have the firebase-messaging-sw.js file under your web directory? To receive push notifications, you need to set up a Service Worker at first.

Link

本文标签: Flutter webno firebase messaging notifications on macosStack Overflow