typeError:未定义不是对象(评估' navigator.serviceworker.addeventlistener')
我想使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己只是遇到了这个问题,这两种情况都在iOS上 - 不确定是否会更广泛地发生。 (我无法在Android上复制。)
getMessaging(get> getMessaging()
从firebase SDK引发此错误。您会认为Firebase SDK可以优雅地处理此案,或者在文档中提供有关如何优雅地降低无支持的浏览器体验的明确说明,但我找不到任何主题的提及。
我这样解决了它:
虽然这不允许您在不支持的浏览器上使用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.)
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:
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!