typeError:未定义不是对象(评估' navigator.serviceworker.addeventlistener')

发布于 2025-01-25 13:45:33 字数 1540 浏览 3 评论 0原文

我想使用firebase云消息传递向用户发送通知,但是当我尝试设置消息时,我会遇到此错误:

typeError:Undefined不是对象(评估'navigator.serviceworker.addeventlistener') 在node_modules/react-native/libraries/core/exceptionsManager.js:104:6中 在node_modules/react-native/libraries/core/exceptionsManager.js:172:19中 在node_modules/react-native/libraries/core/setUperrorhandling.js:24:6中 请访问node_modules/@React-native/polyfills/error-guard.js:49:36 in errorutils.reportfatalerror 在node_modules/metro-runtime/src/polyfills/require.js:204:6 in GuardedLoadModule 在屏幕/tabs/profile/userdata.js:16:4 in userdata

这是我的代码:

import { initializeApp, getApps, getApp } from "firebase/app";
import {getMessaging, getToken} from 'firebase/messaging';

const firebaseConfig = {
   apiKey: "...",
   appId: "...",
   authDomain: "...",
   projectId: "...",
   messagingSenderId: "...",
   storageBucket: "...",
};

export const firebaseApp = getApps().length === 0 ? initializeApp(firebaseConfig) : getApp(); 
const messaging = getMessaging(firebaseApp);


getToken(messaging, { vapidKey: '...' }).then((currentToken) => {
    if (currentToken) {
      // Send the token to your server and update the UI if necessary
      // ...
    } else {
      // Show permission request UI
      console.log('No registration token available. Request permission to generate one.');
      // ...
    }
  }).catch((err) => {
    console.log('An error occurred while retrieving token. ', err);
    // ...
  });

我已经在Firebase Cloud Messaging上生成了密钥

I want to use Firebase Cloud Messaging to send notifications to the users, but I am getting this error when I try to setup messageing:

TypeError: undefined is not an object (evaluating 'navigator.serviceWorker.addEventListener')
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError
at node_modules/metro-runtime/src/polyfills/require.js:204:6 in guardedLoadModule
at Screens/Tabs/Profile/UserData.js:16:4 in UserData

This is my code:

import { initializeApp, getApps, getApp } from "firebase/app";
import {getMessaging, getToken} from 'firebase/messaging';

const firebaseConfig = {
   apiKey: "...",
   appId: "...",
   authDomain: "...",
   projectId: "...",
   messagingSenderId: "...",
   storageBucket: "...",
};

export const firebaseApp = getApps().length === 0 ? initializeApp(firebaseConfig) : getApp(); 
const messaging = getMessaging(firebaseApp);


getToken(messaging, { vapidKey: '...' }).then((currentToken) => {
    if (currentToken) {
      // Send the token to your server and update the UI if necessary
      // ...
    } else {
      // Show permission request UI
      console.log('No registration token available. Request permission to generate one.');
      // ...
    }
  }).catch((err) => {
    console.log('An error occurred while retrieving token. ', err);
    // ...
  });

I have generated key on Firebase Cloud Messaging

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方圜几里 2025-02-01 13:45:33

我自己只是遇到了这个问题,这两种情况都在iOS上 - 不确定是否会更广泛地发生。 (我无法在Android上复制。)

  1. 从Safari上iOS上的Safari:通过HTTP(NO TLS)通过本地网络访问我的DEV框。
  2. 从iOS上的Facebook浏览器(我假设使用某种Safari WebView组件):通过HTTPS访问生产中的站点,该站点试图调用getMessaging(get> getMessaging()从firebase SDK引发此错误。

您会认为Firebase SDK可以优雅地处理此案,或者在文档中提供有关如何优雅地降低无支持的浏览器体验的明确说明,但我找不到任何主题的提及。

我这样解决了它:

let messaging;
try {
    messaging = getMessaging();
} catch (err) {
    console.error('Failed to initialize Firebase Messaging', err);
}

虽然这不允许您在不支持的浏览器上使用firebase消息传递,但至少您的应用程序/站点不会完全无法使用。在受支持的浏览器上,情况应该正常工作。

希望这有帮助!

I just ran into this issue myself, both scenarios were on iOS--not sure if this happens more broadly or not. (I could not duplicate on Android.)

  1. From Safari on iOS: Accessing my dev box over my local network via HTTP (no TLS) throws this error.
  2. From the Facebook browser on iOS (which I assume uses some sort of Safari WebView component): accessing a site in production over HTTPS that tries to call getMessaging() from the Firebase SDK throws this error.

You would think that the Firebase SDK would either gracefully handle this case or provide explicit instructions in the docs on how to gracefully degrade the experience in unsupported browsers, but I couldn't find any mention of the subject.

I solved it like this:

let messaging;
try {
    messaging = getMessaging();
} catch (err) {
    console.error('Failed to initialize Firebase Messaging', err);
}

While this won't allow you to use Firebase Messaging on unsupported browsers, at least your app/site won't be completely unusable. On supported browsers, things should work normally.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文