自从你发帖以来已经有一段时间了,但我也遇到了同样的错误,所以我将分享一些关于它的发现。您正在寻找的解决方案是项目符号 3。
1) Firebase JavaScript SDK 适用于 Node.js 应用程序。尽管 firebase sdk 包可以像 React-native/expo 项目中的任何其他 npm 包一样直接安装和使用,但它意味着在浏览器或 Node.js 运行时上运行。
这特别令人困惑,因为一些 firebase Api 方法(例如 firebase.auth 和 firebase.database)可以在本机应用程序上运行,而其他方法则不能。我猜想这是因为某些方法仅依赖于执行 HTTP 请求的能力。
事实上,firebase sdk 官方文档页面没有react-native 部分。如果您查看JavaScript 部分,然后单击将 Firebase 添加到您的 JavaScript 项目您将看到预期用途包括 Web 应用程序和 Node.js 桌面应用程序(不确定 IoT 应用程序在哪个运行时运行):
如果您仍然想使用 firebase sdk,那么您应该考虑诸如反应原生 Firebase旨在解决在 Node.js 运行时之外使用 JavaScript sdk 的问题。我自己没有尝试过,但看看他们的网站,看起来集成到世博会管理工作流程中并不容易。
2)导入@firebase/messaging也不是一个选项:正如建议的那样这个答案,显式导入消息模块并不能解决react-native/expo项目的问题。同样,如上所述,因为本机应用程序不在 node.js/浏览器运行时上运行。事实上,它使事情变得更糟,因为它尝试加载不可用的依赖项:
[09:56:33] ReferenceError: Can't find variable: IDBIndex
3)在Expo中获取用户设备通知推送令牌:由于您已经在使用 expo,因此您可以使用博览会通知模块获取设备推送令牌,该令牌将返回令牌,然后您可以使用 Cloud Functions 通过以下方式发送通知firebase 管理 sdk。以下是一些示例代码,用于获取推送令牌并将其存储在实时数据库中(稍后从云函数中使用):
import * as Notifications from 'expo-notifications';
import * as Permissions from 'expo-permissions';
import firebase from 'firebase';
export const getDevicePushToken = () => {
return Permissions.getAsync(Permissions.NOTIFICATIONS)
.then((response) =>
response.status === 'granted'
? response
: Permissions.askAsync(Permissions.NOTIFICATIONS)
)
.then((response) => {
if (response.status !== 'granted') {
return Promise.reject(new Error('Push notifications permission was rejected'));
}
return Notifications.getDevicePushTokenAsync();
})
.then(token => {
firebase.database().ref('...').update({ pushToken: token.data });
})
.catch((error) => {
console.log('Error while registering device push token', error);
});
};