TypeError:firebase.default.messaging 不是函数(在 '_firebase.default.messaging()' 中,'firebase.default.messaging 未定义) [英] TypeError: firebase.default.messaging is not a function (in '_firebase.default.messaging()','firebase.default.messaging is undefined)

查看:26
本文介绍了TypeError:firebase.default.messaging 不是函数(在 '_firebase.default.messaging()' 中,'firebase.default.messaging 未定义)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 firebase 云功能发送通知,因此我尝试使用 firebase.messaging().getToken() 获取令牌,但我不断收到错误消息:

TypeError: firebase.default.messaging 不是函数(在 '_firebase.default.messaging()','firebase.default.messaging 未定义)

我已经安装了大约 5 种不同的方式的 firebase 和 firebase/messages,但似乎无法克服这个错误,所以我认为这些方法一定是过时的,或者我做错了什么.

这是我的代码:

import * as firebase from 'firebase'常量 Firebase = firebase.initializeApp(firebaseConfig);导出默认 Firebase

这是我得到错误的地方:

令牌:Firebase.messaging().getToken()

我不确定我是否缺少某些依赖项或任何类似的东西,因此非常感谢任何帮助.我一直在直接从firebase文档中安装东西,到目前为止一直在工作.

我也在使用 expo managed react native

谢谢

还有一些替代方法可以更好地使用事件侦听器在后台发送通知吗?

解决方案

你发帖已经有一段时间了,但我遇到了同样的错误,所以我将分享一些关于它的发现.您正在寻找的解决方案是第 3 个项目符号.

1) Firebase JavaScript SDK 适用于 Node.js 应用程序.尽管可以像 react-native/expo 项目中的任何其他 npm 包一样直接安装和使用 firebase sdk 包,但它应该在浏览器或 node.js 运行时上运行.

这特别令人困惑,因为某些 firebase Api 方法(例如 firebase.auth 和 firebase.database)可以在原生应用上工作,而另一些则不能.我猜这是因为某些方法只依赖于执行 HTTP 请求的能力.

实际上,firebase sdk 官方文档页面没有 react-native 部分.如果您查看

如果您仍想使用 firebase sdk,则应考虑诸如 React Native Firebase 之类的库,旨在解决在 node.js 运行时之外使用 JavaScript sdk 的问题.我自己没有尝试过,但是看看他们的网站,看起来要集成到 expo 管理的工作流程中并不容易.

2) 导入 @firebase/messaging 也不是一种选择:如 this answer,显式导入消息模块不会解决 react-native/expo 项目的问题.同样,如上所述,因为本机应用程序不在 node.js/browser 运行时上运行.事实上,它让事情变得更糟,因为它试图加载不可用的依赖项:

[09:56:33] ReferenceError:找不到变量:IDBIndex

3) 在 Expo 中获取用户设备通知推送令牌:由于您已经在使用 expo,您可以使用 expo-notifications 模块获取设备推送令牌,这将返回令牌,然后您可以从 Cloud Functions 使用该令牌通过 firebase-admin sdk.下面是一些示例代码,用于获取推送令牌并将其存储在实时数据库中(稍后从云函数中使用):

import * as Notifications from 'expo-notifications';从'expo-permissions'导入*作为权限;从'firebase'导入firebase;导出 const getDevicePushToken = () =>{返回 Permissions.getAsync(Permissions.NOTIFICATIONS).then((响应) =>response.status === '授予'?回复: Permissions.askAsync(Permissions.NOTIFICATIONS)).then((响应) => {if (response.status !== 'granted') {return Promise.reject(new Error('推送通知权限被拒绝'));}返回 Notifications.getDevicePushTokenAsync();}).then(token => {firebase.database().ref('...').update({ pushToken: token.data });}).catch((错误) => {console.log('注册设备推送令牌时出错', error);});};

干杯!

I want to send notifications using firebase cloud functions so I am trying to get a token using firebase.messaging().getToken() but I keep getting the error:

TypeError: firebase.default.messaging is not a function (in '_firebase.default.messaging()','firebase.default.messaging is undefined)

I have installed firebase and firebase/messages about 5 different ways but cannot seem to get past this error so I assume that those methods must be outdated or I am doing something very wrong.

Here is my code:

import * as firebase from 'firebase'
const Firebase = firebase.initializeApp(firebaseConfig);
export default Firebase

and here is where I am getting the error:

Token : Firebase.messaging().getToken()

I am not sure if I am missing some dependency or anything of that sort so any help would be very appreciated. I have been installing things straight from the firebase documentation as web which has been working so far.

I am also using expo managed react native

Thank you

Also is there some alternative way to send notifications in the background with an event listener that may be better?

解决方案

It's been a while since you posted, but I've gone through the same error so I will share some findings about it. The solution you are looking for is bullet number 3.

1) Firebase JavaScript SDK is meant for Node.js applications. Even though the firebase sdk package can be installed and used straightaway just like any other npm package in a react-native/expo project, it's meant to run on a browser or node.js runtime.

It's specially confusing because some of the firebase Api methods (e.g. firebase.auth and firebase.database) do work on native apps while others don't. I guess that's due to the fact that some methods only rely on the hability to perform HTTP requests.

In deed the firebase sdk official documentation page doesn't have a react-native section. If you check out the JavaScript section, and click on Add Firebase to your JavaScript Project you will see that the intended uses comprehend web apps and node.js desktop apps (not sure which runtime IoT apps run on):

If you still want to use the firebase sdk you should then consider libraries such as React Native Firebase which aims to solve the issues of using the JavaScript sdk outside a node.js runtime. I haven't tried myself but having a look at their website, looks like it's not going to be easy to integrate in the expo managed workflow.

2) Importing @firebase/messaging is not an option either: As suggested in this answer, explicitly importing the messaging module will not solve the issue for react-native/expo projects. Again, as described above, because native apps do not run on a node.js/browser runtime. In fact, it makes things worst because it tries to load dependencies that aren't available:

[09:56:33] ReferenceError: Can't find variable: IDBIndex

3) Getting the user device notifications push token in Expo: Since you are already using expo, you can use the expo-notifications module to get the device push token, which will return the token you then can use from Cloud Functions to send a notification through the firebase-admin sdk. Here is some sample code to get the push token and storing it in the realtime database (to be later used from the cloud functions):

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);
        });
};

Cheers!

这篇关于TypeError:firebase.default.messaging 不是函数(在 '_firebase.default.messaging()' 中,'firebase.default.messaging 未定义)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆